Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "User-Defined Functions II TK1914: C++ Programming."— Presentation transcript:

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

2 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

3 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

4 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

5 CLUSTER ACTIVITY 6.7 Refer prog06.7.cpp 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

6 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

7 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

8 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

9 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

10 CLUSTER ACTIVITY 6.8 Refer prog06.8.cpp prog06.8.cpp –Examine the source code. How is it different from prog06.7.cpp ? 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

11 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

12 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

13 CLUSTER ACTIVITY 6.9 Refer prog06.9a.cpp 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 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

14 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

15 CLUSTER ACTIVITY 6.10 Refer prog06.10.cpp 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

16 CLUSTER ACTIVITY 6.11 Refer prog06.11.cpp prog06.11.cpp –Examine the source code. How is it different from prog06.10.cpp ? 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

17 CLUSTER ACTIVITY 6.12 Refer prog06.12.cpp 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

18 CLUSTER ACTIVITY 6.13 Refer prog06.13.cpp 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

19 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

20 CLUSTER ACTIVITY 6.14 Refer prog06.14.cpp prog06.14.cpp –Examine the source code. How is it different from prog06.12.cpp ? 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

21 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

22 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

23 CLUSTER ACTIVITY 6.15 Refer prog06.15.cpp prog06.15.cpp –Examine the source code. How is it different from prog06.11.cpp ? 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

24 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

25 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

26 STATIC AND AUTOMATIC VARIABLES Read textbook, pg 380 ROYO 26FTSM :: TK1914, 20112012

27 FUNCTION OVERLOADING Read textbook, pg 382 ROYO 27FTSM :: TK1914, 20112012

28 DEFAULT PARAMETERS Read textbook, pg 384 ROYO 28FTSM :: TK1914, 20112012

29 PROGRAMMING EXAMPLE: DATA COMPARISON Read textbook, pg 392 ROYO 29FTSM :: TK1914, 20112012

30 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


Download ppt "User-Defined Functions II TK1914: C++ Programming."

Similar presentations


Ads by Google