Expression Evaluation, Scope, Parameter Passing, Data Organization, Program Control, Binding By...

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

Transcript of Expression Evaluation, Scope, Parameter Passing, Data Organization, Program Control, Binding By...

Expression Evaluation, Scope, Parameter Passing, Data Organization, Program Control, Binding

By Ramon QuiuskyCS 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 Scope1. 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-value2. 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 <iostream.h>

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.

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 structureactive data structurepersistent data structurerecursive data structure

See http://www.nist.gov/dads/ for definitions

Program Organization 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 efficientLess flexibleStatic type checking leads to safer programs

Dynamic Binding:More flexibleLess 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 http://www.csd.uu.se/kurs/oop

/ht98/Lectures/D5/html/Dynamic_Binding/sld019.htm http://msdn.microsoft.com/library/ http://www.nist.gov/dads/