COMP 111 PROGRAMMING I MODULARITY USING FUNCTIONS Instructor: Dr Dionysiou
ADMINISTRATIVE This week s lecture [BRON06] Chapter 6 (6.1) What is a function? Function declaration (prototype) Function definition (implementation) Function parameters 2
LECTURE OUTLINE Introduction to functions Modular programming Functions with no parameters Function Basics Function declaration (prototype) Function definition (implementation) Function call Functions with parameters 3
WHAT IS A FUNCTION? We ve seen functions before! Math functions Returned value We want to calculate the square root of number 123 1. find math function that performs the calculation double sqrt(double); 2. call the function and store the returned value result = sqrt(123); 4
WHAT IS A FUNCTION? Our programs so far used only one driver function (main) All input, calculations, output operations are performed in this one module Our programs from now on will be modular Self-contained modules (functions) will be responsible for particular tasks Input data Perform calculation Output results Modules are identified by a name are executed when they are called main() is a function that your program will always have! Program execution starts there 5
WHAT IS A FUNCTION? 6
WHAT IS A FUNCTION? Execution starts here 7
WHAT IS A FUNCTION? Calling function Called function 8
WHAT IS A FUNCTION? 9
WHAT IS A FUNCTION? Function declaration (function prototype) A function cannot be called unless it is defined!!! A function is not executed unless it is called!!! Function call Function definition (function implementation) 10
CREATE THESE PATTERNS 11
FUNCTIONS CALLING FUNCTIONS main() house() triangle() rectangle() Calling function? Called function? 12
CALCULATOR EXERCISE PART 1 Write a void function called displaymenu that displays the following menu options. Call this function in your main program. Welcome to my calculator! 1 : Add numbers 2 : Subtract numbers 3 : Multiply numbers 4 : Divide numbers 5 : Exit Enter choice: 13
CALCULATOR EXERCISE PART 1 14
LECTURE OUTLINE Introduction to functions Modular programming Functions with no parameters Function Basics Function declaration (prototype) Function definition (implementation) Function call Functions with parameters 15
FUNCTION BASICS There are 3 steps you need to follow: Declare your function Function prototype Define your function Function implementation Call your function Function call 16
FUNCTION PROTOTYPE Before a function is called, it must be declared Function prototype (or signature) return data type (if any, otherwise void) order and data types of parameters (if any, otherwise void) returndatatype functionname(list of parameter data types) void square(void); void findmax(double, double); char grade(double); double pi(void); double swap(int, char, int); 17
FUNCTION DEFINITION Actual implementation of the function Only defined once in a program Can be used by any other function that calls it Consists of two parts Header line Return data type of the function (could be void) Function name Data type of formal parameters in order (could be void) Function body C++ statements Function prototype and header line data types MUST match!! 18
FUNCTION DEFINITION findmax prototype returns void accepts 2 double arguments findmax definition header returns void accepts 2 double arguments x and y 19
FUNCTION CALL Use the name of the function and actual parameters (if any) Parameters must be listed in the same order and type as declared in the function prototype After the values are passed, control is transferred to the called function After the function terminates, control is returned back to the calling function 20
FUNCTION DEFINITION findmax call passes actual parameters 3.5 and 5.5 to the function 21
FORMAL AND ACTUAL PARAMETERS Actual parameters Formal parameters x has value 5 in the function show y has value 7 in the function show ch has value a in the function show 22
FORMAL AND ACTUAL PARAMETERS Actual parameters Formal parameters How to change the function call so that: x has value 15 in the function show y has value -7 in the function show ch has value d in the function show 23
FUNCTION POWER! Power of a function is its ability to work with parameters! Same function can be used on different arguments of the same type! Functions with parameters! 24
LECTURE OUTLINE Introduction to functions Modular programming Functions with no parameters Function Basics Function declaration (prototype) Function definition (implementation) Function call Functions with parameters 25
FUNCTIONS WITH PARAMETERS What is the flow of execution in this program? Rewrite this program to do the following: 1. Accept grade 2. Call printgrade to output the corresponding letter grade 1. Do a better validation on score 3. Repeat as long as the grade is between 0 and 100 26
FUNCTIONS WITH PARAMETERS 27
IN-CLASS EXERCISES Write a void function to display a line of $, where the number of $ is passed as an argument to this function. Test your program from main. Write a void function to display a square of $, where the side of the square is passed as an argument to this function. Test your program from main. Write a void function to display a line of a character, where the character is passed as an argument to this function. Test your program from main. Write a void function to display a square of a character, where the character and the square side are passed as arguments to this function. Test your program from main. 28
FUNCTIONS WITH PARAMETERS VS. WITHOUT PARAMETERS Write a void function called displaynumbers that will prompt the user to enter 2 integer numbers m and n, and will display all the numbers from m to n. i.e. if m=4 and n=9, the function will display 4 5 6 7 8 9 Test your function by calling it from your main program. Write a void function called displaynumbers that has two integer arguments m and n, and will display all the numbers from m to n. 29
IN-CLASS EXERCISE - ANSWER 30
IN-CLASS EXERCISE - ANSWER 31