Sei sulla pagina 1di 4

Arguments are used to pass values to a called

Chapter 5(c) – Function 


function.
A return value can be used to pass a value from a
- Returning a Value 
called function back to the function that called it.
 For example, in the previous program (Example 8,
from a Function Chapter 5(b)- Function - Sending Information to a Function) the
function addNumbers had three arguments, the first
two being the numbers to be added, the third being
their sum.
 The following program modifies the previous one by
eliminating the third argument, but adding a return
value to the function

1 2

Example 1 Example 1 Sample Output

 The sample input and output may be the


same as in the previous program:

Enter first number: 3


Enter first number: 6
3+6=9

3 4

sum = addNumbers (firstNum, secondNum);

 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

 The body of the called function has the return


keyword followed by a value of the data type
compatible with the function prototype and header,
here int.
 The function’s return value is the value that follows
the return keyword, here the sum of the two
arguments:
return x + y;
 That sum of x + y (in the called function ) then is
assigned to the variable sum in main (the caller
function).

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

Returning more than one value

 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

 Instead, we may do it by calling a function using


reference parameters and pointer variable (as
discussed earlier)
 A function called by reference can return more
than one value
 It cannot be done through calling a function by
value

13 14

Inline Function Example 3

 A function call substantial overhead.


 Extra time & space (resources) have to be used to
invoke the function, pass parameter to it, allocate
storage for its local variable, store the current
variables & the location of execution in the main
program, etc…
 In some cases, it is better to avoid all this by
specifying the function to be inline.
 This is to tell the compiler to replace each call to the
function with explicit code for the function.

15 16
Function Overloading Example 4

 C++ allow you to use the same name for


different function.
 As long as they have different parameter type
lists, they compiler will regard them as
different functions.
 To be distinguished, the parameter lists must
either
 contain a different number of parameters, or
 there must be at least one position in their
parameter lists where the types are different.

17 18

Passing
arguments
to a function
Value Reference parameter Pointer Summary
by

Writing  A function is a group of statements that together


function void passval( int ) ; void passval( int& ) ; void passval( int* ) ; perform a task.
prototype  While no program needs more than a main function,
Calling a as you write more complex and sophisticated
passval( x ); passval( x ); passval( &x ); programs, your code will be easier to write,
function
understand, and fix if you divide the code up among
Writing
void passval( int m ) void passval( int& m) void passval ( int* m ) different functions, each function performing a
{ { { specific task.
function
m = m+5 ; m = m+5 ; *m = *m + 5 ;
definition  You implement a function in addition to main by first
} } }
defining it and then calling it.
Changes made to Changes made to m Changes made to m  A function definition consists of a function header
Changes on m (the parameter) (the parameter) (the parameter) and a function body.
arguments have no effect on have effect on have effect on
x(the argument) x (the argument) x (the argument) 20

 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

Potrebbero piacerti anche