A function in C++ is a group of statements that together perform a specific task. Every C/C++ program has at least one function that the name is main. The main function is called by the operating system by which our code is executed. We can make n number of function in a single program but we can make only one main function in a single program. Every program has only one main function.
The main part of functions is return_type, function_name, parameter and functions body.
Syntax :-
Return_type Function_name (Parameters)
{
// Function Body
}
Let’s see this topic in deep
- Return Type − A function may return some value. A return_type is the data type of the value the function returns. Some functions perform the desired operations and do not return any value. In this case, the return_type is specified by the keyword void.
- Function Name − It is the actual name of the function. The function name and the parameter list together form the function signature.
- Parameters − A parameter is like a placeholder. Whenever a function is called or invoked, you pass a value to the parameter. This value is referred to as an actual parameter or argument. The parameter list refers to the type, order, and a number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters.
- Function Body − The function body constitutes a collection of statements that define what the function does.
Also Read: Function overloading in C++
Function Declaration
Before writing the body of function just declare the function. if we declare the function then we write the definition of function anywhere in the program. We always declare a function above the main function. In a function declaration, we use; in last.
Syntax-
Return_type Fuction_name(arguments);
Function Definition
Function definition show the work of function or this is define what function do.
Syntax-
Return_type Fuction_name(arguments)
{//body}
How to call a Functions
Four ways to call a functions
Syntax-
Fuction_name();
Fuction_name(arguments);
Variable_name = Fuction_name();
Variable_name = Fuction_name(arguments);
Types of functions
We can declare a function in four ways:-
- A function that not take any argument and not return any value that type of function is take nothing and return nothing type
Syntax:-
void Function_name (void)
{
// Function Body
}
- A function that takes an argument and not returns any value that type of function is take something and return nothing type
Syntax:-
void Function_name (Parameter)
{
// Function Body
}
3. A function that not take any argument and return any value that type of function is take nothing and return something type
Syntax :-
Return_type Function_name (void)
{
// Function Body
}
4. A function that takes an argument and return any value that type of function is take something and return something type
Syntax :-
Return_type Function_name (Parameter)
{
// Function Body
}
Let’s understand this concept with one example
Here we take the addition of two number as an example.
Take Nothing Return Nothing type function
int main()———————–line 1
{
addition();———————–line 2
}
line1- Simply we declare the main function in every program.
Line2- Here we call the function that doesn’t take any arguments and return nothing.
Let’s see the body of addition function
void addition( void ) ————LINEA
{
int a=10,b=20,c;————line3
c=a+b;————line4
cout<<c; ————line5
}
Line3- here we initialize a and b with 10 and 20 and declare a variable name c.
Line4- we store the addition of a and b in c
Line5- it shows the addition of a and b or prints the value of c.
Take Something Return Nothing type function
int main()———————–line 1
{
int a=10,b=20;———————–line 2
addition(a,b);———————–line 3
}
line1- Simply we declare the main function in every program.
line2- we initialize two variable that is int type the value of a is 10 and b is 20.
line3- Here we call the function that takes two arguments and returns nothing.
Let’s see the body of addition function
void addition(int a, int b) ————LINEA
{
int c; ————line4
c=a+b; ————line5
cout<<c; ————line6
}
line4- here we declare a variable name c.
line5- we store the addition of a and b in the c
line6- it shows the addition of a and b.
Note:- a and b of both line ( LINE A and line 3 ) are different. We can take another variable name on LINE A.
Take Nothing Return Something type function
int main()———————–line 1
{
int c;———————–line 2
c=addition();———————–line 3
cout<<c;————————line 4
}
line1- Simply we declare the main function in every program.
line2- we initialize the c variable that store the function return value.
line3- Here we call the function that takes nothing but returns some value that is store inc.
Line4 – print the value that function addition return.
Let’s see the body of addition function
int addition( void ) ————LINEA
{
int a=10,b=20,c;————line5
c=a+b;————line6
return c————line7
}
Line5- here we declare a variable name c.
Line6- we store the addition of a and b in c
Line7- it returns the c.
Take Something Return Something type function
int main()———————–line 1
{
int a=10,b=20 , c ; ———————–line 2
c=addition(a,b); ———————–line 3
cout<<c;————————line 4
}
line1- Simply we declare the main function in every program.
line2- we initialize a,b and declare c.
line3- Here we call the function that takes and b as arguments and return some value that is store inc.
Line4 – print the value that function addition return.
Let’s see the body of addition function
int addition( int m, int n) ————LINEA
{
int c;————line5
c=n+m;————line6
return c————line7
}
Line5- here we declare a variable name c.
Line6- we store the addition of m and n in c
Line7- it returns the value of c.
Also Read: C++ Interview Questions
A real-world example of a function:
Let’ assume function like a worker of the company that takes some work from the boss and do some task or some operation on work and then gives the report to Boss that is the result that we return from functions.