Engineering Problem Solving
with C++, Etter/Ingber
Chapter 5
Parameter Passing
11/06/13
Engineering Problem Solving with
C++, Second edition, J. Ingber
1
pass by value
pass by reference
storage class and scope
PARAMETER PASSING
Engineering Problem Solving with
C++, Second edition, J. Ingber
2
Parameter Passing
C++ supports two forms of parameter
passing:
– Pass by value.
– Pass by reference.
Engineering Problem Solving with
C++, Second edition, J. Ingber
3
Parameter Passing
C++ supports two forms of parameter
passing:
– Pass by value.
– Pass by reference.
Engineering Problem Solving with
C++, Second edition, J. Ingber
4
Parameter Passing - pass by value
– Pass by value is the default in C++ (except
when passing arrays as arguments to functions).
– The formal parameter receives the value of the
argument.
– Changes to the formal parameter do not affect
the argument.
Engineering Problem Solving with
C++, Second edition, J. Ingber
5
//Function Definition
int fact(int num)//4
int main()
{
{
int nfact = 1;//5
int n, factorial;//1
while(num>1)
cin >> n; //2
{
if(n>=0)
nfact = nfact*num;
{
num--;
factorial =
}
fact(n);//3
return(nfact);//6
cout << n <<"! is "
} //end fact
<< factorial << endl;//7
}//end if
return 0;
}//end main
Engineering Problem Solving with
C++, Second edition, J. Ingber
6
Parameter Passing - pass by reference
Pass by reference allows modification of a
function argument.
Must append an & to the parameter data type in
both the function prototype and function header
void getDate(int& day, int& mo, int& year)
Formal parameter receives the address of the
argument.
Any changes to the formal parameter directly
change the value of the argument.
Engineering Problem Solving with
C++, Second edition, J. Ingber
7
Pass-By-Reference Example
scale.cpp
Example - pass by reference
#include <iostream>
using namespace std;
//Function swap interchanges the values of two variables
//function definition
void swap(double& a, double& b) //function header
{
double temp; //local variable temp
temp = a;
a=b;
b=temp;
return; //optional return statement
}//end swap
Engineering Problem Solving with
C++, Second edition, J. Ingber
9
Another Example - pass by reference
int main()
{
double x = 5, y = 10;
swap(x,y); //function call; x y are arguments
cout >> “x = “ << x << ‘,’ << “ y= “ << y << endl;
return 0;
} //end main
Output?
Engineering Problem Solving with
C++, Second edition, J. Ingber
10
Practice! - What is the output?
#include <iostream>
using namespace std;
void fun(int& a1, int& a2, int a3)
{ a1++;
a2++;
a3--;
}
int main()
{ int c1=1, c2=2, c3=3;
cout << c1 << ‘,’ << c2 << ‘,’ << c3 << endl;
fun(c1,c2,c3);
cout << c1 << ‘,’ << c2 << ‘,’ << c3 << endl;
fun(c3, c2, c1);
cout << c1 << ‘,’ << c2 << ‘,’ << c3 << endl;
return 0;
}
Engineering Problem Solving with
C++, Second edition, J. Ingber
11
Scope
Scope refers to the portion of the program
in which it is valid to reference a function or
a variable.
Block
A sequence of statements enclosed in {} that introduces
a new scope
Engineering Problem Solving with
C++, Second edition, J. Ingber
12
Scope
Local scope - a local variable is defined
within a function or a block and can be
accessed only within the function or block
that defines it
Global scope - a global variable is defined
outside the any function and can be
accessed by any function within the
program file.
13
Scope
Ch5/scope1.cpp - Global PI and local variables
belonging to the functions
Ch5/scope2.cpp - Global variable referred to in main
and other function.
Ch5/scope3.cpp - Local variable declared within an
if block.
14
Questions
Use pass-by-______________ when a change in
the argument is desired.
The_________ of a name is the portion of the
program in which the name can be used.
A variable declared outside of any block is called a
_________ variable.
15
Write a Midpoint Function
Write a C++ function to find the midpoint of a
line segment in a Cartesian plane, given the
coordinates of the endpoints of the segment.
Write normalize( )
Write a C++ function to take the numerator
and denominator of a fraction. It should
normalize the fraction. Change the fraction
so that the denominator is a positive
number.
© Copyright 2026 Paperzz