1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
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.
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.
C++ Pointer and Functions
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
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.
Computer Science 1620 Function Scope & Global Variables.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Methods of variable creation Global variable –declared very early stage –available at all times from anywhere –created at the start of the program, and.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
1 Lecture 17:User-Definded function II Introduction to Computer Science Spring 2006.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
1 Chapter 9 Scope, Lifetime, and More on Functions.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
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.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Pointer Data Type and Pointer Variables. Objectives: Pointer Data Type and Pointer Variables Pointer Declaration Pointer Operators Initializing Pointer.
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.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
User-Defined Functions II TK1914: C++ Programming.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Chapter 7 User-Defined Methods.
Chapter 7: User-Defined Functions II
A Lecture for the c++ Course
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
User-Defined Functions
Dynamic Memory Allocation Reference Variables
User-defined Functions
Chapter 9 Scope, Lifetime, and More on Functions
User Defined Functions
6 Chapter Functions.
User-defined Functions
Pointers & Functions.
FUNCTION CSC128.
Chapter 7: User-Defined Functions II
Dynamic Memory.
Pointers & Functions.
Presentation transcript:

1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006

2 Parameters Passing Pass by value (A formal parameter is a value parameter): The value of the corresponding actual parameter is copied into it The value parameter has its own copy of the data Any modifications to the local copy do not change the original variable in the calling program Pass by reference (A formal parameter is a reference parameter): An alias of the argument is passed to the called function. no copies of the actual parameter are made. When references are passed into a function, any changes made to the references will be seen in the calling function.

3 #include using namespace std void swap(int x, int y); int main() { int x = 4; int y = 2; cout << "Before swap, x is " << x << ", y is " << y << endl; swap(x,y); cout << "After swap, x is " << x << ", y is " << y << endl; } void swap(int first, int second) { int temp; temp = second; second = first; first = temp; } A formal parameter receives a copy of the content of corresponding actual parameter. which is called Pass by value & & & & Any modifications to the local copy do not change the original variable in the calling program. An alias of the argument is passed to the called function. which is called Pass by reference When references are passed into a function, any changes made to the references will be seen in the calling function. Parameters Passing

4 #include using namespace std; void addFirst(int& first, int& second); void doubleFirst(int one, int two); void squareFirst(int& ref, int val); int main () { int num = 5; addFirst(num, num); doubleFirst(num, num); squareFirst(num, num); return 0; } void addFirst(int& first, int& second) { first = first + 2; second = second * 2; } void doubleFirst(int one, int two) { one = one * 2; two = two + 2; } void squareFirst(int& ref, int val) { ref = ref * ref; val = val + 2; } // After this statement, num=5 //Now first=5, second=5 //After this statement, first=7, second=7 // After this statement, first=14, second=14 // After this statement, num=14 //Now one=14, two=14 //After this statement, one=28, two=14 //After this statement, one=28, two=16 // After this statement, num=14 //Now ref=14, val=14 //After this statement, ref=196, val=14 //After this statement, ref=196, val=16 // After this statement, num=196 //first and second are reference parameters //first, second and num refer to the same object //ref is reference parameters //ref and num refer to the same object

5 Reference Variables as Parameters  Reference parameters can:  Pass one or more values from a function  Change the value of the actual parameter  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

6 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 C++ does not allow nested functions The definition of one function cannot be included in the body of another function

7 #include using namespace std; void f(); int main() { int x=1; f(); } void f() { cout<<“x=”<<x; } Error: x in function f() is not declared. Output : x=2 Try 1 #include using namespace std; void f(); int main() { int x=1; f(); } void f() { int x=2; cout<<“x=”<<x; } Try 2 #include using namespace std; int x=1; void f(); int main() { f(); } void f() { cout<<“x=”<<x; } Try 3 Output : x=1 #include using namespace std; void f(int num); int main() { int x=1; f(x); } void f(int num) { cout<<“x=”<<num; } Try 4 Output : x=1 Local identifier Global identifier Local identifier

8 Scope of an Identifier (continued) Global identifiers (such as variables) are accessible by a function or a block if The identifier is declared before the function definition (block) The function name is different from the identifier All parameters of the function have names different than the name of the identifier All local identifiers (such as local variables) have names different than the name of the identifier

9 #include using namespace std; void f() { cout<<"x="<<x; } int x=1; int main() { f(); } Error: x in function f() is not declared. Error: 'x' redefinition; previous definition was 'function' Case 1 #include using namespace std; void x(); int x=1; int main() { x(); } void x() { cout<<"x="<<x; } Case 2 #include using namespace std; int x=1; void f(int x); int main() { int num=2; f(num); } void f(int x) { cout<< "x="<<x; } Case 3 Output : x=2 #include using namespace std; void f(); int x=1; int main() { f(); } void f() { int x=2; cout<< "x="<< x; } Case 4 Output : x=2 The global identifier should be declared before the function definition The global identifier x can not be accessed by f() The global identifier x can not be accessed by function x() The function name should be different from the global identifier The global identifier x can not be accessed by function f() All parameters of the function should have names different than the name of the global identifier The global identifier x can not be accessed by function f() All local identifiers (such as local variables) should have names different than the name of the global identifier

10 Scope of an Identifier (continued) An identifier declared within a block (Nested Block) is accessible: Only within the block from the point it is declared until the end of the block By those blocks that are nested within that block if the nested block does not have an identifier with the same name as that of the outside block (the block that encloses the nested block) The scope of a function name is similar to the scope of an identifier declared outside of any block

11 #include using namespace std; void f(); int x=1; int main() { int x=2; { int x=3; { int x=4; { int x=5; cout<<"Line 1: "<<x<<endl; } cout<<"Line 2: "<<x<<endl; } cout<<"Line 3: "<<x<<endl; } cout<<"Line 4: "<<x<<endl; f(); } void f() { cout<<"Line 5: "<<x<<endl; } Output: Line 1: 5 Line 2: 4 Line 3: 3 Line 4: 2 Line 5: 1

12 Global Variables Some compilers initialize global variables to default values 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 C++ provides a way to access a global variable declared after the definition of a function extern int w; In this case, the function must not contain any identifier with the same name as the global variable

13 #include using namespace std; void f(); int x=1; int main() { f(); } void f() { int x=2; cout<< "x="<< x; } Case 4 Output : x=2 The global identifier x can not be accessed by function f() All local identifiers (such as local variables) should have names different than the name of the global identifier #include using namespace std; void f(); int x=1; int main() { f(); } void f() { int x=2; cout<< "x="<<::x; } Output: x=1

14 #include using namespace std; void f() { cout<<"x="<<x; } int x=1; int main() { f(); } Error: x in function f() is not declared. Case 1 The global identifier should be declared before the function definition The global identifier x can not be accessed by f() #include using namespace std; void f() { extern int x; cout<<"x="<<x; } int x=1; int main() { f(); } Output: x=1

15 Side Effects of Global Variables Using global variables has side effects Any function that uses global variables Is not independent Usually it cannot be used in more than one program 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

16 Static and Automatic Variables Automatic variable - memory is allocated at block entry and deallocated at block exit Static variable - memory remains allocated as long as the program executes Variables declared outside of any block are static variables By default variables declared within a block are automatic variables

17 Static and Automatic Variables (continued) Declare a static variable within a block by using the reserved word static The syntax for declaring a static variable is: static dataType identifier; The statement static int x; declares x to be a static variable of the type int Static variables declared within a block are local to the block Their scope is the same as any other local identifier of that block

18 Output: x=2 y=11 x=4 y=11 x=6 y=11 x=8 y=11 x=10 y=11 //Program: Static and automatic variables #include using namespace std; void test(); int main() { int count; for (count = 1; count <= 5; count++) test(); return 0; } void test() { static int x = 0; int y = 10; x = x + 2; y = y + 1; cout << "x = " << x <<endl; cout << " y = " << y << endl; }

19 End of lecture 18 Thank you!