Functions g g Data Flow g Scope local global part 4 part 4.

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

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.
Chapter 7: User-Defined Functions II
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 5. Functions.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
1 Chapter 7 Functions Dale/Weems/Headington. 2 Functions l Control structures l every C++ program must have a function called main l program execution.
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.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
Chapter 6: Functions.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
1 Chapter 9 Scope, Lifetime, and More on Functions.
Functions Structured Programming 256 Chapter 6 Functions g prototypes g arguments g overloading g return values part I.
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.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
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.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
1 Value Returning Functions // Function prototype int Largest(int num1, int num2, int num3); Function Name Type Parameters Type of parameters Formal parameters.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
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.
Passing Data - by Reference Syntax && double Pythagorus(double &, double &); Pythagorus(height, base); & & double Pythagorus(double& a, double& b) function.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Chapter 7 Functions. Types of Functions Value returning Functions that return a value through the use of a return statement They allow statements such.
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.
Cosc175/module.ppt1 Introduction to Modularity Function/procedures void/value-returning Arguments/parameters Formal arguments/actual arguments Pass by.
Functions g g Data Flow g Scope local global part II g Global Resolution Operator part II.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13.
#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.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
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.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
April 11, 2005 More about Functions. 1.Is the following a function call or a function header? calcTotal(); 2.Is the following a function call or a function.
1 Chapter 9 Scope, Lifetime, and More on Functions.
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
1 Scope Lifetime Functions (the Sequel) Chapter 8.
User-Defined Functions (cont’d) - Reference Parameters.
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
CHAPTER 8 Scope, Lifetime, and More on Functions.
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 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.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
Functions Scope local global Global Resolution Operator part 5.
Chapter 7: User-Defined Functions II
Functions prototypes arguments overloading return values part I.
Suppose we want to print out the word MISSISSIPPI in big letters.
Functions and Structured Programming
Functions Scope local global Global Resolution Operator part II
Scope of Variables The region of code where it is legal to reference (use) an identifier. Local Scope Global Scope Class Scope.
Chapter 9 Scope, Lifetime, and More on Functions
CS150 Introduction to Computer Science 1
Functions Imran Rashid CTO at ManiWeber Technologies.
CS1201: Programming Language 2
FOR statement a compact notation for a WHILE e.g. sumgrades = 0;
Programming Fundamental
Presentation transcript:

Functions g g Data Flow g Scope local global part 4 part 4

Data Flow Data flow is the direction of the information flow between the function and its caller. Adding data flow documentation to the function interface is helpful. The flow could be into a function, out of a function or both.

Parameter and Data Flow /* in */ 4 pass data into a function/* in */ /* out */ 4 pass data out of a function/* out */ /* inout */ 4 pass data into and out of a function/* inout */

Examples void myFunction( /* in */ double nana, /* in */ int count) void yourFunction( /* out */ int& num1, /* out */ int& num2) void ourFunction( /* in */ int alpha, /* inout */ int& beta) Parameter and Data Flow

, To be certain a function does what you want it to do, write value of variables as you enter and exit a function., Put the output statement into a function and call it whenever you need it. void ShowIt(void) { cout<< var1<< ‘\t’ <<var2<< ‘\t’ <<var3<< ‘\n’; } *

Data Flow - Example #include using namespace std; void getTemp(double&); void activity(double); void convertCtoF(double&); void main(void) { double temperature; getTemp(temperature); activity(temperature); }

Data Flow - Example void getTemp(/* */ double& temp){ cout<<"Enter the temperature in degrees C: "; cin>> temp; cout<<"The current temperature is " <<temp<<" degrees celsius."<<endl; convertCtoF(temp);} void convertCtoF( /* */ double& temp){ temp=(1.8)*temp +32; cout<<"This equals "<<temp <<" degrees Fahrenheit."<<endl;} out inout *

Data Flow - Example void activity(/* */ double temp){ cout<<"The recommended activity is "; if(temp>85) cout<<"swimming."<<endl; else if(temp>70) cout<<"tennis."<<endl; else if(temp>35) cout<<"golf."<<endl; else if(temp>10) cout<<"skiing."<<endl; else cout<<"dancing."<<endl;} in *

Scope A function is like a black box: You know what goes in and what comes out, but you do not know what happens inside the box.

Scope The section of the program where the variable is valid (known or visible). local = available to only one function or block. Used to limit access. global = available multiple functions or blocks. Used to save call time or reduce complexity of call sequence

Scope block Local: The scope of an identifier declared inside a block extends from the point of declaration to the end of that block. Global: The scope of an identifier declared outside all functions and classes extends from the point of declaration to the end of the source file. * Block = { }

Local Variables / declared within a function definition / private to a function definition / variables in different functions are totally independent / different functions can have variables with the same names; however, each variable will have its own memory address / actually applies to any block * *

int x = 3; int x = 3;// global because before main void main(void) {// no variables local to main( ) void myfunction( );// prototype cout <<"x = "<<x<<" before the function call.\n"; myfunction( ); cout <<"x = "<<x<<" after the function call.\n"; } void myfunction( ) { int r;// local to myfunction( ) r = ++x; cout <<"r = "<<r<<" within the function.\n"; } // what happens to global x?

Scope OUTPUT x = 3 before the function call. r = 4 within the function. x = 4 after the function call.

Example Example - ReadValues void main(void) { int a, b, c; float avg; void ReadValues( int&, int&, int& ); void Adjust( int&, int&, int& ); float Average( int, int, int ); void WriteResults( int, int, int, int, float ); ReadValues(a, b, c); Adjust(a, b, c); avg = Average(a, b, c); WriteResults(a, b, c, a + b + c, avg); }

Example Example - ReadValues 1.main declares and calls ReadValues 2.ReadValues declares and calls ReadOne [3x] 3.main declares and calls Adjust 4.main declares and calls Average 5.main declares and calls WriteResults * *

Example Example - ReadValues void ReadValues( /* */ int& x, /* */ int& y, /* */ int& z ) { void ReadOne( char, int& ); ReadOne('1', x ); ReadOne('2', y ); ReadOne('3', z ); return; } * out

Example Example - ReadOne void ReadOne( /* */ char number, /* */ int& item ) { cout << "Enter value " << number << ": "; cin >> item; return; } * in out

Example Example - Adjust void Adjust( /* */ int& i, /* */ int& j, /* */ int& k ) { int smallest; smallest = i; if (j < smallest) i = i - smallest; smallest = j; j = j - smallest; if (k < smallest) k = k - smallest; smallest = k; return; } * inout

Example Example - Average float Average( /* */ int item1, /* */ int item2, /* */ int item3 ) { int total; total = item1 + item2 + item3; return float(total) / 3; } * in

Example Example - WriteResults void WriteResults( /* */ int item1, /* */ int item2, /* */ int item3, /* */ int total, /* */ float average ) { cout << "Adjusted values: " << item1 << ", " << item2 << ", " << item3 << '\n' << "Sum: " << total << " Average: " << average << '\n'; return; } * in

ReadValuesAdjustAverageWriteResults ReadOne Main Enter value 1: 23 Enter value 2: 56 Enter value 3: 78 Adjusted values: 0, 33, 55 Sum: 88 Average:

Common Errors 4 Wrong positioning of the called function prototype 4 Terminating a function header with a ; 4 Forgetting the data type of a function’s parameter 4 Remember the NOT rule: 4 Number 4 Order 4 Type