A function is a group of statements that collectively perform a task. To delve deeper into what functions are in C++, refer to the following article by Mytour.
Functions in C++
Functions in C++
In C++, functions are employed to provide modules for the program. Utilizing functions in application development helps developers easily detect, test, and rectify errors.
Syntax of Functions in C++
Below is the syntax for defining a function in C++:
Return Type-Function Name (Parameter1, Parameter2, ...)
{
// Function body
}
In which:
- Return type: suggests what the function will return; it can be int, char, ... or even an object class. Additionally, there are functions that do not return any value, referred to with void.
- Function name: is the name of the function, used when calling the function.
- Parameters: are variables to hold the values of the objects passed when the function is called. A function may or may not contain a parameter list.
- Function body: is the part where code statements are written.
Here is an example of declaring a function in C++:
// function to add 2 values
void sum(int x, int y)
{
int z;
z = x + y;
cout <>
}
int main()
{
int a = 10;
int b = 20;
// call the function named 'sum'
sum (a, b);
}
Here, a and b are 2 variables sent as arguments to the sum function, and x and y are parameters holding the values of a and b to perform the necessary operations inside the function.
Declare, define, and call a function in C++
Function declaration is performed to inform the compiler about the existence of a function. The return type, name, and parameter list are mentioned. The function body is written in the definition.
For a clearer understanding, readers can refer to the example below:
#include < iostream=''>
using namespace std;
//function declaration
int sum (int x, int y);
int main()
{
int a = 10;
int b = 20;
int c = sum (a, b); //call function
cout <>
}
//function definition
int sum (int x, int y)
{
return (x + y);
}
In the example above, a function is declared with no body. Inside the main() function, it is called, as the function returns the sum of 2 values, and variable c will store the result.
Finally, the function is defined, where the function body is specified. Additionally, we can declare and define functions together, but it must be done before the function is called.
Calling a Function in C++
To call any function, we use its name. If the function has no parameters, we can call it directly by its name. Conversely, if the function has parameters, we have two ways to call the function:
1. Call the function by value.
2. Call the function by reference.
Calling a Function by Value in C++
In calling a function by value in C++, we pass the values of the arguments stored or copied into the formal parameters of the function. Therefore, the initial values remain unchanged, and only the parameters inside the function are modified.
For example:
void calculate(int x);
int main()
{
int x = 10;
calculate(x);
printf('%d', x);
}
void calculate(int x)
{
x = x + 10 ;
}
The output result is 10.
In this case, the actual variable x cannot be changed because we pass the argument by value, so a copy of x will be passed, modified, and that copied value will disappear when the function ends (goes out of scope). Therefore, the variable x in the main() function still has a value of 10.
However, we can still modify the program to alter the initial value of x by using the calc() function to return a value and store that value in x.
int calculate(int x);
int main()
{
int x = 10;
x = calculate(x);
printf('%d', x);
}
int calculate(int x)
{
x = x + 10 ;
return x;
}
The output result is 20.
Calling a Function by Reference
In this case, we pass the address of the variable as an argument. Formal parameters can be taken as references or pointers, in both cases, they will alter the values of the original variable.
For example:
void modifyValue(int *pointer);
int main()
{
int x = 10;
modifyValue(&x); // send the address of variable x as an argument
printf('%d', x);
}
void modifyValue(int *pointer)
{
*pointer = *pointer + 10;
}
The output result is 20.
This article introduces you to functions in C++, how to use them. In the upcoming articles, we will continue to explore numbers in C++.
