Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Objectives

More on Functions:  This topic should allow students to


 Recognise the need for and write functions
Reference Parameters that take by-reference parameters.
Procedures  Write functions that do not return a value
(aka void functions, or procedures)
1E3  Choose between functions and procedures.
Topic 10  Read Chapter 7 of the textbook.

10 More on Functions 1 10 More on Functions 2

A void function in payroll


void functions
program
 Our functions till now returned a single  In the current draft of the “functions”
value. version of the payroll program (see next
 Some sub-tasks don’t return anything slide) there is the line
 E.g. the task of printing output  write_pay_details (employee_ID,
gross_pay, tax_due, net_pay)
 These are implemented as void functions
in C++  write_pay_details () is being called for its
effect, not to return a value.
 void print_results (int StID,
double grade);

10 More on Functions 3 10 More on Functions 4

1
Body of payrollFNS.cpp Functions and Procedures
 Note that in computer science in general
 “function” means a sub-task that returns a single
value
 reflecting the mathematical meaning of function
 “procedure” is the term used for sub-tasks that
do something rather than returning something.
 C++ used the word function for all sub-tasks.

10 More on Functions 5 10 More on Functions 6

Side-effects Reference parameters


 Proper functions (i.e. non-void ones)  Consider the read_details() function which
should not have any side-effects. we need for payrollFNS.cpp
 Functions should be called for the value  It returns nothing. It prints nothing.
they return only. What’s it supposed to do?
 If a function has side effects  To provide values for the variables
 E.g. printing, reading, updating a value num_hours, hourly_rate, tax_credit in the
main program.
 then it won’t be as broadly useful and
could be confusing

10 More on Functions 7 10 More on Functions 8

2
WRONG definition of
Call-by-value parameters
read_details ()
 void read_details (int  Up to now, our parameters are call-by-value
num_hours, double hourly_rate,  The value of the argument is passed in.
double tax_credit) {  Read_details (num_hours, … ) will pass in 0 or
… whatever num_hours happens to be at that time.
cin >> num_hours;  The parameter variable num_hours inside
… read_details function is initialised to 0, and then cin
gives it another value.
}
 But the main num_hours variable is not affected.
 This won’t change the value of the main  See lecture for details.
program’s variable num_hours.

10 More on Functions 9 10 More on Functions 10

Call-by-reference parameters Call-by-reference syntax


 We need a way to pass the address of a  The function declaration should be
void read_details (int& hrs, ….){
variable to the read_details () function.
 The & says “address of” or “reference to”
 We want the function to be able to access  int& hrs can be read as “hrs is a reference to
and update the place where num_hours is an integer variable”.
stored by the main program.  read_details (num_hours, …); passes a
 So we need to pass in a reference to the reference to the variable num_hours in to the
function,
variable num_hours.
 Inside the function, this parameter is called hrs
 This is call-by-reference.  cin >> hrs; will update the place referenced
by hrs, which in this case is num_hours.
10 More on Functions 11 10 More on Functions 12

3
Which to use Clean living!!
 If you want a function to change the value of a  Functions that return a value should have
variable it must be a call-by-reference parameter no other effect
i.e. use &  Including they should not update reference
 If the function only needs the value of the parameters.
parameter don’t use &  If your function needs to return multiple
 it’s confusing - the reader will expect the function to values use reference parameters for all of
change the value them and make it a void function.
 and dangerous - the function may inadvertently
change the value of a main program variable.
 This is recommended practice, not
absolute.

10 More on Functions 13 10 More on Functions 14

Exercises
 We can now complete the “functions”
version of the payroll program.
 Write a function to swap the values of two
integer variables.
 Write a function to order the values of two
integer variables, so that x <= y.

10 More on Functions 15

You might also like