Functions 1E3 Topic 9 9 Functions 1 Objectives n This topic should allow students to n Understand the importance of abstraction in programming n 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. 9 Functions 2 1
Writing/Using Functions n The 3 & 3 approach to writing and using functions n The function itself 1. Name: come up with a meaningful/descriptive name for the procedure. 2. Parameters: what the function needs in order to carry out its job 3. Return: what kind of data the function gives back (if any). 9 Functions 3 Writing/Using Functions n The 3 & 3 approach to writing and using functions n The function itself 1. Name: come up with a meaningful/descriptive name for the procedure. 2. Parameters: what the function needs in order to carry out its job 3. Return: what kind of data the function gives back (if any). n Appearing in the Programme 1. Declaring the function: function prototype (what it looks like). 2. Calling the function: using it from inside any other function. 3. Implementing the function body: writing its code (what it does). 9 Functions 4 2
n Example 1: a function that prints out a fixed menu: n The 3: 1. Name: displaymenu 2. Parameters: none therefore, leave the brackets empty 3. Returns: nothing therefore, write void. void displaymenu (); 5 n Example 1: a function that prints out a fixed menu: n The 3: 1. Name: displaymenu 2. Parameters: none therefore, leave the brackets empty 3. Returns: nothing therefore, write void. void displaymenu (); n And 3: 1. Declaration: void displaymenu (); // written before the main(). 2. Call: displaymenu(); //written anywhere inside the main(). 3. Implementation, written after the main() void displaymenu () { cout << Option 1: << endl; cout<< Option 2:. <<endl;.. } 6 3
n Example 2: a function that adds two given numbers and returns the result: int add (int x, int y); 7 n Example 2: a function that adds two given numbers and returns the result: int add (int x, int y); int main () { int a=5, b=2; int sum; //now let s call the function sum = add(a, b); //right-hand side is evaluated and result is stored in left-hand side cout << The sum is: << sum; } 8 4
n Example 2: a function that adds two given numbers and returns the result: int add (int x, int y); int main () { int a=5, b=2; int sum; //now let s call the function sum = add(a, b); //right-hand side is evaluated and result is stored in left-hand side cout << The sum is: << sum; } int add(int x, int y) //you can give any names to the variables. { int total; //note that we did not need to re-declare variables x and y. total = x + y; return total; } 9 Parameters & Return n So far we ve seen two examples: n A function that does not take parameters or return a value; it just performs a certain task straightaway (in C it is called a function, although mathematically speaking it s not really a function ). n A function that takes parameters and does some computation then returns the result. 10 5
Parameters & Return n So far we ve seen two examples: n A function that does not take parameters or return a value; it just performs a certain task straightaway (in C it is called a function, although mathematically speaking it s not really a function ). n A function that takes parameters and does some computation then returns the result. n Q: Can there be a function that takes parameters but does not return a value? Or a function that does not take a parameter but returns a value? 11 Parameters & Return n So far we ve seen two examples: n A function that does not take parameters or return a value; it just performs a certain task straightaway (in C it is called a function, although mathematically speaking it s not really a function ). n A function that takes parameters and does some computation then returns the result. n Q: Can there be a function that takes parameters but does not return a value? Or a function that does not take a parameter but returns a value? n Yes. n Think about it and raise your hand if you have an example. 12 6
Boolean Functions" (functions that return a Boolean value) n Boolean functions are useful for abstracting complex conditions. n bool even (int n); n returns true if n is an even number n Use: " " "bool ok = even (n/2); " if (even (number))... n Defn body: { return (n%2 == 0) };" 9 Functions 13 Another Boolean Function n 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 9 Functions 14 7
Parameters and arguments double ppsi (double d, double p) {... area = d/2 * d/2 * PI;" " "return (p / area);} n d and p are formal parameters of the function n In the call " x = ppsi (diameter1, price1) n the values of diameter1 and price1 are the arguments passed to the function. 9 Functions 15 Parameter-passing n 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. 9 Functions 16 8
Recap 9 Functions 17 Example n Let s write a payroll program (details on web) n Use a top-down design approach. n i.e. write the overall algorithm first n Worry about the details of individual tasks later. n These individual tasks can be handled by functions. 9 Functions 18 9
Algorithm for payroll n For each record in the file n Read the details n Compute the gross pay n Based on hours and hourly rate n Compute the tax due n Based on gross pay and tax_credit n Compute the net pay n Output the computed details 9 Functions 19 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 time n E.g. ppsi formula in our better-value pizza program n See pizzafn.cpp 9 Functions 20 10
n ADVANCED EXERCISE n What does this program print? n What does the function f compute? 9 Functions 21 11