Lec05-06 (Topic 3 Functions) - v2
Lec05-06 (Topic 3 Functions) - v2
Lec05-06 (Topic 3 Functions) - v2
LECTURE 5
Functions
Overview
5.1 Predefined and Programmer-Defined Functions
5.1.1 Predefined Functions
5.1.2 Functions Declaration
5.1.3 Function Definition
5.2 Local Variables
5.3 Overloading Function Names
5.4 void Functions
5.5 Passing Variables in Function
3
5.1
Predefined and Programmer-Defined Functions
Predefined Functions
• C++ comes with libraries of predefined functions
• Example:
• side = sqrt(area);
#include <cmath>
srand(time(0));
candy_per_person = double(total_candy)/number_of_people;
Function Declaration
• Two components of a function definition
• Function declaration (or function prototype)
• Shows how the function is called
• Must appear in the code before the function can be called
• Syntax:
Type_returned Function_Name(Parameter_List);
//Comment describing what function does
• Function definition
;
• Describes how the function does its task
• Can appear before or after the function is called
• Syntax:
Type_returned Function_Name(Parameter_List)
{
//code to make the function work
}
Function Declaration
• Tells the return type
• Example:
double total_cost(int number, double price);
// Compute total cost including 5% sales tax on
// number items at cost of price each
Function Definition
• Provides the same information as the declaration
•
• Describes how the function does its task
function body
The Return Statement
• Ends the function call
• Syntax:
return expression;
• expression performs the calculation
or
• expression is a variable containing the calculated value
• Example:
return subtotal + subtotal * TAX_RATE;
The Function Call
• Tells the name of the function to use
• Example:
double bill = total_cost(number, price);
Function Call Details
• The values of the arguments are plugged into the formal
parameters (Call-by-value mechanism with call-by-value
parameters)
• The first argument is used for the first formal parameter,
the second argument for the second formal parameter,
and so forth.
• Examples:
• Compiler cannot check that arguments are in the correct logical order
// In Function declaration
char grade(int received_par, int min_score_par);
_____________________________________________________
// In Function Called
int received = 95, min_score = 60;
• Produces a faulty result because the arguments are not in the correct logical
order. The compiler will not catch this!
Function Definition Syntax
• Within a function definition
• Variables must be declared before they are used
is easier to read as
if (appropriate (rate))
• n! = 1 x 2 x 3 x … x n
double d = 11 / 2;
b b2 4ac y 7
x y 2a
x
Knowledge Check
• Can you
5.2
Local Variables
Local Variables
• Variables declared in a function:
• Are local to that function, they cannot be used from
outside the function
• Have the function as their scope
int main()
{...}
5.3
Overloading Function Names
Overloading Function Names
• C++ allows more than one definition for the same function
name
• Very convenient for situations in which the “same”
function is needed for different numbers or types
of arguments
AND / OR
5.4
void Functions
void-Functions
• In top-down design, a subtask might produce
• No value (just input or output for example)
• One value
• More than one value
• Example:
• Example:
show_results(32.5, 0.3);
NOT:
C = (5/9) (F – 32)
5.5
Passing Variables in Functions
Call-by-Reference Parameters
• Call-by-value is not adequate when we need a sub-task to obtain input
values
• Recall that we have changed the values of formal parameters in a function body,
but we have not changed the arguments found in the function call
age 1001 34
initial 1002 A
hours 1003 23.5
1004
• Example:
void good_stuff(int& par1, int par2, double& par3);
LECTURE 6
Functions (Cont.)
Overview
6.1 Functions Calling Functions
6.2 Testing and Debugging
6.3 Arrays in Functions
89
6.1
Functions Calling Functions
Using Procedural Abstraction
• Functions should be designed so they can be used as
black boxes
• Postcondition
• Describes the effect of the function call
• Tells what will be true after the function is executed
(when the precondition holds)
• If the function returns a value, that value is described
• Changes to call-by-reference parameters are described
swap_values revisited
• Using preconditions and postconditions the declaration of
swap_values becomes:
• Minimize time wasted writing code that doesn’t match the task at
hand
Case Study
Supermarket Pricing
• Problem definition
• Determine the retail price of an item given suitable input
• 5% markup if an item should sell in a week
• 10% markup if an item is expected to take more than a
week
• Summary: 5% for up to 7 days, changes to 10% at 8 days
• Input
• The wholesale price and the estimate of days until the item sells
• Output
• The retail price of the item
Supermarket Pricing:
Problem Analysis
• Three main subtasks
• Input the data
• Compute the retail price of the item
• Output the results
int main()
{
double wholesale_cost, retail_price;
int shelf_time;
get_input(wholesale_cost, shelf_time);
return 0;
}
Supermarket Pricing:
Algorithm Design -- price
• Implementations of get_input and give_output are
straightforward, so we concentrate on the price function
6.2
Testing and Debugging
Testing and Debugging Functions
• Each function should be tested as a separate unit
• Write a stub?
121
6.3
Arrays in Functions
Arrays in Functions
• Indexed variables can be arguments to functions
• Variables a[0] through a[9] are of type int, making these calls
legal:
my_function( a[0] );
my_function( a[3] ); It passes one single
integer to the function
my_function( a[i] ); as an argument.
NOT THE ARRAY!
123
Arrays as Function Arguments
• A formal parameter can be for an entire array
• Such a parameter is called an array parameter
• It is not a call-by-value parameter
• It is not a call-by-reference parameter
• Array parameters behave much like call-by-reference parameters
Array Parameter Declaration
• An array parameter is indicated using empty brackets in
the parameter list such as
fill_up(score, 5);
fill_up(time, 10);
132
const Modifier
• Array parameters allow a function to change the values
stored in the array argument
• The compiler will issue an error if you write code that changes the
values stored in the array parameter
Error Message from compiler:
Function Calls and const
• If a function with a constant array parameter calls another
function using the const array parameter as an
argument…
• The compiler will issue an error if a function is called that does not
have a const array parameter to accept the array argument
const Parameters Example
// function declaration
double compute_average(int a[], int size);