1.4a __ A first look at function parameters and arguments
Function parameters and arguments
In the previous lesson, you learned that a function can return a value back to the caller via the function’s return value.
In many cases, it is useful to be able to pass information to a function being called, so that the function has data to work with. For example, if we wanted to write a function to add two numbers, we need a way to tell the function which two numbers to add when we call it. Otherwise, how would the function know what to add? We do that via function parameters and arguments.
A function parameter is a variable used in a function where the value is provided by the caller of the function. Function parameters are placed in between the parenthesis after the function identifier, with multiple parameters being separated by commas.
Here’s some examples of functions with different numbers of parameters:
An argument is a value that is passed from the caller to the function when a function call is made:
Note that multiple arguments are also separated by commas. The number of arguments must match the number of function parameters. Otherwise, the compiler will throw an error.
How parameters and arguments work together
When a function is called, all of the parameters of the function are created as variables, and the value of each of the arguments is copied into the matching parameter. This process is called pass by value.
For example:
When printValues() is called with arguments 6 and 7, printValues’s parameter x is created and assigned the value of 6, and printValues’s parameter y is created and assigned the value of 7.
This results in the output:
How parameters and return values work together
By using both parameters and a return value, we can create functions that take data as input, do some calculation with it, and return the value to the caller.
Here is an example of a very simple function that adds two numbers together and returns the result to the caller.
When function add() is called, parameter x is assigned the value 4, and parameter y is assigned the value 5.
The function add() then evaluates x + y, which is the value 9, and returns this value back to function main(). This value of 9 is then sent to cout (by main()) to be printed on the screen.
Output:
In pictorial format:
More examples
Let’s take a look at some more function calls:
This program produces the output:
The first two statements are straightforward.
In the third statement, the parameters are expressions that get evaluated before being passed. In this case, 1 + 2 evaluates to 3, so 3 is passed to x. 3 * 4 evaluates to 12, so 12 is passed to y. add(3, 12) resolves to 15.
The next pair of statements is relatively easy as well:
In this case, add() is called where x = a and y = a. Since a = 5, add(a, a) = add(5, 5), which resolves to 10.
Let’s take a look at the first tricky statement in the bunch:
When the function add() is executed, the CPU needs to determine what the values for parameters x and y are. x is simple since we just passed it the integer 1, so it assigns x=1. To get a value for y, it needs to evaluate multiply(2, 3) first. The CPU assigns z = 2 and w = 3, and multiply(2, 3) returns the integer value 6. That return value of 6 can now be assigned to the y parameter of the add() function. add(1, 6) returns the integer 7, which is then passed to cout for printing.
Put less verbosely (where the => symbol is used to represent evaluation):
add(1, multiply(2, 3)) => add(1, 6) => 7
add(1, multiply(2, 3)) => add(1, 6) => 7
The following statement looks tricky because one of the parameters given to add() is another call to add().
But this case works exactly the same as the above case where one of the parameters is a call to multiply().
Before the CPU can evaluate the outer call to add(), it must evaluate the inner call to add(2, 3). add(2, 3) evaluates to 5. Now it can evaluate add(1, 5), which evaluates to the value 6. cout is passed the value 6.
Less verbosely:
add(1, add(2, 3)) => add(1, 5) => 6
add(1, add(2, 3)) => add(1, 5) => 6
Conclusion
Parameters are the key mechanism by which functions can be written in a reusable way, as it allows them to perform tasks without knowing the specific input values ahead of time. Those input values are passed in as arguments by the caller.
Return values allow a function to return a value back to the caller.
Quiz
1) What’s wrong with this program fragment?
2) What two things are wrong with this program fragment?
3) What value does the following program print?
4) Write a function called doubleNumber() that takes one integer parameter and returns twice the value passed in.
5) Write a complete program that reads an integer from the user (using cin, discussed in lesson 1.3a -- A first look at cout, cin, endl, namespaces, and using statements), doubles it using the doubleNumber() function you wrote for question 4, and then prints the doubled value out to the console.
Quiz Answers
To see these answers, select the area below with your mouse.
multiply() is defined as returning void, which means it can’t return a value. Since the function is trying to return a value, this function will produce a compiler error. The function should return an int.
Problem 1: main() passes one argument to multiply(), but multiply() requires two parameters.
Problem 2: multiply() calculates a value and puts the result in a variable, but never returns the value to the caller. Because there is no return statement, and the function is supposed to return an int, this will either produce a compiler error (on some compilers) or unexpected results (on other compilers).
multiply is called where x = add(1, 2, 3), and y = 4. First, the CPU resolves x = add(1, 2, 3), which returns 1 + 2 + 3, or x = 6. multiply(6, 4) = 24, which is the answer.
Note: You may come up with other (similar) solutions for #4 and #5. There are often many ways to do the same thing in C++.
No comments:
Post a Comment
whatiscpp.blogspot.com is a free website devoted to teaching you how to program in C++. Whether you’ve had any prior programming experience or not, the tutorials on this site will walk you through all the steps to write, compile, and debug your C++ programs, all with plenty of examples.