Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1 2
3 4
The return value is added by indicating its The function call is on the right side of the
data type, here an int, in front of the function assignment operator.
name in both the function prototype and To the left of the assignment operator is a
header: variable of the same data type as the return
int addNumbers(int, int); value of the function.
int addNumbers (int x, int y) The concept is that the return value from the
function call is assigned to the variable sum
on the left side of the assignment operator.
5 6
7 8
It is common that a function returning a value is the return value could have been displayed
called on the right side of an assignment operator as:
with a variable on the left side of the assignment
operator to capture the return value.
cout << firstNum << " + " << secondNum << " = "
However, this is not required.
In the program, the variable sum was not necessary. << addNumbers (firstNum, secondNum);
Instead of the lines
sum = addNumbers (firstNum, secondNum);
cout << firstNum << " + " << secondNum << " = "
<< sum;
9 10
The only difference is that once this cout statement While multiple values can be passed to a
completes, the return value of the function cannot be
used in later statements since it was not stored in a function as arguments, at this point, multiple
variable. values cannot be returned from functions using
In this program, that is not a problem because the return the data types we have covered so far.
value is not used again.
However, if you are going to use a return value more This will change when we cover arrays in the
than once, it’s generally a good idea to store that return next chapter (Chapter 6 – Arrays) and structures
value in a variable.
This is typically done by calling the function on the right (Chapter 7 – Structures) in later chapters
side of an assignment operator with a variable on the left However, we may return multiple values to a
side of the assignment operator to capture the return
value. function without using any return statements
11 12
Example 2
13 14
15 16
Function Overloading Example 4
17 18
Passing
arguments
to a function
Value Reference parameter Pointer Summary
by
The function header consists of a return type, a In programs where the only function is main, all
function name, and an argument list. variables defined at the top of that function
necessarily can be accessed throughout the entire
The function header always is followed by an open program.
curly brace, which begins the function body. However, once we start dividing up the code into
The function body ends with a close curly brace and separate functions, issues arise concerning variable
scope and lifetime.
contains one or more statements, generally ending
A variable’s scope determines where it can be
with a return statement. referred to in the code. A variable’s lifetime
Additionally, unless the function is defined above determines when it is destroyed.
where it is called, it must be prototyped. A local variable’s scope and lifetime is limited to the
function in which it was declared.
21 22
By contrast, a global variable’s scope and You can pass information to a function by using
lifetime are throughout the entire program. arguments, and pass arguments by value or by
reference.
Finally, a static local variable’s scope is limited
You can also pass a variable argument by value when
to the function in which it was declared like a you don’t intend any change to that variable in the
local variable, but its lifetime lasts throughout called function to affect that variable’s value in the
the entire program like a global variable. calling function.
Conversely, you pass a variable argument by
reference when you intend a change to that variable in
the called function to affect that variable’s value in the
calling function.
23 24
The order and data type of the arguments in While arguments are used to pass values to a called
the function prototype must correspond to the function, a return value can be used to pass a value
from a called function back to the function that
order and data type of the arguments in the called it.
function header. However, while multiple values can be passed to a
Similarly, the order and data type of the function as arguments, multiple values cannot be
arguments in the function call must correspond returned from functions.
to the order and data type of the arguments in So far, the variables we’ve used have only been
able to hold one value at a time.
the function header.
In the next chapter, we’ll discuss a type of variable
that can hold multiple values simultaneously.
25 26