Understanding Functions
Introduction to Functions
In C++, a function is a group of statements that together perform a task. Every C++ program has at least one function, which is main()
, and all the most trivial programs can define additional functions.
You can divide up your code into separate functions. How you divide up your code among different functions is up to you, but logically the division is such that each function performs a specific task.
A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function.
In C++, all functions are globally available, i.e., a function can be called from anywhere.
Defining a Function
A function definition in C++ programming consists of a function header and a function body. Here are all the parts of a function:
return_type function_name( parameter list ) {
body of the function
}
return_type
: A function may return a value. Thereturn_type
is the data type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, thereturn_type
is the keywordvoid
.function_name
: This is the actual name of the function. The function name and the parameter list together constitute the function signature.parameters
: A parameter is like a placeholder. When a function is 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 number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters.function body
: The function body contains a collection of statements that define what the function does.
Here is the source code for a function called Add()
. This function takes two parameters, Num1
and Num2
, and returns the sum of these numbers:
int Add(int Num1, int Num2) {
int result;
result = Num1 + Num2;
return result;
}
Calling a Function
While creating a C++ function, you give a definition of what the function has to do. To use a function, you will have to call or invoke that function.
When a program calls a function, the program control is transferred to the called function. A called function performs a defined task, and when its return statement is executed or when its function-ending closing brace is reached, it returns the program control back to the main program.
To call a function, you simply need to pass the required parameters along with the function name. If the function returns a value, you can store that value. For example:
#include <iostream>
using namespace std;
int Add(int Num1, int Num2) {
int result;
result = Num1 + Num2;
return result;
}
int main() {
int a = 5;
int b = 7;
int sum;
sum = Add(a, b);
cout << "The sum is: " << sum << endl;
return 0;
}
In this example, the function Add()
is called from the main()
function.
Function Arguments
If a function is to use arguments, it must declare variables that accept the values of the arguments. These variables are called the formal parameters of the function.
The formal parameters behave like other local variables inside the function. They are created upon entry into the function and destroyed upon exit.
While calling a function, there are two ways in which arguments can be passed to a function:
- Call by value
- Call by reference
Call by Value
This method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.
Call by Reference
This method copies the reference of an argument into the formal parameter. Inside the function, the reference is used to access the actual argument used in the call. This means that changes made to the parameter affect the argument.
Conclusion
So, functions in C++ are important for structuring the code and making the code reusable. The usage of functions enhances the readability of the code, makes the code easier to manage, and lends itself to more effective debugging.