Expression Evaluation, Scope, Parameter Passing, Data Organization, Program Control, Binding By Ramon Quiusky CS 490 Fall 2003.

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms The C Programming Language.
Advertisements

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.
Functions in C++. Functions  Groups a number of program statements into a unit & gives it a name.  Is a complete and independent program.  Divides.
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.
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
Variables Names Bindings Type Scope. L-Value versus R-Value Not complicated Associated with assignment statements Left hand side represents an address.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 7: User-Defined Functions II.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects.
CS 201 Functions Debzani Deb.
1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
Chapter 6: Functions.
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).
CS 403: Programming Languages Lecture 2 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
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
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Variable Scope Storage Class Recursion
Basic Semantics Associating meaning with language entities.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120: Introduction to Computer Science Functions.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
CPS120: Introduction to Computer Science Lecture 14 Functions.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
FUNCTIONS. Funtions  The heart of effective problem solving is problem decomposition.  breaking a problem into small, manageable pieces  In C, the.
CSCI 171 Presentation 6 Functions and Variable Scope.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
1 Brief Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
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.
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.
L what are executable/non-executable statements l out of the ones below which constructs are executable #include p=3.14; const double PI=3.14; int myfunc(int);
Constructs for Data Organization and Program Control, Scope, Binding, and Parameter Passing. Expression Evaluation.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
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.
APS105 Functions (and Pointers) 1. Modularity –Break a program into manageable parts (modules) –Modules interoperate with each other Benefits of modularity:
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Functions + Overloading + Scope
Functions.
Chapter 7 User-Defined Methods.
CSC113: Computer Programming (Theory = 03, Lab = 01)
User-Defined Functions
6 Chapter Functions.
Binding Times Binding is an association between two things Examples:
Scope Rules and Storage Types
Chapter 7: User-Defined Functions II
Languages and Compilers (SProg og Oversættere)
The Function Prototype
Programming Languages and Paradigms
Standard Version of Starting Out with C++, 4th Edition
Functions Chapter No. 5.
Presentation transcript:

Expression Evaluation, Scope, Parameter Passing, Data Organization, Program Control, Binding By Ramon Quiusky CS 490 Fall 2003

Expression Evaluation Expressions are evaluated according to the precedence and order of their operators. Consider the following examples:

Ex. 1 Multiplication has the highest priority. Addition has the next highest precedence Left shift has the lowest precedence.

Ex. 2

Scope Variables that are declared within the body of a function definition are said to be local to that function or to have that function as their scope. A variable within a function definition can have the same name of a variable within another function of main function definition (Static Scope).

5 Types of Scope 1. Local scope: a name declared within a block is accessible only within that block and blocks enclosed by it. { int i; }

2. Function scope: Labels are the only names that have function scope. They can be used anywhere within a function, but are not accessible outside that function. 3. File scope: Any name declared outside all blocks or classes has file scope. It is accessible anywhere in the translation unit after its declaration. Also known as Global Variables.

4. Class scope: Names of class members have class scope. Class member functions can be accessed only by using the member- selection operators (. or –>) or pointer-to-member operators (.* or –>*) on an object or pointer to an object of that class. 5. Prototype scope: Names declared in a function prototype are visible only until the end of the prototype. The following prototype declares two names (szDest, szSource); these names go out of scope at the end of the prototype: char *strcpy( char *szDest, const char *szSource );

Parameter Passing Two main parameter types: 1. Call-by-value 2. Call-by-reference But first, what are formal parameters and arguments?

Formal Parameter: a kind of blank space holder that is filled in with something when the function is called. They are listed in a function prototype and used in the body of a function definition. Argument: something that is used to fill in a formal parameter. When a function call is execute, the arguments are “plugged in” for the formal parameters.

Example: //libraries (I/O, math, etc) //prototype: int sum(int a, int b); //======= //main function //function call: cout << “Sum = “ << sum(24, 25) << endl; //======= int sum(int a, int b) { return (a+b); }

1. Call-by-value: a formal parameter that when listed in the prototype of a function there is no ampersand (“&”) attached to its type specification ex: int sum(int a, int b);

2. Call-by-value: a formal parameter that when listed in the prototype of a function there is an ampersand (“&”) attached to its type specification

#include void get_number(int& input1, int& input2); void show_results(int output1, int output2); int main() { int first_num, second_num; get_number(first_num, second_num); show_results(first_num, second_num); return 0; } void get_number(int& input1, int& input2) { cin >> input1; cin >> input2; } void show_results(int output1, int output2) { cout << "\n\nnumber: " << output1 << " " << output2 << endl<< endl; }

Functions can also be used as parameters. Ex.: //function call: sum(get_first_num(), get_sec_num()); int sum(int a, int b); { return (a+b);} int get_first_num() {int a; cin>>a; return a;} int get_sec_num() {int b; cin>>b; return b;}

Constructs for Data Organization What is a data structure? An organization of information, usually in memory, for better algorithm efficiency, such as queue, stack, linked list, heap, dictionary, and tree, or conceptual unity, such as the name and address of a person. It may include redundant information, such as length of the list or number of nodes in a subtree.algorithmefficiencyqueuestack linked listheapdictionarytreelistnodessubtree

Static Data Structure: size determined when a program is compiled Dynamic Data structure: size determined when a program is actually running rather than at compilation time Pointers enable us to define this type of data structure

Other types of data structures: external memory data structure passive data structure active data structure persistent data structure recursive data structure See for definitionshttp://

Program Organization A collection of program units constitutes an executable program. Program units can contain other smaller units. A program consists of the following (from a Fortran Program): Main program External subprogram (subroutine or function) Module Block data

Program Control It is good practice to break a program down into pieces that can be thought of independently. Once the program has been completed, we can think of its execution as being a series of these pieces that work together in a certain sequence. These pieces then pass the control of the program between each other.

Examples if-else Statements while Loops for Loops continue Statement break Statement

Nesting Control Statements if (my_money > cost_of_CD) then buy_CD Else { if (see_a_friend) then borrow_money else get_a_job }

Binding Linking between messages and methods Early Binding: Static binding. Binding of functions at compile time AddRealNums (x, y) x & y are of type real Late Binding: Late Binding. Binding of message selector to the appropriate method at run time AddNumber(x, y) x & y are number objects

Static Type: what can be determined at compile time Dynamic Type: actual type as determined during run time.

Static Binding Vs Dynamic Binding Static binding: binding based on static type More efficient Less flexible Static type checking leads to safer programs Dynamic Binding: More flexible Less efficient May need dynamic type checking

Resources Data Structures and other objects using C++ - Savitch Problem Solving with C++ - Savitch Java: How to Program – Deitel & Deitel _Binding/sld019.htm _Binding/sld019.htm