Week 12 Methods for passing actual parameters to formal parameters.

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 10 Pointers and Dynamic Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Pointers Pointer variables.
Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.
1 Procedural Programming Paradigm Stacks and Procedures.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
A pointer is the memory address of a variable. A memory address is a physical location within a system’s memory space. A pointer variable is variable used.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
CPSC 388 – Compiler Design and Construction Parameter Passing.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 7: User-Defined Functions II.
CIS 101: Computer Programming and Problem Solving Lecture 9 Usman Roshan Department of Computer Science NJIT.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
Values, variables and types © Allan C. Milne v
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
Programming Languages and Design Lecture 7 Subroutines and Control Abstraction Instructor: Li Ma Department of Computer Science Texas Southern University,
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Chapter 7: Pointers Basic concept of pointers Pointer declaration Pointer operator (& and *) Parameter passing by reference.
PASSING VALUE TO A FUNCTION # CALL BY VALUECALL BY VALUE # CALL BY REFERENCECALL BY REFERENCE STORAGE CLASS # AUTOAUTO # EXTERNALEXTERNAL # STATICSTATIC.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
FUNCTIONS (a) Value returning e.g. int main() ….…. return 0; (b) Void (returning) no return statements example To print this message **** ** Welcome.
C++ Pointers Review. Overview  What is a pointer  Why do I care?  What can be 'pointed to'?  Example.
What does the following code write to the monitor? #include int main( void ) { int a ; int *pa ; a = 77 ; pa = &a ; printf("a=%d *pa=%d\n", a, *pa ); system("pause");
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
+ Structures and Unions. + Introduction We have seen that arrays can be used to represent a group of data items that belong to the same type, such as.
Pointers *, &, array similarities, functions, sizeof.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
By Mr. Muhammad Pervez Akhtar
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Pointers  * symbol and & symbol  Pointer operations  Pointer.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Prepared by Andrew Jung. Accessing Pointer Data Pointer can be used to access the contents of an array Look at the following syntax: /* Declaration and.
Instructions for test_function
User-Written Functions
Chapter 7: User-Defined Functions II
Learning Objectives Pointers Pointer in function call
Pointers and Pointer-Based Strings
Java Review: Reference Types
Pointer.
Using local variable without initialization is an error.
Advanced Programming Behnam Hatami Fall 2017.
Basic notes on pointers in C
CSC 253 Lecture 8.
C Passing arrays to a Function
Chapter 10: Pointers 1.
CSC 253 Lecture 8.
Chapter 4 (part 2).
Lecture 18 Arrays and Pointer Arithmetic
Anatomy of a Function Part 3
Pointers Call-by-Reference CSCI 230
Parameter Passing in Java
Initializing variables
Pointers and Pointer-Based Strings
C Programming Pointers
A simple function.
Introduction to Pointers
C Parameter Passing.
Introduction to Pointers
Presentation transcript:

Week 12 Methods for passing actual parameters to formal parameters

People are more concerned about the value status of actual parameters before calling and after calling than other issues. In some situations we wish no changes taking place with the actual parameters after the calling manipulations; in other situations we wish the changes taking place after the calling manipulations, or the original values of the actual parameters would be substituted by the new values. In general there are three methods to pass actual parameters to formal parameters in the calling process, which have different effects on the value status of actual parameters. These methods are – call by passing values to formal variables. (values = actual parameters) – call by passing values to reference variables. (values = actual parameters) – call by passing addresses to pointer variables. (addresses = addresses of actual parameters)

Call by passing actual parameters to formal parameters

Call by passing actual parameters to formal reference parameters

Reference variables introduced in C++ only A reference variable is a new type of variable that is declared as a second variable relative to the first variable. The reference variable has the different name from the first variable but shares the same memory address with the first variable. The reference variable need to be declared, so the compiler knows which is the first variable and which the second (reference) variable. For example, int a=5; int &m=a; m is declared as a reference variable to the first variable a. Both m and a share the same address despite the different names. In nature a reference variable is the first variable except the different name. So m is 5 when a is assigned with 5.

Reference variables Any other declared variables, e.g., n, can be made “equal ” to a but n and a have different addresses. The following program illustrates the difference.

Reference’s changes leading to the first variable’s change

Mechanism of formal reference parameters’ change leading to actual parameters’ change From the discussion of the last subsection, we may see that if people want to swap two given values by calling a function with the swapping functionality, they may treat the given values as the first variables while the arguments as the second variables to accomplish this job. In the parameter passing process, both the first and second will be equalled and share the same address. As a result of this, when the defined function swaps the formal parameters (reference), the actual parameters (first variables) will be updated as well because they share the same memory addresses.

Call by passing actual parameter addresses to pointer variables of formal parameters the addresses are always with pointer variables. If we take the addresses of the given values as the actual parameters while the pointer variables as the formal parameters. In the calling process, let the pointer variables of the formal parameters point to the actual parameters. Now loot at what is happening. The addresses are used to hold the variable values. If the addresses are called, then the values in the addresses will be accessed. Therefore if the addresses (&a, &b) of the actual parameters are passed to the formal parameters (pointer variables) int *x, int *y, the stored values will be passed to *x, *y. The function will swap *x, *y; but the addresses &a, &b remain unchanged. The mechanism is illustrated in the following program.

Call by passing actual parameter addresses to pointer variables of formal parameters

Summary