Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects.

Slides:



Advertisements
Similar presentations
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)
Advertisements

Programming Languages and Paradigms
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
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.
Expression Evaluation, Scope, Parameter Passing, Data Organization, Program Control, Binding By Ramon Quiusky CS 490 Fall 2003.
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Local and Global Variables. COMP104 Local and Global / Slide 2 Scope The scope of a declaration is the block of code where the identifier is valid for.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
Programming Scope of Identifiers. COMP102 Prog. Fundamentals I: Scope of Identifiers/ Slide 2 Scope l A sequence of statements within { … } is considered.
Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects.
Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
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.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Local, Global Variables, and Scope. COMP104 Slide 2 Functions are ‘global’, variables are ‘local’ int main() { int x,y,z; … } int one(int x, …) { double.
1 Data Structures Topic #1 Welcome !. 2 Today’s Agenda Introduction...what to expect!?! Talk about our Goals and Objectives Textbook is highly recommended.
Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Call-by-Value vs. Call-by-Reference Call-by-value parameters are used for passing information from the calling function to the called function (input parameters).
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
FALL 2001ICOM Lecture 21 ICOM 4015 Advanced Programming Lecture 2 Procedural Abstraction Reading: LNN Chapter 4, 14 Prof. Bienvenido Velez.
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.
Names and Scope. Scope Suppose that a name is used many times for different entities in text of the program, or in the course of execution. When the name.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
CPS120: Introduction to Computer Science Decision Making in Programs.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
Chapter 6: Programmer- defined Functions Development of simple functions using value and reference parameters JPC and JWD © 2002 McGraw-Hill, Inc. Modified.
Chapter 8 Scope of variables Name reuse. Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends.
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.
Object Oriented Programming (OOP) Lecture No. 11.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Overloading operators C++ incorporates the option to use standard operators to perform operations with.
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Function Definition: Return_DT F_name (
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
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.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
LECTURE 3 PASS BY REFERENCE. METHODS OF PASSING There are 3 primary methods of passing arguments to functions:  pass by value,  pass by reference, 
Introduction to Programming
Class and Objects UNIT II.
School of EECS, Peking University
COMP2012H Object-Oriented Programming and Data Structures
Pass by Reference.
Local, Global Variables, and Scope
The Function Prototype
7 Arrays.
Fundamental Programming
Predefined Functions Revisited
Object Oriented Programming (OOP) Lecture No. 11
Object Oriented Programming (OOP) Lecture No. 12
Functions Chapter No. 5.
Presentation transcript:

Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects Dynamic objects Functions on objects structured programming (104), modularity (member) variables OOP (104, 151) Data structure: Linear: list, stack, queue Nonlinear: tree, graph Algorithms Algorithms+Data Structures = Programs Niklaus Wirth (171) (member) functions Array, struct pointer objects Data, variable, object Operation, function, procedure, subprogram, module, method operation class C, Pascal C++, Java

Slide 2 int main() { int x,y,z; int a,b,c; a=f1(x); b=f2(y); c=f3(z); … } int f1() { } int f2() { } int f3() { } main(), is the first function, and is composed of a sequence of ‘functions’. int main() { A a; B b; C c; a.f1(); b.f2(); c.f3(); … } Class A { Int x; Int f1(); } Class B { Int y; Int f2() } Class C { Int z; Int f3(); } Structured programming: Object oriented programming: a sequence of ‘objects’!

Slide 3 Programming paradigms * Modular programming (functions) * Object-oriented programming (classes) * Generic programming (templates)

Slide 4 l Pass by value: formal parameters and arguments are different variables. ideal desirable behavior (but not efficient some times) l Pass by reference: they are the same variables, but different names! should carefully handled! Communication between functions:

Slide 5 Reference: X& l int& b a; l b is an alternative name for a void f(int& b) {}; int main() { int a; f(a); } int a=10; int& b = a; int& c = a; b = 100; a ??? int& b; 10 a b c Relationship with pointers (later on)!

Slide 6 int f(int x) { cout << “value of x = “ << x << endl; x = 4; } main() { int v = 5; f(v); cout << “value of v = “ << v << endl; } Output: Value of x = Value of v = n When a variable v is passed by value to a function f, its value is copied to the corresponding variable x in f n Any changes to the value of x does NOT affect the value of v n Call by value is the default mechanism for parameter passing in C Call by Value

Slide 7 int f(int &x) { cout << “value of x = “ << x << endl; x = 4; } main() { int v = 5; f(v); cout << “value of v = “ << v << endl; } Output: Value of x = Value of v = n When a variable v is passed by reference to a parameter x of function f, v and the corresponding parameter x refer to the same variable n Any changes to the value of x DOES affect the value of v 5 4 Call by Reference

Slide 8 int f( const int &x ) { cout << “value of x = “ << x << endl; x = 4; // invalid } main() { int v = 5; f(v); cout << “value of v = “ << v << endl; } Passing variable v by constant reference to parameter x of f will NOT allow any change to the value of x. It is appropriate for passing large objects that should not be changed by the called function. Call by Constant Reference

Slide 9 * Call by value n for small objects that should not be changed by the function * Call by constant reference n for large objects that should not be changed by the function * Call by reference n is appropriate for all objects that may be changed by the function, n not recommended!!! rare! Parameter Passing

Slide 10 * return by value, n for small objects that should not be changed by the function * return by constant reference, n for large objects that should not be changed by the function * return by reference, n for all objects that may be changed by the function, n not recommended!!! rare! Return Passing

Slide 11 Don ’ t do this (very bad!!!) int& foo(){ int local; return local; }

Slide 12 Scope of variables The scope of a declaration is the block of code where the identifier is valid for use. n A global declaration is made outside the bodies of all functions and outside the main program. It is normally grouped with the other global declarations and placed at the beginning of the program file. n A local declaration is one that is made inside the body of a function. Locally declared variables cannot be accessed outside of the function they were declared in. Local to a function (the variables in Main are also local, local to ‘main’ function) n It is possible to declare the same identifier name in different parts of the program : local to a block Some code enclosed in braces

Slide 13 int main() { int x,y,z; … } void f() { int x; … } void f() { int x; x=1; { int x; x=2; cout << x << endl; } cout << x << endl; } int x; int main() { x=0; cout << x << endl; int x; x=1; { int x; x=2; cout << x << endl; } cout << x << endl; } Local to blocksLocal to functions Global

Slide 14 In a for-loop { int i; for (i=1;i<10;i++) cout << A[i]; } for (int i=1;i<10;i++) cout << A[i]; equivalent

Slide 15 Global Variables * Undisciplined use of global variables may lead to confusion and debugging difficulties. * Instead of using global variables in functions, try passing local variables by reference. It is forbidden in structured programming!

Slide 16 int MIN; void min(int,int); int main() { int x,y; cin >> x >> y >> endl; min(x,y); cout << MIN; } void min(int a, int b) { if (a<b) MIN=a; else MIN=b; } void min(int,int,int&); int main() { int x,y,mini; cin >> x >> y >> endl; min(x,y,mini); cout << mini; } void min(int a, int b, int& m) { if (a<b) m=a; else m=b; } int min(int,int); int main() { int x,y,mini; cin >> x >> y >> endl; mini=min(x,y); cout << mini; } int min(int a, int b) { int m; if (a<b) m=a; else m=b; return (m); } Summary Global variablePass by reference Pass by value