Top Banner
User-Defined Functions II TK1914: C++ Programming
30

User-Defined Functions II TK1914: C++ Programming.

Jan 06, 2018

Download

Documents

Caitlin Lambert

PARAMETER PASSING A function may define parameters which a caller needs to provide when calling that function. The provision of parameters when calling a function is sometimes referred to as parameter passing. We will look at two ways of passing parameters to functions: –Pass by value (value parameters) –Pass by reference (reference parameters) Analogy: Paying for something by cash / direct debit 3FTSM :: TK1914,
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: User-Defined Functions II TK1914: C++ Programming.

User-Defined Functions II

TK1914: C++ Programming

Page 2: User-Defined Functions II TK1914: C++ Programming.

FUNCTIONS

• In this second part of the chapter on User-Defined Functions, you will learn about– Passing parameters– Value and reference parameters– Scope of an identifier

• Local variables• Global variables

2FTSM :: TK1914, 20112012

Page 3: User-Defined Functions II TK1914: C++ Programming.

PARAMETER PASSING

• A function may define parameters which a caller needs to provide when calling that function.

• The provision of parameters when calling a function is sometimes referred to as parameter passing.

• We will look at two ways of passing parameters to functions:– Pass by value (value parameters)– Pass by reference (reference parameters)

• Analogy: Paying for something by cash / direct debit

3FTSM :: TK1914, 20112012

Page 4: User-Defined Functions II TK1914: C++ Programming.

VALUE PARAMETERS

• If a formal parameter is a value parameter– The value of the corresponding actual parameter is

copied into it

• The formal parameter has its own copy of the value of the corresponding actual parameter.

• During program execution– The value parameter manipulates the data stored in its

own memory space

4FTSM :: TK1914, 20112012

Page 5: User-Defined Functions II TK1914: C++ Programming.

CLUSTER ACTIVITY 6.7

• Refer prog06.7.cpp– Examine the source code. What do you think the

program is trying to do?– What do you think the program will output if the user

inputs 53?– Run the program to see if it behaves as expected.– Could you explain your observation?

5FTSM :: TK1914, 20112012

Page 6: User-Defined Functions II TK1914: C++ Programming.

REFERENCE PARAMETERS

• If a formal parameter is a reference parameter

– It receives the address of the corresponding actual parameter

• A reference parameter stores the address of the corresponding actual parameter

6FTSM :: TK1914, 20112012

Page 7: User-Defined Functions II TK1914: C++ Programming.

REFERENCE PARAMETERS

• When a function with reference parameters is executed

– The address stored in those parameters are used to refer to the memory spaces of the corresponding actual parameters

7FTSM :: TK1914, 20112012

Page 8: User-Defined Functions II TK1914: C++ Programming.

REFERENCE PARAMETERS

A reference parameter receives the address of the actual parameter

Using reference parameters, you can:

Pass one or more values from a function

Change the value of the actual parameter

8FTSM :: TK1914, 20112012

Page 9: User-Defined Functions II TK1914: C++ Programming.

REFERENCE PARAMETERS

Reference parameters are useful in three situations:

Returning more than one value

Changing the actual parameter

When passing the address would save memory space and time

9FTSM :: TK1914, 20112012

Page 10: User-Defined Functions II TK1914: C++ Programming.

CLUSTER ACTIVITY 6.8

• Refer prog06.8.cpp– Examine the source code. How is it different from prog06.7.cpp?

– What do you think the program will output if the user inputs 53?

– Run the program to see if it behaves as expected.– Could you explain your observation?

10FTSM :: TK1914, 20112012

Page 11: User-Defined Functions II TK1914: C++ Programming.

PARAMETERS AND MEMORY ALLOCATION

• When a function is called– Memory for its formal parameters and variables

declared in the body of the function (called local variables) is allocated in the function data area

11FTSM :: TK1914, 20112012

Page 12: User-Defined Functions II TK1914: C++ Programming.

PARAMETERS AND MEMORY ALLOCATION

• In the case of a value parameter– The value of the actual parameter is copied into the

memory cell of its corresponding formal parameter

• In the case of a reference parameter

– The address of the actual parameter is passed to the formal parameter

– In the other words, the content of the formal parameter is an address

12FTSM :: TK1914, 20112012

Page 13: User-Defined Functions II TK1914: C++ Programming.

CLUSTER ACTIVITY 6.9

• Refer prog06.9a.cpp– Examine the source code. What do you think the

program is trying to do?– What do you think the program will output?– Run the program. Is the output generated as

expected? Explain.• Refer prog06.9b.cpp

– Examine the source code. How is it different from the previous program?

– What do you think the program will output?– Run the program. Is the output generated as

expected? Explain.13FTSM :: TK1914, 20112012

Page 14: User-Defined Functions II TK1914: C++ Programming.

SCOPE OF AN IDENTIFIER

• The scope of an identifier refers to where in the program an identifier is accessible

• Local identifier - identifiers declared within a function (or block)

• Global identifier – identifiers declared outside of every function definition

14FTSM :: TK1914, 20112012

Page 15: User-Defined Functions II TK1914: C++ Programming.

CLUSTER ACTIVITY 6.10

• Refer prog06.10.cpp– Examine the source code. What do you think the

program is trying to do?– Compile the source code.

• Study the compilation error messages.• Could you explain the compile-time errors?

– Can you suggest one way of correcting the program?

15FTSM :: TK1914, 20112012

Page 16: User-Defined Functions II TK1914: C++ Programming.

CLUSTER ACTIVITY 6.11

• Refer prog06.11.cpp– Examine the source code. How is it different from prog06.10.cpp?

– Compile the program. Are there any compilation errors?

– Run the program to test it. Does the program produce correct results?

16FTSM :: TK1914, 20112012

Page 17: User-Defined Functions II TK1914: C++ Programming.

CLUSTER ACTIVITY 6.12

• Refer prog06.12.cpp– Examine the source code.

• How many variables in the program are named area?

• How are they different?– Compile the program. Are there any compilation

errors?– What do you think the program will output if the user

inputs 20 for width and 10 for height?– Run the program. Is the output as expected?

17FTSM :: TK1914, 20112012

Page 18: User-Defined Functions II TK1914: C++ Programming.

CLUSTER ACTIVITY 6.13

• Refer prog06.13.cpp– Examine the source code. What do you think the

program does?– Focus on the if statement.

• What is the purpose of the if statement?• Observe that the variable temp is declared in

the body of the if statement.• Insert a cout statement WITHIN the body of

the if statement which outputs the value of temp after swapping.

• Recompile the program and run it.

18FTSM :: TK1914, 20112012

Page 19: User-Defined Functions II TK1914: C++ Programming.

CLUSTER ACTIVITY 6.13 (CONT.)

• Insert the same cout statement OUTSIDE the body of the if statement.

• Recompile the program. Could you explain the compilation error?

19FTSM :: TK1914, 20112012

Page 20: User-Defined Functions II TK1914: C++ Programming.

CLUSTER ACTIVITY 6.14

• Refer prog06.14.cpp– Examine the source code.

• How is it different from prog06.12.cpp?

• Which variable does ::area in the function calcArea refer to?

– Compile the program. Are there any compilation errors?

– What do you think the program will output if the user inputs 20 for width and 10 for height?

– Run the program. Is the output as expected?

20FTSM :: TK1914, 20112012

Page 21: User-Defined Functions II TK1914: C++ Programming.

GLOBAL VARIABLES

• The operator :: is called the scope resolution operator

• By using the scope resolution operator – A global variable declared before the definition of a

function (block) can be accessed by the function (or block) even if the function (or block) has an identifier with the same name as the variable

21FTSM :: TK1914, 20112012

Page 22: User-Defined Functions II TK1914: C++ Programming.

GLOBAL VARIABLES

• C++ provides a way to access a global variable declared after the definition of a function

• In this case, the function must not contain any identifier with the same name as the global variable

22FTSM :: TK1914, 20112012

Page 23: User-Defined Functions II TK1914: C++ Programming.

CLUSTER ACTIVITY 6.15

• Refer prog06.15.cpp– Examine the source code.

• How is it different from prog06.11.cpp?• What is the purpose of the function outputDebugMessages?

– What do you think the program will output if the user inputs 20 for width and 10 for height?

– Run the program. • Is the output as expected?• Before analyzing the source code in detail, could

you guess which function in the program is most likely to contain the source of the error?

• Analyze the program code for the source of the error. Is it as you suspected?

23FTSM :: TK1914, 20112012

Page 24: User-Defined Functions II TK1914: C++ Programming.

GLOBAL VARIABLES

• Using global variables has side effects

• Any function that uses global variables

– Is not independent

– Usually cannot be used in more than one program

24FTSM :: TK1914, 20112012

Page 25: User-Defined Functions II TK1914: C++ Programming.

GLOBAL VARIABLES

• If more than one function uses the same global variable and something goes wrong

– It is difficult to find what went wrong and where

• Problems caused by global variables in one area of a program might be misunderstood as problems caused in another area

• A very good practice in programming is to try to avoid the use of unnecessary global variables in programs.

25FTSM :: TK1914, 20112012

Page 26: User-Defined Functions II TK1914: C++ Programming.

STATIC AND AUTOMATIC VARIABLES

• Read textbook, pg 380

ROYO

26FTSM :: TK1914, 20112012

Page 27: User-Defined Functions II TK1914: C++ Programming.

FUNCTION OVERLOADING

• Read textbook, pg 382

ROYO

27FTSM :: TK1914, 20112012

Page 28: User-Defined Functions II TK1914: C++ Programming.

DEFAULT PARAMETERS

• Read textbook, pg 384

ROYO

28FTSM :: TK1914, 20112012

Page 29: User-Defined Functions II TK1914: C++ Programming.

PROGRAMMING EXAMPLE: DATA COMPARISON

• Read textbook, pg 392

ROYO

29FTSM :: TK1914, 20112012

Page 30: User-Defined Functions II TK1914: C++ Programming.

YOU SHOULD NOW KNOW…

Passing parameters by value and by reference Value and reference parameters

in what situation should each of them be used Scope of an identifier

Local variables Global variables

why you should avoid unnecessary use of global variables in your programs

30FTSM :: TK1914, 20112012