3.10. Parameters and Arguments¶
Some of the built-in functions we have used have parameters, which
are values that you provide to let the function do its job. For example,
if you want to find the sine of a number, you have to indicate what the
number is. Thus, sin
takes a double
value as a parameter.
Some functions take more than one parameter, like pow
, which takes two
double
s, the base and the exponent.
Notice that in each of these cases we have to specify not only how many parameters there are, but also what type they are. So it shouldn’t surprise you that when you write a function definition, the parameter list indicates the type of each parameter. For example:
void print_twice (char phil) {
cout << phil << phil << '\n';
}
This function takes a single parameter, named ‘phil’, that has type char
.
Whatever that value is (and at this point we have no idea what it
is), it gets printed twice, followed by a new line. I chose the name phil
to suggest that the name you give a parameter is up to you, but in
general you want to choose something more illustrative than phil.
In order to call this function, we have to provide a char
. For example,
we might have a main
function like this:
int main () {
print_twice ('a');
return 0;
}
The char
value you provide is called an argument, and we say that
the argument is passed to the function. In this case the value ’a’
is passed as an argument to print_twice where it will get printed twice.
Alternatively, if we had a char
variable, we could use it as an argument
instead:
int main () {
char argument = 'b';
print_twice (argument);
return 0;
}
Notice something very important here: the name of the variable we pass as an argument (argument) has nothing to do with the name of the parameter (phil). Let me say that again:
Note
The name of the variable we pass as an argument has nothing to do with the name of the parameter.
They can be the same or they can be different, but it is important to realize that they are not the same thing, except that they happen to have the same value (in this case, the character ’b’).
Caution
The value you provide as an argument must have the same type as the parameter of the function you call.
This rule is important, but it is sometimes confusing because C++ sometimes converts arguments from one type to another automatically. Recall the section Converting from double to int. What is true in expressions is also true for function arguments. For now focus on the general rule, and we will deal with exceptions later.
The following code will show the output of the print_twice function. Try modifying the value of argument to change the output.
Q-2:
#include <iostream>
void print_twice (char phil) {
std::cout << phil << phil << '\n';
}
int main () {
string argument = "s";
print_twice (argument);
return 0;
}
What will be printed to the terminal? If nothing will print, or if the compiler will throw an error, type “error”.
Q-3:
#include <iostream>
void print_twice (char phil) {
std::cout << phil << phil << std::endl;
}
int main () {
char argument = 'b';
print_twice (argument);
return 0;
}
What will be printed to the terminal? If nothing will print, or if the compiler will throw an error, type “error”.
-
Q-4: Match the function declaration to an example of its function call.
Try again!
- int times_two(int x, int y);
- times_two(4, 7);
- int times_two(string y, int x);
- times_two("hello", 10);
- int times_two(double x, string y);
- times_two(4.5, "hello");
- int times_two(string x, string y);
- times_two("hello", "hi");
More to Explore
From cppreference.com