comp 111 – programming modularity using functions · comp 111 – programming i modularity using...

31
COMP 111 – PROGRAMMING I MODULARITY USING FUNCTIONS Instructor: Dr Dionysiou

Upload: others

Post on 13-Mar-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: COMP 111 – PROGRAMMING MODULARITY USING FUNCTIONS · COMP 111 – PROGRAMMING I MODULARITY USING FUNCTIONS Instructor: Dr Dionysiou . ADMINISTRATIVE 2 ! This week’s lecture

COMP 111 – PROGRAMMING I MODULARITY USING FUNCTIONS

Instructor: Dr Dionysiou

Page 2: COMP 111 – PROGRAMMING MODULARITY USING FUNCTIONS · COMP 111 – PROGRAMMING I MODULARITY USING FUNCTIONS Instructor: Dr Dionysiou . ADMINISTRATIVE 2 ! This week’s lecture

ADMINISTRATIVE

2

¢ This week’s lecture   [BRON06] Chapter 6 (6.1)

¢  What is a function? ¢  Function declaration (prototype) ¢  Function definition (implementation) ¢  Function parameters

Copyrigh

t © D

ionysiou

2010

Page 3: COMP 111 – PROGRAMMING MODULARITY USING FUNCTIONS · COMP 111 – PROGRAMMING I MODULARITY USING FUNCTIONS Instructor: Dr Dionysiou . ADMINISTRATIVE 2 ! This week’s lecture

LECTURE OUTLINE

3

¢  Introduction to functions   Modular programming   Functions with no parameters

¢ Function Basics   Function declaration (prototype)   Function definition (implementation)   Function call

¢ Functions with parameters

Copyrigh

t © D

ionysiou

2010

Page 4: COMP 111 – PROGRAMMING MODULARITY USING FUNCTIONS · COMP 111 – PROGRAMMING I MODULARITY USING FUNCTIONS Instructor: Dr Dionysiou . ADMINISTRATIVE 2 ! This week’s lecture

WHAT IS A FUNCTION?

4

¢ We’ve seen functions before!   Math functions

Copyrigh

t © D

ionysiou

2010

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);

Page 5: COMP 111 – PROGRAMMING MODULARITY USING FUNCTIONS · COMP 111 – PROGRAMMING I MODULARITY USING FUNCTIONS Instructor: Dr Dionysiou . ADMINISTRATIVE 2 ! This week’s lecture

WHAT IS A FUNCTION?

5

¢ 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

Copyrigh

t © D

ionysiou

2010

Page 6: COMP 111 – PROGRAMMING MODULARITY USING FUNCTIONS · COMP 111 – PROGRAMMING I MODULARITY USING FUNCTIONS Instructor: Dr Dionysiou . ADMINISTRATIVE 2 ! This week’s lecture

WHAT IS A FUNCTION?

6

Copyrigh

t © D

ionysiou

2010

Page 7: COMP 111 – PROGRAMMING MODULARITY USING FUNCTIONS · COMP 111 – PROGRAMMING I MODULARITY USING FUNCTIONS Instructor: Dr Dionysiou . ADMINISTRATIVE 2 ! This week’s lecture

WHAT IS A FUNCTION?

7

Copyrigh

t © D

ionysiou

2010

Execution starts here

Page 8: COMP 111 – PROGRAMMING MODULARITY USING FUNCTIONS · COMP 111 – PROGRAMMING I MODULARITY USING FUNCTIONS Instructor: Dr Dionysiou . ADMINISTRATIVE 2 ! This week’s lecture

WHAT IS A FUNCTION?

8

Copyrigh

t © D

ionysiou

2010

Called function

Calling function

Page 9: COMP 111 – PROGRAMMING MODULARITY USING FUNCTIONS · COMP 111 – PROGRAMMING I MODULARITY USING FUNCTIONS Instructor: Dr Dionysiou . ADMINISTRATIVE 2 ! This week’s lecture

WHAT IS A FUNCTION?

9

Copyrigh

t © D

ionysiou

2010

Page 10: COMP 111 – PROGRAMMING MODULARITY USING FUNCTIONS · COMP 111 – PROGRAMMING I MODULARITY USING FUNCTIONS Instructor: Dr Dionysiou . ADMINISTRATIVE 2 ! This week’s lecture

WHAT IS A FUNCTION?

10

Copyrigh

t © D

ionysiou

2010

Function declaration (function prototype)

Function definition (function implementation)

Function call

A function cannot be called unless it is defined!!! A function is not executed unless it is called!!!

Page 11: COMP 111 – PROGRAMMING MODULARITY USING FUNCTIONS · COMP 111 – PROGRAMMING I MODULARITY USING FUNCTIONS Instructor: Dr Dionysiou . ADMINISTRATIVE 2 ! This week’s lecture

CREATE THESE PATTERNS

11

Copyrigh

t © D

ionysiou

2010

Page 12: COMP 111 – PROGRAMMING MODULARITY USING FUNCTIONS · COMP 111 – PROGRAMMING I MODULARITY USING FUNCTIONS Instructor: Dr Dionysiou . ADMINISTRATIVE 2 ! This week’s lecture

FUNCTIONS CALLING FUNCTIONS

12

Copyrigh

t © D

ionysiou

2010

main() house()

triangle()

rectangle()

Calling function? Called function?

Page 13: COMP 111 – PROGRAMMING MODULARITY USING FUNCTIONS · COMP 111 – PROGRAMMING I MODULARITY USING FUNCTIONS Instructor: Dr Dionysiou . ADMINISTRATIVE 2 ! This week’s lecture

CALCULATOR EXERCISE – PART 1

13

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:

Copyrigh

t © D

ionysiou

2010

Page 14: COMP 111 – PROGRAMMING MODULARITY USING FUNCTIONS · COMP 111 – PROGRAMMING I MODULARITY USING FUNCTIONS Instructor: Dr Dionysiou . ADMINISTRATIVE 2 ! This week’s lecture

CALCULATOR EXERCISE – PART 1

14

Copyrigh

t © D

ionysiou

2010

Page 15: COMP 111 – PROGRAMMING MODULARITY USING FUNCTIONS · COMP 111 – PROGRAMMING I MODULARITY USING FUNCTIONS Instructor: Dr Dionysiou . ADMINISTRATIVE 2 ! This week’s lecture

LECTURE OUTLINE

15

¢  Introduction to functions   Modular programming   Functions with no parameters

¢ Function Basics   Function declaration (prototype)   Function definition (implementation)   Function call

¢ Functions with parameters

Copyrigh

t © D

ionysiou

2010

Page 16: COMP 111 – PROGRAMMING MODULARITY USING FUNCTIONS · COMP 111 – PROGRAMMING I MODULARITY USING FUNCTIONS Instructor: Dr Dionysiou . ADMINISTRATIVE 2 ! This week’s lecture

FUNCTION BASICS

16

¢ There are 3 steps you need to follow:   Declare your function

¢  Function prototype

  Define your function ¢  Function implementation

  Call your function ¢  Function call

Copyrigh

t © D

ionysiou

2010

Page 17: COMP 111 – PROGRAMMING MODULARITY USING FUNCTIONS · COMP 111 – PROGRAMMING I MODULARITY USING FUNCTIONS Instructor: Dr Dionysiou . ADMINISTRATIVE 2 ! This week’s lecture

FUNCTION PROTOTYPE

17

¢ 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)

Copyrigh

t © D

ionysiou

2010

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);

Page 18: COMP 111 – PROGRAMMING MODULARITY USING FUNCTIONS · COMP 111 – PROGRAMMING I MODULARITY USING FUNCTIONS Instructor: Dr Dionysiou . ADMINISTRATIVE 2 ! This week’s lecture

FUNCTION DEFINITION

18

¢ 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!!

Copyrigh

t © D

ionysiou

2010

Page 19: COMP 111 – PROGRAMMING MODULARITY USING FUNCTIONS · COMP 111 – PROGRAMMING I MODULARITY USING FUNCTIONS Instructor: Dr Dionysiou . ADMINISTRATIVE 2 ! This week’s lecture

FUNCTION DEFINITION

19

Copyrigh

t © D

ionysiou

2010

findmax prototype returns void accepts 2 double arguments

findmax definition header returns void accepts 2 double arguments x and y

Page 20: COMP 111 – PROGRAMMING MODULARITY USING FUNCTIONS · COMP 111 – PROGRAMMING I MODULARITY USING FUNCTIONS Instructor: Dr Dionysiou . ADMINISTRATIVE 2 ! This week’s lecture

FUNCTION CALL

20

¢ 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

Copyrigh

t © D

ionysiou

2010

Page 21: COMP 111 – PROGRAMMING MODULARITY USING FUNCTIONS · COMP 111 – PROGRAMMING I MODULARITY USING FUNCTIONS Instructor: Dr Dionysiou . ADMINISTRATIVE 2 ! This week’s lecture

FUNCTION DEFINITION

21

Copyrigh

t © D

ionysiou

2010

findmax call passes actual parameters 3.5 and 5.5 to the function

Page 22: COMP 111 – PROGRAMMING MODULARITY USING FUNCTIONS · COMP 111 – PROGRAMMING I MODULARITY USING FUNCTIONS Instructor: Dr Dionysiou . ADMINISTRATIVE 2 ! This week’s lecture

FORMAL AND ACTUAL PARAMETERS

22

Copyrigh

t © D

ionysiou

2010

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

Page 23: COMP 111 – PROGRAMMING MODULARITY USING FUNCTIONS · COMP 111 – PROGRAMMING I MODULARITY USING FUNCTIONS Instructor: Dr Dionysiou . ADMINISTRATIVE 2 ! This week’s lecture

FORMAL AND ACTUAL PARAMETERS

23

Copyrigh

t © D

ionysiou

2010

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

Page 24: COMP 111 – PROGRAMMING MODULARITY USING FUNCTIONS · COMP 111 – PROGRAMMING I MODULARITY USING FUNCTIONS Instructor: Dr Dionysiou . ADMINISTRATIVE 2 ! This week’s lecture

FUNCTION POWER!

24

¢ 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!

Copyrigh

t © D

ionysiou

2010

Page 25: COMP 111 – PROGRAMMING MODULARITY USING FUNCTIONS · COMP 111 – PROGRAMMING I MODULARITY USING FUNCTIONS Instructor: Dr Dionysiou . ADMINISTRATIVE 2 ! This week’s lecture

LECTURE OUTLINE

25

¢  Introduction to functions   Modular programming   Functions with no parameters

¢ Function Basics   Function declaration (prototype)   Function definition (implementation)   Function call

¢ Functions with parameters

Copyrigh

t © D

ionysiou

2010

Page 26: COMP 111 – PROGRAMMING MODULARITY USING FUNCTIONS · COMP 111 – PROGRAMMING I MODULARITY USING FUNCTIONS Instructor: Dr Dionysiou . ADMINISTRATIVE 2 ! This week’s lecture

FUNCTIONS WITH PARAMETERS

26

Copyrigh

t © D

ionysiou

2010

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

Page 27: COMP 111 – PROGRAMMING MODULARITY USING FUNCTIONS · COMP 111 – PROGRAMMING I MODULARITY USING FUNCTIONS Instructor: Dr Dionysiou . ADMINISTRATIVE 2 ! This week’s lecture

FUNCTIONS WITH PARAMETERS

27

Copyrigh

t © D

ionysiou

2010

Page 28: COMP 111 – PROGRAMMING MODULARITY USING FUNCTIONS · COMP 111 – PROGRAMMING I MODULARITY USING FUNCTIONS Instructor: Dr Dionysiou . ADMINISTRATIVE 2 ! This week’s lecture

IN-CLASS EXERCISES

28

¢ 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.

Copyrigh

t © D

ionysiou

2010

Page 29: COMP 111 – PROGRAMMING MODULARITY USING FUNCTIONS · COMP 111 – PROGRAMMING I MODULARITY USING FUNCTIONS Instructor: Dr Dionysiou . ADMINISTRATIVE 2 ! This week’s lecture

FUNCTIONS WITH PARAMETERS VS. WITHOUT PARAMETERS

29

¢ 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.

Copyrigh

t © D

ionysiou

2010

Page 30: COMP 111 – PROGRAMMING MODULARITY USING FUNCTIONS · COMP 111 – PROGRAMMING I MODULARITY USING FUNCTIONS Instructor: Dr Dionysiou . ADMINISTRATIVE 2 ! This week’s lecture

IN-CLASS EXERCISE - ANSWER

30

Copyrigh

t © D

ionysiou

2010

Page 31: COMP 111 – PROGRAMMING MODULARITY USING FUNCTIONS · COMP 111 – PROGRAMMING I MODULARITY USING FUNCTIONS Instructor: Dr Dionysiou . ADMINISTRATIVE 2 ! This week’s lecture

IN-CLASS EXERCISE - ANSWER

31

Copyrigh

t © D

ionysiou

2010