Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
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)
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
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.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 5 Functions.
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.
Chapter 5 Functions.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
More on Functions Programming. COMP104 Lecture 19 / Slide 2 Passing Parameters by Reference l To have a function with multiple outputs, we have to use.
Programming Scope of Identifiers. COMP102 Prog. Fundamentals I: Scope of Identifiers/ Slide 2 Scope l A sequence of statements within { … } is considered.
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects.
Functions:Passing Parameters by Value Programming.
Computer Science 1620 Function Scope & Global Variables.
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.
Methods of variable creation Global variable –declared very early stage –available at all times from anywhere –created at the start of the program, and.
Chapter 6: Functions.
Programming Functions: Passing Parameters by Reference.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
Copyright © 2012 Pearson Education, Inc. Chapter 6 Modular Programming with Functions.
Functions g g Data Flow g Scope local global part 4 part 4.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
M. Taimoor Khan #include void main() { //This is my first C++ Program /* This program will display a string message on.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
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.
Functions Pass by reference, or Call by reference Passing addresses Use of & part 3.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
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.
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.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
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.
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Function Definition: Return_DT F_name (
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.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
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.
A Lecture for the c++ Course
Engineering Problem Solving with C++, Etter
Pointers and Pointer-Based Strings
Global & Local Identifiers
User-defined Functions
Modular Programming with Functions
Chapter 5 Function Basics
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Return_DT F_name ( list of formal parameters)
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
User-defined Functions
Pointers & Functions.
Anatomy of a Function Part 1
Dr. Khizar Hayat Associate Prof. of Computer Science
Function “Inputs and Outputs”
Local, Global Variables, and Scope
The Function Prototype
Pointers and Pointer-Based Strings
Fundamental Programming
CS150 Introduction to Computer Science 1
Predefined Functions Revisited
Functions Imran Rashid CTO at ManiWeber Technologies.
Pointers & Functions.
Dr. Khizar Hayat Associate Prof. of Computer Science
CS1201: Programming Language 2
Presentation transcript:

Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13

PARAMETER PASSING pass by value pass by reference storage class and scope Engineering Problem Solving with C++, Second edition, J. Ingber 2

Parameter Passing  C++ supports two forms of parameter passing: –Pass by value. –Pass by reference. Engineering Problem Solving with C++, Second edition, J. Ingber 3

Parameter Passing  C++ supports two forms of parameter passing: –Pass by value. –Pass by reference. Engineering Problem Solving with C++, Second edition, J. Ingber 4

5 Parameter Passing - pass by value –Pass by value is the default in C++ (except when passing arrays as arguments to functions). –The formal parameter receives the value of the argument. –Changes to the formal parameter do not affect the argument.

Engineering Problem Solving with C++, Second edition, J. Ingber 6 //Function Definition int fact(int num)//4 { int nfact = 1;//5 while(num>1) { nfact = nfact*num; num--; } return(nfact);//6 } //end fact int main() { int n, factorial;//1 cin >> n; //2 if(n>=0) { factorial = fact(n);//3 cout << n <<"! is " << factorial << endl;//7 }//end if return 0; }//end main

Engineering Problem Solving with C++, Second edition, J. Ingber 7 Parameter Passing - pass by reference  Pass by reference allows modification of a function argument.  Must append an & to the parameter data type in both the function prototype and function header void getDate(int& day, int& mo, int& year)  Formal parameter receives the address of the argument.  Any changes to the formal parameter directly change the value of the argument.

Pass-By-Reference Example  scale.cpp

Engineering Problem Solving with C++, Second edition, J. Ingber 9 Example - pass by reference #include using namespace std; //Function swap interchanges the values of two variables //function definition void swap(double& a, double& b)//function header { double temp;//local variable temp temp = a; a=b; b=temp; return;//optional return statement }//end swap

Engineering Problem Solving with C++, Second edition, J. Ingber 10 Another Example - pass by reference int main() { double x = 5, y = 10; swap(x,y);//function call; x y are arguments cout >> “x = “ << x << ‘,’ << “ y= “ << y << endl; return 0; } //end main Output?

Engineering Problem Solving with C++, Second edition, J. Ingber 11 Practice! - What is the output? #include using namespace std; void fun(int& a1, int& a2, int a3) {a1++; a2++; a3--; } int main() {int c1=1, c2=2, c3=3; cout << c1 << ‘,’ << c2 << ‘,’ << c3 << endl; fun(c1,c2,c3); cout << c1 << ‘,’ << c2 << ‘,’ << c3 << endl; fun(c3, c2, c1); cout << c1 << ‘,’ << c2 << ‘,’ << c3 << endl; return 0; }

Engineering Problem Solving with C++, Second edition, J. Ingber 12 Scope  Scope refers to the portion of the program in which it is valid to reference a function or a variable.  Block  A sequence of statements enclosed in {} that introduces a new scope

13 Scope  Local scope - a local variable is defined within a function or a block and can be accessed only within the function or block that defines it  Global scope - a global variable is defined outside the any function and can be accessed by any function within the program file.

14 Scope  Ch5/scope1.cpp - Global PI and local variables belonging to the functions  Ch5/scope2.cpp - Global variable referred to in main and other function.  Ch5/scope3.cpp - Local variable declared within an if block.

15 Questions  Use pass-by-______________ when a change in the argument is desired.  The_________ of a name is the portion of the program in which the name can be used.  A variable declared outside of any block is called a _________ variable.

Write a Midpoint Function Write a C++ function to find the midpoint of a line segment in a Cartesian plane, given the coordinates of the endpoints of the segment.

Write normalize( ) Write a C++ function to take the numerator and denominator of a fraction. It should normalize the fraction. Change the fraction so that the denominator is a positive number.