Table of Contents
Function parameters are variables declared in a function’s parameter list. They receive values supplied by the caller and allow a function to operate on different input values.
Parameters are local to the function in which they are declared. Each time the function is called, its parameters are initialized with the corresponding arguments supplied by the caller.
Source Code #
#include <iostream>
int add(int first, int second)
{
return first + second;
}
int main()
{
int result = add(15, 25);
std::cout << "Sum: " << result << '\n';
return 0;
}
Output #
Sum: 40
Explanation #
firstandsecondare function parameters of theadd()function.- The parameters are declared inside the function’s parameter list.
firstandsecondare both of typeint.- Parameters receive values when the function is called.
add(15, 25)supplies15tofirstand25tosecond.- Parameters are local to the function and can be used within its function body.
- The function can use its parameters to perform operations on the values supplied by the caller.
- A function can have zero, one, or multiple parameters.
return first + second;returns the result calculated using the parameters.return 0;terminates the program successfully.