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

Slides:



Advertisements
Similar presentations
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Advertisements

C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
AU/MITM/1.6 By Mohammed A. Saleh 1. Arguments passed by reference  Until now, in all the functions we have seen, the arguments passed to the functions.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 Functions.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
Chapter 14: Overloading and Templates
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 7: User-Defined Functions II.
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Chapter 11: Classes and Data Abstraction
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Chapter 7: User-Defined Methods
Chapter 15: Operator Overloading
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
Review of C++ Programming Part II Sheng-Fang Huang.
Pointer Data Type and Pointer Variables
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
TK 1914 : C++ Programming Control Structures I (Selection)
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
1 Brief Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
1 MORE ON MODULAR DESIGN: MODULE COMMUNICATIONS. 2 WHEN A FUNCTION IS INVOKED, MEMORY IS ALLOCATED LOCALLY FOR THE FORMAL PARAMETERS AND THE VALUE OF.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 05: Classes and Data Abstraction.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
1 Functions Part 1 Prototypes Arguments Overloading Return values.
Chapter 7 User-Defined Methods.
Chapter 7: User-Defined Functions II
A Lecture for the c++ Course
About the Presentations
Function There are two types of Function User Defined Function
User-Defined Functions
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
User Defined Functions
FUNCTION CSC128.
Chapter 7: User-Defined Functions II
Standard Version of Starting Out with C++, 4th Edition
Presentation transcript:

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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

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

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

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

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,