![]() |
CPP makes function prototype in C language makes it |
How To Use Functions in C++ Explain Briefly With Examples
A function is a set of statements that take inputs, do some specific computation, and produce output.
The idea is to place some ordinarily or repeatedly done task along and build an operation, so rather than writing constant code once more and once more for various inputs, we can decide the operation function.
The idea is to place some ordinarily or repeatedly done task along and build an operation, so rather than writing constant code once more and once more for various inputs, we can decide the operation function.
Some Others Programming Language Functions
We can call a function again and again.
The most important feature of function is code reusability.
The C++ library provides many pre-defined functions.
Syntax of Function C++
return_Type Function_Name(Parameter List)
{
Statement;
}
return_Type Function_Name(Parameter List)
{
Statement;
}
Function Declaration in C++
Return Type: A operation could come at a price. The return type is that the knowledge variety of the worth the operate returns. Some functions perform the required operations while not returning a price.
Function Name: This is that the actual name of the operation. The operating name and also the argument(parameter) list along represent the operate signature.
Arguments: A parameter is sort of a placeholder. once an operation is invoked, you pass a price to the parameter. This price is mentioned as an actual parameter or argument.
The parameter list refers to the sort, order, and range of the parameters of an operation. Parameters square measure optional; that's, an operation could contain no parameters.
Function Body: The operating body contains a group of statements that outline what the operation will do.
Return Type: A operation could come at a price. The return type is that the knowledge variety of the worth the operate returns.
Some functions perform the required operations while not returning a price.
Function Name: This is that the actual name of the operation. The operating name and also the argument(parameter) list along represent the operate signature.
Arguments: A parameter is sort of a placeholder. once an operation is invoked, you pass a price to the parameter. This price is mentioned as an actual parameter or argument.
The parameter list refers to the sort, order, and range of the parameters of an operation. Parameters square measure optional; that's, an operation could contain no parameters.
Function Body: The operating body contains a group of statements that outline what the operation will do.
A key point about the function.
void data();
A operate declaration during which void(no come type) indicates there's no price returning by this operate and data is that the name of operating.
How To Use Function in C++?
#include<iostream>
using namespace std;
void data() {
int x=10;
int y=20;
int z=x+y;
cout<<"Addition:"<<z;
}
#include<iostream>
using namespace std;
void data()
{
int x=10;
int y=20;
int z=x+y;
cout<<"Addition:"<<z;
}
This is an operation definition and here we can see that code is written to perform the addition task.
Function Calling: At this stage, the function is termed.
data();
To decision a perform simply write perform name and place semi-colon(;).
How To Use Function in C++?
#include<iostream>
using namespace std;
void data();
{ int x=100;
int y=200;
int z=x+y; cout<<"Addition of data:"<<z;
}
int main()
{
data();
} *****OUTPUT*****
Addition of data:300
#include<iostream>
using namespace std;
void data();
{
int x=100;
int y=200;
int z=x+y;
cout<<"Addition of data:"<<z;
}
int main()
{
data();
}
*****OUTPUT*****
Addition of data:300
Predefined Function in C++
A predefined function in the library in C++ Language is called a predefined function.cout()
cin()
clrscr()
getch()
User-defined Function in C++
A User-defined function that is made by the user is called a user-defined function.Addition()
Subtraction()
Multiplication()
Division()
Category of Userdefined Function
There are four categories of user-defined function1. operate with no come kind and no parameter
2. Function is used with no return type and with parameter.
3. Function with return type and no parameter.
4. operate with come kind and with parameter.
Function no return type and no parameter
The operation during which there's no parameter and there's no worth coming by that operation is termed operate with no return kind and no parameter.How To Use No Return Type No Parameter Function in C++?
#include<iostream>
using namespace std;
void data();
{
int x=20;
int y=20;
int z=x*y;
cout<<"Multiplication of data:"<<z;
}
int main()
{
data();
}
*****OUTPUT*****
Multiplication of data:400
#include<iostream>
using namespace std;
void data();
{
int x=20;
int y=20;
int z=x*y;
cout<<"Multiplication of data:"<<z;
}
int main()
{
data();
}
*****OUTPUT*****
Multiplication of data:400
Function with no return type and with parameter
The operation during which there's no parameter and there's some worth coming by that operation is termed operate with return kind and no parameter.How To Use No Return Type With Parameter Function in C++?
#include<iostream>
using namespace std;
void data(int x,int y);
{ int z=x+y;
cout<<"Addition of data:"<<z;
}
int main()
{ data(100,100);
} *****OUTPUT*****
Addition of data:200
#include<iostream>
using namespace std;
void data(int x,int y);
{
int z=x+y;
cout<<"Addition of data:"<<z;
}
int main()
{
data(100,100);
}
*****OUTPUT*****
Addition of data:200
In the above example, there are two parameters of integer type names x and y there at the time of calling two integer values will be passed in which first will assign to x and the second will assign to y.
variable z in-store data information.
Function with return type and no parameter
The function in which there is no parameter and there is some value returning by that function is called Function with return type and no parameter.How To Return Type No Parameter Function in C++
#include<iostream>
using namespace std;
int data();
{ int x=100;
int y=100;
int z=x+y; return z;
}
int main()
{
data();
cout<<"Addition of data:"<<z;
} *****OUTPUT*****
Addition of data:200
#include<iostream>
using namespace std;
int data();
{
int x=100;
int y=100;
int z=x+y;
return z;
}
int main()
{
data();
cout<<"Addition of data:"<<z;
}
*****OUTPUT*****
Addition of data:200
Above example, there is no parameter but the function will return an integer value because there is an int keyword in the place of return type and returned value will assign to variable z.
Function with return type and with parameter
The perform during which there's no parameter and there's some worth come backing by that perform is named perform with return kind and no parameter.
How To Return Type With Parameter Function in C++?
#include<iostream>
using namespace std;
int data(int x,int y);
int data(int x,int y)
{ int z;
int z=x+y;
return z;
}
int main()
{ int a=data(100,100);
cout<<"Addition of data:"<<a;
} *****OUTPUT*****
Addition of data:200
#include<iostream>
using namespace std;
int data(int x,int y);
int data(int x,int y)
{
int z;
int z=x+y;
return z;
}
int main()
{
int a=data(100,100);
cout<<"Addition of data:"<<a;
}
*****OUTPUT*****
Addition of data:200
Above example, there are two parameters and the function will return an integer value because there is an int keyword in the place of return type and returned value will assign to variable a.
Calling of Function
There are two ways of calling a function
Calling of Function
There are two ways of calling a function
1. Call By Value.
2. Call By Reference.
Call by Value: In this type of calling a function direct value is passed at the time of calling.
Indecision by worth the changes created informal parameters do not replicate in actual parameters.
How To Call By Value Function in C++?
#include<iostream>
using namespace std;
void data(int a)
{
a=a+100;
int main()
{ int x=100;
cout<<"Before Calling of data:"<<x;
data(x);
cout<<"After Calling of data:"<<x;
} *****OUTPUT*****
Before Calling of data:100
After Calling of data:100
#include<iostream>
using namespace std;
void data(int a)
{
a=a+100;
int main()
{
int x=100;
cout<<"Before Calling of data:"<<x;
data(x);
cout<<"After Calling of data:"<<x;
}
*****OUTPUT*****
Before Calling of data:100
After Calling of data:100
Call by Reference: In this type of calling a function, the reference of the value is passed at the time of calling.
call by value the changes made informal parameters reflect in actual parameters, Reference is also called address.
When the address of information is passed at the time of business thus it's necessary to use a pointer within the place of a parameter.
For better understanding see the example below.
How To Call By Reference Function in C++?
#include<iostream>
using namespace std;
void data(int *a)
{
*a=*a+100;
int main()
{ int x=100;
cout<<"Before Calling of data:"<<x;
data(&x);
cout<<"After Calling of data:"<<x;
} *****OUTPUT*****
Before Calling of data:100
After Calling of data:200
#include<iostream>
using namespace std;
void data(int *a)
{
*a=*a+100;
int main()
{
int x=100;
cout<<"Before Calling of data:"<<x;
data(&x);
cout<<"After Calling of data:"<<x;
}
*****OUTPUT*****
Before Calling of data:100
After Calling of data:200
Function with the default value
default value: In this type of function ,the function contains a number of parameter with some initial value[for example:void data(int x=100,int y=200)].At the time of calling if there is no value is passed [for example: data();] then the default value will be x=100 and y=200,but if value is passed [for example: sum(20,10);] then the value will be x=20 and y=10.
How To Default Value Function in C++?
#include<iostream>
using namespace std;
void data(int x=100,int y=200)
{
int res=x+y;
cout<<"Adition of data:"<<res;
}
int main()
{ int x=100;
int y=200;
cout<<"Without the value of Add data:";
data();
cout<<"With the value of Add data:";
data(20,10);
} *****OUTPUT*****
Addition of data:300
Without the value of Add data:300
With a value of Add data:30
#include<iostream>
using namespace std;
void data(int x=100,int y=200)
{
int res=x+y;
cout<<"Adition of data:"<<res;
}
int main()
{
int x=100;
int y=200;
cout<<"Without the value of Add data:";
data();
cout<<"With the value of Add data:";
data(20,10);
}
*****OUTPUT*****
Addition of data:300
Without the value of Add data:300
With a value of Add data:30
Passing Array to Function
Array Function: In this type of function, there is an array in the place of the parameter.
example: void data(int a[5])] and its value are passed at the time of calling.
How To Array Function in C++?
#include<iostream>
using namespace std;
void data(int a[5])
{
int r=0;
for(int i=0;i<5;i++)
r=r+a[i];
cout<<"Total Addition Element of data:"<<r;
}
int main()
{ int x[5]={80,77 14,78,01};
data(x);
} *****OUTPUT*****
Total Addition Element of data:250
#include<iostream>
using namespace std;
void data(int a[5])
{
int r=0;
for(int i=0;i<5;i++)
r=r+a[i];
cout<<"Total Addition Element of data:"<<r;
}
int main()
{
int x[5]={80,77 14,78,01};
data(x);
}
*****OUTPUT*****
Total Addition Element of data:250
Recursion: The process of calling a function by itself is called Recursion and the function that calls itself is called Recursive Function.
How To Find Factorial of any number in C++?
#include<iostream>
using namespace std; void factorial(int n,int f)
{
if(n>=1) { f=f*n; n--; factorial(n,f);
}
else cout<<"Factorial:"<<f; } int main() { int num;
cout<<"Enter any number to find factorial:"; cin>>num;
factorial(num,1);
} *****OUTPUT*****Enter any number to find factorial:5Factorial:120
#include<iostream>
using namespace std;
void factorial(int n,int f)
{
if(n>=1)
{
f=f*n; n--;
factorial(n,f);
}
else
cout<<"Factorial:"<<f;
}
int main()
{
int num;
cout<<"Enter any number to find factorial:";
cin>>num;
factorial(num,1);
}
*****OUTPUT*****
Enter any number to find factorial:5
Factorial:120
0 Comments