In a C++ programming language a function is a group of statements that together perform a special task.
Every C++ program has at least one function, which is main().
In a C++ library has a numerous predefined or built-in functions.
For example, strcat() to concatenate two strings, main() from where program execution starts.
In a C++ programming function has a 3 part:
- Function declaration
- Function definition
- Function call
Note: In a C++ program we can remove the function definition but we must place the function definition before the main() function.
Function declaration : function declaration tells the compiler about a function’s name, return type, and parameters.
A syntax of function declaration –
1 | return_type function_name( parameter list ); |
Example of the function declaration is as follows −
1 | int max(int num1, int num2); |
here int is a return type of function which tells function will return integer value and max is a name of the function.
“ int num1, int num2” is a parameter list which tells max function will receive two integer arguments.
Arguments are optional which depends on program requirements.
Function definition : function definition is an actual body of the function.
Syntax of function definition:
1 2 3 4 | return_type function_name( parameter list ) { body of the function } |
function definition consist a return type, function name, parameter list and body of the function.
Function call : whenever program calls a function, the flow of control is transferred to the called function definition.
A called function performs a defined task and when flow of control reached to function-ending closing brace, flow of control returns to back to the main program.
Example of a user defined function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include <iostream> using namespace std; void DemoFunction(); // Function declaration int main() // The main method { DemoFunction(); // call the function return 0; } void DemoFunction() // Function definition { cout << "Hello Aditya"; } |
Output
1 | Hello Aditya |
Description: Program execution start from main() function.
In main() when DemoFunction(); executes it calls and transfer control to the function definition.
Then statements of DemoFunction() execute and print “Hello Aditya”.
Note: In a C++ function can be called multiple times.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include <iostream> using namespace std; void DemoFunction(); // Function declaration int main() // The main method { DemoFunction(); // call the function DemoFunction(); // call the function DemoFunction(); // call the function return 0; } void DemoFunction() // Function definition { cout << "Hello Aditya \n"; } |
Output
1 2 3 | Hello Aditya Hello Aditya Hello Aditya |
Note: In a C++ program we can remove the function definition but we must place the function definition before the main() function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #include <iostream> using namespace std; void DemoFunction() // Function definition { cout << "Hello Aditya"; } int main() // The main method { DemoFunction(); // call the function return 0; } |
Output
1 | Hello Aditya |
User-defined
function can’t declared after the main()
In a C++ program we can remove the function definition but we must place the function definition before the main() function.
If we place the function definition after main() then we will get the error message.
Because in a C++ execution occurs from top-to-bottom and if the function is not declared above main()
, the program is unaware of it.
1 2 3 4 5 6 7 8 9 10 11 12 13 | #include <iostream> using namespace std; int main() // The main method { DemoFunction(); // call the function return 0; } void DemoFunction() // Function definition { cout << "Hello Aditya"; } |
1 | error: ‘DemoFunction’ was not declared in this scope |
Categories of function
We can categories the function as:
- Function with no argument, no return value
- Function with argument but no return value
- Function with argument and return value
- Function with no argument, but return value
Function with no argument & no return value
Example: Write a program to find the area of circle using function with no argument, no return value.
The formula for the area of the circle is :
Area_circle = Π * r * r
Program: To calculate Area of Circle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include <iostream> using namespace std; void areaOfcircle(); //Function declaration int main() { areaOfcircle(); // Function Call return 0; } void areaOfcircle() // function definition to calculate area of circle { float area, radius; cout << "Enter the radius of circle : "; cin >> radius; // take radius as input area = 3.14 * radius * radius; cout << "\n Area of circle= : " << area; } |
Output
1 2 3 | Enter the radius of circle 5 // enter by user Area of circle=78.50 |
Function with argument but no return value
Example: Write a program to find the area of circle using function with argument but no return value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include <iostream> using namespace std; void areaOfcircle(float r); //Function declaration int main() { float radius; cout << "Enter the radius of circle : "; cin >> radius; // take radius as input areaOfcircle(radius); // Function Call return 0; } void areaOfcircle(float r) // function definition to calculate area of circle { float area; area = 3.14 * r * r; cout << "\n Area of circle= : " << area; } |
Output
1 2 3 | Enter the radius of circle 5 // enter by user Area of circle=78.50 |
Function with argument & return value
Example: Write a program to find the area of circle using function with argument but return value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include <iostream> using namespace std; float areaOfcircle(float r); //Function declaration int main() { float radius, a; cout << "Enter the radius of circle : "; cin >> radius; // take radius as input a = areaOfcircle(radius); // Function Call cout << "\n Area of circle= : " << a; return 0; } float areaOfcircle(float r) // function definition to calculate area of circle { float area; area = 3.14 * r * r; return area; } |
Output
1 2 3 | Enter the radius of circle 5 // enter by user Area of circle=78.50 |
Function with no argument but return value
Example: Write a program to find the area of circle using function with no argument but return value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include <iostream> using namespace std; float areaOfcircle(); //Function declaration int main() { float a; a = areaOfcircle(); // Function Call cout << "\n Area of circle= : " << a; return 0; } float areaOfcircle() // function definition to calculate area of circle { float radius, area; cout << "Enter the radius of circle : "; cin >> radius; // take radius as input area = 3.14 * radius * radius; return area; } |
Output
1 2 3 | Enter the radius of circle 5 // enter by user Area of circle=78.50 |
Call by value & Call by reference
Call by value : Whenever we call a function and passes the value of variable to the called function is called call by value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include <iostream> using namespace std; void swap(int a, int b) { int temp; temp = a; a = b; b = temp; } int main() { int a = 10, b = 20; cout << "Value of a & b before swap=" << a <<" "<< b << endl; swap(a, b); // passing value to function cout << "Value of a & b after swap=" << a <<" "<< b; return 0; } |
Output
1 2 | Value of a & b before swap=10 20 Value of a & b after swap=10 20 |
Call by reference : Whenever we call a function and passes the reference of variable to the called function is called call by reference.
In the call by reference, to pass the reference of the variable we use pointer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include <iostream> using namespace std; void swap(int * x, int * y) { int swap; swap = * x; * x = * y; * y = swap; } int main() { int x = 10, y = 20; cout << "Value of x & y before swap=: " << x <<" "<< y << endl; swap( & x, & y); // passing reference/ address of variable to function cout << "Value of x & y after swap=: " << x <<" "<< y; return 0; } |
Output
1 2 | Value of x & y before swap=: 10 20 Value of x & y after swap=: 20 10 |