Hello folks, In the previous lecture, we discussed about Goto statement in cpp. Today, we will learn about functions in cpp. Functions are an important concept in every programming language.
A function can accept data or information in the form of parameters. Functions are used to perform specific actions and are essential for code–reuse.
Code-reuse: Create the code once and then reuse it.
Functions: A function in cpp is defined as a group of statements that cooperate to complete a task.
Every Cpp program includes at least one function, main(), and even the most basic programs can define additional functions.
You can separate your code into functions. It is up to you to divide your code into different functions, but each function should perform a specific task logically.
A function has three major components:
- Defining
- Declaration
- Calling
Related: StackOverflow developer survey 2022
Defining a function in CPP
A Cpp function definition takes the following general form:
return_type function_name( parameter list ) {
body of the function
}
A Cpp function definition comprises a function header and a function body. The component of a function are as follows:
1. Return type: A function can either return a value or not. Some functions perform the operations required but do not return a value. void
return type is used, when we don’t return anything.
2. Function Name: This is the function’s actual name. The function signature comprises the function name and the parameter list.
3. Parameters: A parameter is similar to a placeholder. When you call a function, you pass a value to the parameter. The value which is passed is known as the argument. The type, order, and some parameters of a function are referred to as the parameter list. It is also possible that a function has no parameters.
Example: The following is the source code for the max function (). This function accepts two parameters, num1 and num2, and returns the greater of the two.
4. Function Body: The function body is made up of statements that define what the function does.
int max(int num1, int num2) {
// local variable declaration
int result;
if (num1 > num2){
result = num1;
}
else{
result = num2;
}
return result;
}
Related: Star Pattern problems in Cpp | Practice you coding skills here
Declaring a function in CPP
A function declaration informs the compiler about the name of a function and how to call it. The function’s actual body can be defined separately.
A function declaration has the following parts:
return_type function_name( parameter list );
The function declaration for the previously defined function max()
is as follows:
int max(int num1, int num2);
The names of parameters are unimportant in function declarations; only their types are required, so the following is a valid declaration.
int max(int, int);
When you want to define a function in one source file and call it in another, you must use function declaration. So, in this case, You should declare the function at the top of the file that calls it.
Calling a function in CPP
When you write a Cpp function, you specify what it must do. For using a function, you must call it.
When a program calls a function, control is passed to that called function. When the control encounters the function’s return statement or function-ending closing brace, the control is passed to the main program. So, simply pass the required parameters along with the function name to call it, and if the function returns a value, you can store it.
As an example,
#include <iostream>
using namespace std;
// function declaration
int max(int num1, int num2);
int main () {
// local variable declaration:
int a = 100;
int b = 200;
int ret;
// function calling for the max value
ret = max(a, b);
cout << "Max value is : " << ret << endl;
return 0;
}
// function returning the max between two numbers
int max(int num1, int num2) {
// local variable declaration
int result;
if (num1 > num2)
{
result = num1;
}
else
{
result = num2;
}
return result;
}
I kept the max() and main() functions and compiled the source code. The above code produces the following result:
Max value is: 200
Related: Sophia Robot going for mass production
Function Arguments
If a function accepts arguments, it must declare variables that accept the arguments’ values. These variables are known as the function’s formal parameters.
The formal parameters operate similarly to other local variables within the function, they’re being created upon entry and destroyed upon exit.
There are two methods for passing arguments to a function when calling it.
1. Call by value
When we use ‘call by value’ method, the copy of the original variable is passed. Changes to the parameter within the function have no effect on the argument in this case.
2. Call by Reference
This method inserts an argument’s reference into the formal parameter. This reference is used within the function to access the actual argument used in the call. This means that changes to the parameter affect the argument.
Cpp uses call by value to pass the arguments by default. In general terms, this means that code within a function cannot change the arguments used to call the function, and the example mentioned above used the same method when calling the max() function.
Conclusion
In this article, we thoroughly discussed the functions in cpp. I hope you found this piece of article useful. If you have any doubts regarding that, you can comment below or you can email us at: cpptopics@techoutflow.com. The next article will be on default arguments in functions.
Till then, stay tuned