A function refers to a block of code (or a group of statements) that performs a specific task. A function takes some inputs, does some computation based on that input and returns the output. The function can be called many times by passing different values each time.
For example, let us say that you need to compute a complex multiplication for different values on a different part of a program. One thing you can do is repeat the block of code for computation again and again, which is not a good practice as a programmer and makes the program complex and time-consuming. Better you can create a function and call that function every time you need by passing the different values.
Every C++ program has at least one function which is always executed and that is the main
function from where the execution of the program begins. The function is declared within the curly braces {}.
Advantages of functions in C++
There are many advantages of using function in C++.
Reusability of code: As mention above in the example, we can reuse the function by calling it again and again as required in the program.
Optimization of Code: Use of functions makes the program short and simple to understand. You only need to write the logic of the calculation once and use it several times.
C++ function is defined into two categories:
1. Standard Library Functions
2. User-defined functions
1. C++ Standard Library Functions
These are the pre-defined function in a C++ program, like a main
function. These built-in functions are already defined and written in C++, to use them you just need to invoke or call them directly.
Some common library functions in C++ are sqrt()
, abs()
, isdigit()
, etc.
To use these library functions an appropriate header file needs to be included in the program. Example: to use math functions such as sqrt()
, you need to include cmath
header file.
Example of C++ Standard Library Functions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include <iostream> #include <cmath> //math header file to use maths library function using namespace std; int main() { double number, square_result; cout << "Enter the number: "; cin >> number; //here sqrt() is a library function that calculates the square root square_result = sqrt(number); cout << "The square root of " << number << " is: " << square_result; return 0; } |
1 2 3 4 | //Output: Enter the number: 625 The square root of 625 is: 25 |
2. C++ User-defined functions
These are the functions created by the programmer in order to use it as many times as required. The user or programmer writes a block of code inside a body of a function to do some specific task.
These functions can also be called or invoke from any part of the program and code inside the body of that function will be executed.
We will see the examples of both Library Functions and User-defined functions below in this article.
Example of C++ User-defined Functions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #include <iostream> using namespace std; //user-defined function int sum(int a, int b = 5) { int result; result = a + b; return result; } //Main function int main() { int a = 10, b = 20, result; // calling a function by passing a and b result = sum(a, b); cout << "Sum result: " << result << endl; // calling a function again by passing only a //In this case b will be added through the function defined above result = sum(a); cout << "Sum result: " << result << endl; return 0; } |
1 2 3 4 | //Output: Sum result: 30 Sum result: 15 |
Function Declaration or Prototype:
Declaring a function means telling the compiler about the function name and its return type and parameters (if any). it is like stating the prototype of the function.
In C++, the function declaration declares a function without a body. It is important to declare the function when you are using one of the source file function to another file. We declare the function at the top of the program.
The syntax for function declaration:
1 | return_type function_name( parameter list ); |
Example:
1 2 3 4 | int max(int, int); //function that takes a ponter and integer int *swap(int*,int); |
Note: Also note that, if you defined the function before the main function then the separate function declaration is not required in a program else you need to mention the declaration. Example: In the syntax below, a declaration is not required
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #include <iostream> using namespace std; //user-defined function int sum(int a, int b = 5) { ........... ........... return result; } //Main function int main() { ............. ............. // calling a function result = sum(a, b); ............ ............. return 0; } |
Function Definition
Generally, the function is defined in following way in C++:
1 2 3 | return_type function_name( parameter_list ) { body of the function; } |
Each part of a function:
- Return type: A function may return a value unless it is a void function, which does not return any type of value. return_type is a data type of the function. It could return arithmetic values (int, float, etc.), pointers, structures, etc.
- Function name: Function name refers to identifiers through which function can be identified uniquely. It is a function’s name that is stated to function by following some naming rules.
- Parameter list: Parameter is also referred to as Argument. We can pass the values through the parameter when the function is invoked. The parameter list refers to the type, order, and number of the parameters of a function
- Function Body: The function body is enclosed within the curly braces’{}’. It defines the task of a method within the curly braces.
Note: The function declaration and definition is for the user-defined type function only because the library function is already declared and defined. We just need to call the library function in order to use it.
Function Call
We need to call the function in order to use it. To call a function we simply write the function name by passing the values as parameters (if any).
When the function is called, the execution control passes to the function and does some specific task. When the return statement of the function is executed or when it executes the end braces, it means the program control is returned to the main function.
Following is the syntax t call a function.
1 | function_name( parameter_list ); |
Example: How to declare and define a function and call the function in C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #include <iostream> using namespace std; //declaring a function int sumFunc(int, int); //Main FUnction int main() { int x, y, sum; cout << "Enter two numbers to add: "; cin >> x >> y; //calling a function by passing x and y sum = sumFunc(x, y); cout << "The addition of " << x << " and " << y << " is: " << sum; return 0; } //function definition int sumFunc(int num1, int num2) { int result = num1 + num2; return result; //returning the result to main funciton } |
Output:
1 2 | Enter two numbers to add: 10 20 The addition of 10 and 20 is: 30 |
Function Arguments
The function is called by passing some values, and the function must declare variables in order to accept those values of the argument. These variables are called a formal parameters of the function and created during the entry of the function.
The actual arguments refers to the values passed while calling the function.
There are two ways in which the argument can be passed through a function:
- Call by Value.
- Call by Reference.
Click here to learn about them in detail.