Functions - Trinity College, Dublin · 9 Functions 1 Functions 1E3 Topic 9 9 Functions 2...
Transcript of Functions - Trinity College, Dublin · 9 Functions 1 Functions 1E3 Topic 9 9 Functions 2...
1
9 Functions 1
Functions
1E3 Topic 9
9 Functions 2
Objectives
n This topic should allow students ton Understand the importance of abstraction in
programmingn Recognise when a function would be useful.n Design appropriate functions.n Understand how functions are used in C++.n Write and use functions in C++.
n Read chapter 6 of the textbook now.
2
9 Functions 3
Sample Function “cube”
Function declaration
Function definition
Use of the function
9 Functions 4
See also
n Previous slide came from cube.cpp which shows another example of using the cube fn:n cout << "\n5 cubed is " << cube(5);
n fttocmfn.cpp is your first practical but using a function to do the calculation:
n double ft_to_cm (int ft, int in) { return ((ft*12)+in) * 2.54; }
3
9 Functions 5
Function Declarationn Declare functions before usen Declaration includes all you need to know to use
the functionn double cube (double x);n double VAT_added_price
(double price, double VAT_rate);n double ppsi (double diam, double
price);
n Provide a comment with each function declaration.
9 Functions 6
Function Definitionn Function definition consists of a
declaration and the function bodyn The body
n computes the function value n returns the value
n return (x*x*x); n return (price / area);
n (Note that we are ignoring void functions for now.)
4
9 Functions 7
Using the function
n A function call can appear anywhere an expression of the type returned by the function is expected:n double x = ppsi (10, 15);n cout << ppsi (8, 7.99);
n All our examples so far return doubles, but functions can return ints, chars, strings, booleans.
9 Functions 8
Functions of other types
n int max (int x, int y, int z); n Use: !
n = max (10, p, q); n string letter_grade (int mark);
n Returns D, P1, P2, … F1, F2 … based on the percentage grade.
n Use: !cout << letter_grade (55) << endl;
5
9 Functions 9
Boolean Functionsn Boolean functions are useful for abstracting
complex conditions.n bool even (int n);
n returns true if n is an even numbern Use:
bool ok = even (n/2); if (even (number)) . . .
n Defn body: { return (n%2 == 0) };
9 Functions 10
Another Boolean Functionn Declaration:
n bool is_a_factor_of (int n, int f);
n Use:n if (is_a_factor_of (number, i))
composite = true; n Definition:
n bool is_a_factor_of (int n, int f) {return ((n % f) == 0) };
n Useful in testing for primes or perfect numbers
6
9 Functions 11
Parameters and argumentsdouble ppsi (double d, double p) {... area = d/2 * d/2 * PI;
return (p / area);}n d and p are formal parameters of the
functionn In the call !x = ppsi (diameter1, price1)
n the values of diameter1 and price1 are the arguments passed to the function.
9 Functions 12
Parameter-passingn Suppose diameter1 is 10 and price1 is 15.n 10 is passed as the first argument, and is the
value assigned to the formal parameter d.n 15 is passed as the second argument, and is the
value assigned to p.n It’s as though the call is!
ppsi (10, 15)n Inside the body of the function d and p are like
variables whose initial values are 10 and 15 respectively. (see diagrams in lecture).
7
13
Zero Parameters (advanced)n So far we’ve seen functions that take 1 or more
parameters:n bool even (int n); n double ppsi (double diameter, double price);n int max (int x, int y, int z);
n What would it mean to have a function with no parameters?n int f (); n double g ();
n (Once we introduce void functions there will be more reasons to have zero parameters!)
9 Functions 14
In Class Programming
n Let’s write a payroll program (details on web)n Use a top-down design approach.n i.e. write the overall algorithm firstn Worry about the details of individual tasks
later.n These individual tasks can be handled by
functions.
8
9 Functions 15
Algorithm for payroll
n For each record in the filen Read the detailsn Compute the gross pay
n Based on hours and hourly raten Compute the tax due
n Based on gross pay and tax_creditn Compute the net payn Output the computed details
9 Functions 16
When to use a function
n When the task is easy to identify but hard to implement or hard to understand the code.
n Whenever you are about to write the same or very similar code a second timen E.g. ppsi formula in our better-value pizza
programn See pizzafn.cpp
9
9 Functions 17
n ADVANCED EXERCISE
n What does this program print?
n What does the function f compute?