Академический Документы
Профессиональный Документы
Культура Документы
Example 2, cont.
How might we call our function from a main() function?
#include <iostream>
long factorial(int); // declaration
Void main()
{
int x;
cout << “Please enter a number> “ << endl;
cin >> x;
cout << x << “! is “ << factorial(x) << endl;
}
Example 2, cont.
The definition contains the actual code for the function.
void main ()
{ printmessage (5); }
Function Definitions with Multiple Parameters
A function is a group of statements that is executed when it is called
from some point of the program.
---- ;
Var = funcName (arguments); //function call
---;
#include <iostream>
using namespace std;
The result is 8
The second result is 8
The third result is 8
The fourth result is 12
Monday, March 30, 20 12
20
Reference Parameters “Argument Passing”
There are two ways to pass arguments to functions in C++:
Pass by VALUE
Pass by REFERENCE
Pass by VALUE
The value of a variable is passed along to the function
The function creates copies of the argument passed to it.
If the function modifies that value, the modifications stay within the
scope of that function and do not affect the original variable’s value in
memory.
Pass by REFERENCE
A reference to the variable is passed along to the function.
The called function access the caller’s data directly.
If the function modifies that value, the modifications appear also
within the scope of the calling function, i.e. The function can change
the original variable’s value in memory.
Monday, March 30, 20 13
20
Argument Passing by Value
Passing Constants
int z;
z = addition (5,3);
Passing Variables
int x=5, y=3, z;
z= addition (x,y);
void main( )
{ Call by Value
int n=5;
cout<< n<<endl;
cubev( n);
cout<< n<<endl;
}
n 5
5 x
n 5
125 x
void main( )
{ Call by Value
int n=5;
cout<< n<<endl; 5
cubev( n); 5
cout<< n<<endl;
}
void main( ){ 5
int n=5; 125
cout<< n<<endl;
cuber(n );
cout<< n<<endl;
}
n 5 x
n 125 x
num2 12 b
Main()
order
num1 12 a
num2 21 b
Pass by Reference
(with and without the const keyword)
24 Monday, March 30, 2020
Example
void aFunc(int& a, const int& b); //declaration
void main()
{
int alpha = 7;
int beta = 11;
aFunc(alpha, beta); }
//--------------------------------------------------------------
void aFunc(int& a, const int& b) //definition
{
a = 107; //OK
b = 111; //error: can’t modify constant argument
}
Example
//Program to demonstrate call-by-reference parameters.
#include <iostream>
using namespace std;
void get_numbers(int& input1, int& input2); //Reads two integers.
void swap_values(int& variable1, int& variable2);
//Interchanges the values of variables
void show_results(int output1, int output2);
//Shows the values of variables in order.
int main( )
{ int first_num, second_num;
get_numbers(first_num, second_num);
swap_values(first_num, second_num);
show_results(first_num, second_num);
return 0; }
Example cont….
void get_numbers(int& input1, int& input2)
{ cout << "Enter two integers: ";
cin >> input1 >> input2; }
get_numbers(first_num, second_num);
When the function call is executed, the function is not given the
argument names first_num and second_num.
Instead, it is given a list of the memory locations associated with each
name. (ex. 1010, 1012)
It is these memory locations that are associated with the formal
parameters.
The first memory location is associated with the first formal
parameter, the second memory location is associated with the second
formal parameter, and so forth.
Behavior of Call-by-Reference
Arguments
void get_numbers(int& <the variable at memory location
1010>, int& <the variable at memory location 1012>)
{ cout << "Enter two integers: ";
cin >> <the variable at memory location 1010>
>> <the variable at memory location 1012>; }
The swap_values Function
swap_values(first_num, second_num);
the definition of the function swap_values uses
a local variable called temp. This local variable
is needed.
You might be tempted to think the function
definition could be simplified to the following:
The swap_values Function
The variables first_num and second_num are
substituted for the formal parameters variable1
and variable2 so that, with this incorrect
function definition, the function call is
equivalent to the following:
first_num = second_num;
second_num = first_num;
Tips
Call-by-reference parameters are not restricted to
void functions.
It is perfectly legitimate to mix call-by-value and call-
by-reference formal parameters in the same function.
For example,