Programming
Fundamentals
Lecture 06a
Parameter Types
• Parameters provide a communication link between the calling function (such as
main) and the called function.
• They enable functions to manipulate different data each time they are called.
• In general, there are two types of formal parameters: value parameters and
reference parameters.
• Value parameter: A formal parameter that receives a copy of the content of
the corresponding actual parameter.
• Reference parameter: A formal parameter that receives the location (memory
address) of the corresponding actual parameter.
• When the ampersand sign (&) is attached after the datatype in the formal
parameter list of a function, the variable following that datatype becomes a
reference parameter.
Parameter Types [example]
• Consider the following function definition:
void areaAndPerimeter(double length, double width,
double& area, double& perimeter)
{
area = length * width;
perimeter = 2 * (length + width);
}
• The function areaAndPerimeter has four parameters: length and width are value
parameters of type double; and area and perimeter are reference parameters of type
double.
Returning multiple Parameter
• In a typical function invocation, the called function receives values from its
calling function, stores and manipulates the passed values and directly returns
at most one value.
• When data is passed in this manner, it’s referred to as a pass by value.
• Calling a function and passing arguments by value is a distinct advantage of C+
+.
• It allows functions to be written as independent entities that can use any
variable or parameter name without concern that other functions might be
using the same name.
• Value of the parameters can be changed in function body of the called
function, but its effect can't be felt in calling function.
Returning multiple Values
• At times, however, you need to modify this approach by giving a called function
direct access to its calling function’s variables.
• This approach allows one function—the called function—to use and change
the value of variables that have been defined in the calling function.
• Doing this requires passing the variable’s address to the called function.
• After the called function has the variable’s address, it can access and change
the value stored there.
• Passing addresses is referred to as a function pass by reference because the
called function can reference, or access, the variable whose address has been
passed.
• C++ provides two types of address parameters: references and pointers.
Example
#include <iostream>
using namespace std;
void newval(double&, double&); // prototype with two
reference parameters
int main()
{ void newval(double&
double firstnum, secnum; xnum, double& ynum)
cout << "Enter two numbers: "; {
cin >> firstnum >> secnum; cout << "The value in
cout << "\nThe value in firstnum is: " << firstnum << endl; xnum is: " << xnum
cout << "The value in secnum is: " << secnum << "\n\n"; << endl;
newval(firstnum, secnum); // call the function cout << "The value in
cout << "The value in firstnum is now: " << firstnum << endl;
ynum is: " << ynum <<
cout << "The value in secnum is now: " << secnum << endl;
return 0; "\n\n";
} xnum = 89.5;
ynum = 99.5;
return;
Pass by reference
• In calling the newval() function in example, you need to understand the
connection between the arguments, firstnum and secnum, used in the function
call and the parameters, xnum and ynum, used in the function header.
• Both refer to the same data items.
• The significance is that the values in the arguments (firstnum and secnum) can
now be altered from within newval() by using the parameter names (xnum and
ynum).
• Therefore, the parameters xnum and ynum don’t store copies of the values in
firstnum and secnum; instead, they access the locations in memory set aside
for these two arguments.
Example 2 void calc(double num1, double
#include <iostream> num2, double num3, double&
using namespace std; total, double& product)
void calc(double, double, double, double&, double&); // {
prototype total = num1 + num2 + num3;
int main() product = num1 * num2 *
{ num3;
double firstnum, secnum, thirdnum, sum, product; return;
cout << "Enter three numbers: "; }
cin >> firstnum >> secnum >> thirdnum;
calc(firstnum, secnum, thirdnum, sum, product); //
function call
cout << "\nThe sum of the numbers is: " << sum << endl;
cout << "The product of the numbers is: " << product <<
endl;
return 0;
}
Example 3 // intfrac()
#include <iostream> // finds integer and fractional parts of
using namespace std; real number
int main() void intfrac(float n, float& intp, float&
{ fracp)
void intfrac(float, float&, float&); //declaration {
float number, intpart, fracpart; //float variables long temp=
do { static_cast<long>(n); //convert to
cout << “\nEnter a real number: “; //number from user long,
cin >> number;
intfrac(number, intpart, fracpart); //find int and frac intp = static_cast<float>(temp);
cout << “Integer part is “ << intpart //print them //back to float
<< “, fraction part is “ << fracpart << endl; fracp = n - intp; //subtract integer
} while( number != 0.0 ); //exit loop on 0.0 part
return 0; }
}
//--------------------------------------------------------------
Reference Parameters
Reference parameters are useful in three situations:
When the value of the actual parameter needs to be changed.
When you want to return more than one value from a function
(recall that the return statement can return only one value)
When passing the address would save memory space and time
relative to copying a large amount of data
Variable Scope
• A variable declared inside the function is local to the function only and have block
scope.
• Scope is the section of the program where the identifier (variable name), such as a
variable, is valid or “known.”
• This section of the program is also referred to as where the variable is “visible”.
• A variable can have a local scope or a global scope.
• A variable with a local scope is simply one with storage locations set aside for it by a
declaration statement in a function body.
• Local variables are meaningful only when used in expressions or statements inside
the function that declared them.
• This means the same variable name can be declared and used in more than one
function.
• For each function that declares the variable, a separate and distinct variable is
created.
Variable Scope
• A variable with global scope, more commonly termed a global variable, has
storage created for it by a declaration statement located outside any function.
• These variables can be used by all functions that are placed after the global
variable declaration.
• Following example shows using a global variable, and the same variable name
has been used on purpose inside both functions in the program.
Example 4 void valfun() // no values are passed to
#include <iostream> this function
using namespace std; {
int firstnum; // create a global variable named firstnum
void valfun(); // function prototype (declaration) int secnum; // create a second local
int main() variable named secnum
{ secnum = 30; // affects only this local
int secnum; // create a local variable named secnum variable's value
firstnum = 10; // store a value in the global variable
secnum = 20; // store a value in the local variable cout << "\nFrom valfun(): firstnum = "
cout << "From main(): firstnum = " << firstnum << endl; << firstnum << endl;
cout << "From main(): secnum = " << secnum << endl; cout << "From valfun(): secnum = " <<
valfun(); // call the function valfun
secnum << endl;
cout << "\nFrom main() again: firstnum = " << firstnum
<< endl; firstnum = 40; // changes firstnum for
cout << "From main() again: secnum = " << secnum << both functions
endl; return;
return 0;
}
}
The three
storage areas
reserved by
Program
Scope resolution operator
• When a local variable has the same name as a global variable, all references to the variable
name made within the local variable’s scope refer to the local variable.
Practice Problem
Define a C++ function swap and call it from main program. Take two integers
from user in main program and swap (smaller first) it using Swap function.
Display the output in main program.