Week# 12
Functions
Dr Taimur Ahmed
Department of IT & CS, PAF-IAST
Lecture# 31
passing/returning values in functions
Reference variables
Using Reference Variables as Parameters
❑ A mechanism that allows a function to work with the original argument
from the function call, not a copy of the argument
❑ Allows the function to modify values stored in the calling environment
❑ Provides a way for the function to ‘return’ more than one value
Lecture# 31 - Functions: Arguments/parameters | 4
Passing by Reference
❑ A reference variable is an alias for another variable
❑ Defined with an ampersand (&)
void getDimensions(int&, int&);
❑ Changes to a reference variable are made to the variable it refers to
❑ Use reference variables to implement passing parameters by reference
Lecture# 31 - Functions: Arguments/parameters | 5
Passing by Reference
The & here in the prototype indicates that the
parameter is a reference variable.
Here we are passing value by
reference.
Program Continues...
Lecture# 31 - Functions: Arguments/parameters | 6
Passing by Reference
The & also appears here in the function header.
Lecture# 31 - Functions: Arguments/parameters | 7
Reference Variables
❑ Each reference parameter must contain &
❑ Space between type and & is unimportant
❑ Must use & in both prototype and header
❑ Argument passed to reference parameter must be a variable – cannot
be an expression or constant
❑ Use when appropriate – don’t use when argument should not be
changed by function, or if function needs to return only 1 value
Lecture# 31 - Functions: Arguments/parameters | 8
Overloading Functions
Overloading Functions
❑ Overloaded functions have the same name but different parameter lists
❑ Can be used to create functions that perform the same task but take
different parameter types or different number of parameters
❑ Compiler will determine which version of function to call by argument
and parameter lists
Lecture# 31 - Functions: Arguments/parameters | 10
Overloading Functions
❑ Using these overloaded functions,
void getDimensions(int); // 1
void getDimensions(int, int); // 2
void getDimensions(int, double); // 3
void getDimensions(double, double);// 4
the compiler will use them as follows:
int length, width;
double base, height;
getDimensions(length); // 1
getDimensions(length, width); // 2
getDimensions(length, height); // 3
getDimensions(height, base); // 4
Lecture# 31 - Functions: Arguments/parameters | 11
Overloading Functions
The overloaded
functions have different
parameter lists
Passing a double
Passing an int Program Continues...
Lecture# 31 - Functions: Arguments/parameters | 12
Overloading Functions
Lecture# 31 - Functions: Arguments/parameters | 13
Arrays as Function Arguments
1D Arrays as Function Arguments
❑ To pass an array to a function, just use the array name:
showScores(tests); // function call
❑ To define a function that takes an array parameter, use empty [] for
array argument:
void showScores(int []);
// function prototype
void showScores(int tests[])
// function header
Lecture# 31 - Functions: Arguments/parameters | 15
1D Arrays as Function Arguments
❑ When passing an array to a function, it is common to pass array size so
that function knows how many elements to process:
showScores(tests, ARRAY_SIZE);
❑ Array size must also be reflected in prototype, header:
void showScores(int [], int);
// function prototype
void showScores(int tests[], int size)
// function header
Lecture# 31 - Functions: Arguments/parameters | 16
1D Arrays as Function Arguments - Example
Program Continues...
Lecture# 31 - Functions: Arguments/parameters | 17
1D Arrays as Function Arguments - Example
Lecture# 31 - Functions: Arguments/parameters | 18
Modifying Arrays in Functions
❑ Array names in functions are like reference variables – changes made
to array in a function are reflected in actual array in calling function
❑ Need to exercise caution that array is not inadvertently changed by a
function
Lecture# 31 - Functions: Arguments/parameters | 19
Two-Dimensional Array as Parameter/Argument
❑ Use array name as argument in function call:
getExams(exams, 2); //function call
❑ Use empty [] for row, size declarator for column in prototype, header:
const int COLS = 2;
// Prototype
void getExams(int [][COLS], int);
// Header
void getExams(int exams[][COLS], int rows)
Lecture# 31 - Functions: Arguments/parameters | 20
2D Arrays as Function Arguments - Example
Program Continues...
Lecture# 31 - Functions: Arguments/parameters | 21
2D Arrays as Function Arguments - Example
Lecture# 31 - Functions: Arguments/parameters | 22