PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.

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

Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Functions Prototypes, parameter passing, return values, activation frams.
Introduction to C++ Functions Chapter 6 Sections 6.1 – 6.3 Computer II.
1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging.
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.
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.
1 CS 105 Lecture 10 Functions Version of Mon, Mar 28, 2011, 3:13 pm.
#include using namespace std; void main() { cout
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
Examples from “c++ how to program” book. SELECTIONS WITH IF-ELSE Example: if ( grade >= 60) cout = 60) cout
Programming is instructing a computer to perform a task for you with the help of a programming language.
CSC 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 04.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs.
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
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.
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.
CPS120: Introduction to Computer Science Lecture 14 Functions.
FUNCTIONS (a) Value returning e.g. int main() ….…. return 0; (b) Void (returning) no return statements example To print this message **** ** Welcome.
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Lecture 4 Function example. Example1 int max (int a, int b) { int c; if (a > b) c = a; else c = b; return (c); } void main ( ) {int x, y; cin>>x>>y; cout.
Instructor - C. BoyleFall Semester
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Function prototype A function must be declared before it can be referenced. One way to declare a function is to insert a function prototype before the.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
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 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
User-Defined Functions (cont’d) - Reference Parameters.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Pointers  * symbol and & symbol  Pointer operations  Pointer.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Pointers & Arrays.
Chapter 10: Void Functions
FIGURE 4-10 Function Return Statements
Chapter 5 Function Basics
Learning Objectives Pointers Pointer in function call
Chapter 5 Functions DDC 2133 Programming II.
Pointers Psst… over there.
Method Mark and Lyubo.
Pointers Psst… over there.
Anatomy of a Function Part 2
Chapter 5 Function Basics
CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
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.
CS150 Introduction to Computer Science 1
Pointers & Functions.
Lec 14 Oct 23, 02.
Parameter Passing in Java
CS150 Introduction to Computer Science 1
Dynamic Memory A whole heap of fun….
The Function Prototype
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Fundamental Programming
Pointers & Arrays.
Functions Imran Rashid CTO at ManiWeber Technologies.
Pointers & Functions.
The Stack.
CS150 Introduction to Computer Science 1
Methods and Data Passing
Presentation transcript:

PASSING PARAMETERS 1

2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within the function to perform a task Actual Parameters - parameters listed in the function call Actual data being passed to the function for use within the function

3 Parameter Passing (by Value) void print_nums (int num1, int num2, int num3); { cout << endl; cout << “Number 1 is : “ << num1 << endl; cout << “Number 2 is : “ << num2 << endl; cout << “Number 3 is : “ << num3 << endl; } Formal Parameters

4 Parameter Passing (by value) int main( ) { number_1 = 10; number_2 = 20; number_3 = 30; cout << “The Numbers are: “ << endl; print_nums ( number_1, number_2, number_3 ); cout << system(“Pause”); } Actual Parameters

5 Parameter Passing (by Value) void print_nums ( int num1, int num2, int num3) { Number_1 = 10; Number_2 = 20; Number_3 = 30; print_nums ( number_1, number_2, number_3 ); cout << system(“Pause”); } num1 num2 num3 number_1 number_2 number_ num1 20 num2 30 num3

6 Parameter Passing (by Value) The Numbers are: Number 1 is : 10 Number 2 is : 20 Number 3 is : 30 Screen

7 Parameter Passing (by Reference) void print_nums ( int num1, int num2, int& num3); { num3 = num1 * num2; cout << endl; cout << “Number 1 is :” << num1 << endl; cout << “Number 2 is :” << num2 << endl; cout << “Number 3 is :” << num3 << endl; } Variable Parameter

8 Parameter Passing (by Reference) int main( ) { number_1 = 10; number_2 = 20; number_3 = 30; cout << “The Numbers are: “ << endl; cout << number_1 << number_2 << number_3 << endl; print_nums ( number_1, number_2, number_3 ); cout << “The Numbers are: “ << endl; cout << number_1 << number_2 << number_3 << endl; cout << system(“Pause”); } Actual Parameters

9 Passing by Reference void print_nums ( int num1, int num2, int& num3) { number_1 = 10; number_2 = 20; number_3 := 30; print_nums ( number_1, number_2, number_3 ); } num1 num2 num3 10 number_1 20 number_2 30 number_ num1 20 num2 add 408 num3 address 10 number_1 20 number_2 200 number_3

10 Passing by Reference The Numbers are: Number 1 is : 10 Number 2 is : 20 Number 3 is : 200 The Numbers are: Screen