Parameter passing mechanism: pass-by-value. Introduction In the last webpage, we discussed how to pass information to a method I have kept it (deliberately)

Slides:



Advertisements
Similar presentations
Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.
Advertisements

C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
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.
Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Numeric literals and named constants. Numeric literals Numeric literal: Example: A numeric literal is a constant value that appears in a Java program.
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Nested conditional statements. Previously discussed Conditional statements discussed so far: Syntax of the if-statement: if-statement if-else-statement.
Writing algorithms using the while-statement. Previously discussed Syntax of while-statement:
Writing algorithms using the for-statement. Programming example 1: find all divisors of a number We have seen a program using a while-statement to solve.
Macro & Function. Function consumes more time When a function is called, the copy of the arguments are passed to the parameters in the function. After.
Programming a computer. What does programming a computer mean ? Programming a computer: Since a computer can only execute machine instructions (encoded.
The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)
Shorthand operators.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
The character data type char
Advanced Functions Lecture 14: Supporting Material Dr Kathryn Merrick Tuesday 21 st April, 2009.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
The Rectangle Method. Introduction Definite integral (High School material): A definite integral a ∫ b f(x) dx is the integral of a function f(x) with.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
The dangling-else ambiguity. Previously discussed The Java compiler (translator) consider white space characters (i.e., SPACE, TAB and New line) as insignificant.
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
Floating point numerical information. Previously discussed Recall that: A byte is a memory cell consisting of 8 switches and can store a binary number.
What does a computer program look like: a general overview.
The scope of local variables. Murphy's Law The famous Murphy's Law says: Anything that can possibly go wrong, does. (Wikipedia page on Murphy's Law:
Boolean expressions, part 2: Logical operators. Previously discussed Recall that there are 2 types of operators that return a boolean result (true or.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Working with arrays (we will use an array of double as example)
Introduction to programming in the Java programming language.
Assignment statements using the same variable in LHS and RHS.
Chapter 7 Functions. Types of Functions Value returning Functions that return a value through the use of a return statement They allow statements such.
Mixing integer and floating point numbers in an arithmetic operation.
Introduction to Methods. Previously discussed There are similarities in make up of that can help you remember the construct of a class a class in the.
The Bisection Method. Introduction Bisection Method: Bisection Method = a numerical method in Mathematics to find a root of a given function.
The life time of local variables (in a method). Local variables Local variable: A local variable is used to store information that is relevant for the.
Integer numerical data types. The integer data types (multiple !) The integer data types use the binary number system as encoding method There are a number.
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.
The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained.
Arithmetic expressions containing Mathematical functions.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
User-Defined Functions II TK1914: C++ Programming.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Working with floating point expressions. Arithmetic expressions Using the arithmetic operators: and brackets (... ), we can construct arithmetic expression.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
Simple algorithms on an array - compute sum and min.
Week 12 Methods for passing actual parameters to formal parameters.
The ++ and -- expressions. The ++ and -- operators You guessed it: The ++ and -- are operators that return a value.
Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same.
C Part 2 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens The Three Attributes of an Identifier Identifiers have three essential.
User-Written Functions
Chapter 7: User-Defined Functions II
User-Defined Functions
The method invocation mechanism and the System Stack
The Boolean (logical) data type boolean
Lecture 18 Arrays and Pointer Arithmetic
Array and Method.
Chapter 7: User-Defined Functions II
The for-statement.
Local variables and how to recognize them
Presentation transcript:

Parameter passing mechanism: pass-by-value

Introduction In the last webpage, we discussed how to pass information to a method I have kept it (deliberately) simple by using constant values as parameters: In this webpage, we will discuss how to pass information stored in variables to a method. Specifically, we will study the pass-by-value mechanism r = ToolBox.min( 1.0, 4.0 );

Example: passing information stored in variables Consider the following program:

Example: passing information stored in variables (cont.) Question to ponder: There are quite a few ways to allow (enable) you to accomplish this "passing" The possible answers ranges from simple to pretty weird How can we pass (give) the information stored inside the variables x and y to the method ToolBox.min

Parameter passing mechanisms Definition: Parameter passing mechanism = agreement between the calling method and the called method on how a parameter is passed between them

Parameter passing mechanisms (cont.) Important note: Both the calling method and the called method must agree to use the same passing mechanism (or else, the information will be passed incorrectly)

Parameter passing mechanisms (cont.) Most commonly used parameter passing mechanisms: Pass-by-value The calling method passes the information stored inside a variable by passing (= copying) the value contained inside a variable into the parameter variable.

Parameter passing mechanisms (cont.) This is the most obvious way to pass information... Example: if you want to give you phone number of your home to someone, you make a copy of the information (in the parameter variable)

Parameter passing mechanisms (cont.) Pass-by-reference The calling method passes the information stored inside a variable by passing (= copying) the address (location) of a variable into the parameter variable.

Parameter passing mechanisms (cont.) This is a less obvious but more powerful way to pass information... Example: if you want to give you phone number of your home to someone, you make a copy of the address of your home (in the parameter variable) He/she can find the phone number by visiting that address !!!

Parameter passing mechanisms (cont.) Terminology: A reference in Computer Science is the location (or address) (of a variable or a method)

Terminology: formal parameters and actual parameters Definitions: Formal parameter = a parameter variable Actual parameter = a variable whose value is to be passed to some formal parameter

Terminology: formal parameters and actual parameters (cont.) Illustrated example:

Terminology: formal parameters and actual parameters (cont.) Explanation: The parameter variables a and b in the definition of the ToolBox.min method are formal parameters The variables x and y used in the method invocation ToolBox.min(x, y) are actual parameters

The Pass-by-value mechanism - the agreement Recall: Parameter passing mechanism = agreement between the calling method and the called method on how a parameter is passed between them

The Pass-by-value mechanism - the agreement (cont.) The agreement used in the Pass-by-value mechanism: For the calling method: The calling method creates the parameter variables for the called method,.... and The calling method copies the value of the actual parameter into the formal parameter

The Pass-by-value mechanism - the agreement (cont.) For the called method: The called method obtains the information directly from the parameter variables

The Pass-by-value mechanism - an example Example program:

The Pass-by-value mechanism - an example (cont.) When main starts running, it will first create its local variables:

The Pass-by-value mechanism - an example (cont.) When execution reaches the method call ToolBox.min(x,y), the Pass-by-value mechanism first creates the parameter variables:

The Pass-by-value mechanism - an example (cont.) Then the Pass-by-value mechanism copies the value of the actual parameter to the corresponding formal parameter:

The Pass-by-value mechanism - an example (cont.) The method invocation mechanism is completed as usually with the following steps: Save the return address on the stack:

The Pass-by-value mechanism - an example (cont.) Jump to the called method:

The Pass-by-value mechanism - an example (cont.) When the min method executes, it will create its local variable m:

The Pass-by-value mechanism - an example (cont.) Notice how the called method uses the parameter variables: When the called method uses a parameter variable, the information is obtained directly from the parameter variable:

A quiz on the Pass-by-value mechanism Consider the following program:

A quiz on the Pass-by-value mechanism (cont.) Questions: What value is printed by the statement System.out.println(x); ? What value is printed by the statement System.out.println(y); ? What value is printed by the statement System.out.println(r); ?

A quiz on the Pass-by-value mechanism (cont.) Example Program: (Demo above code) –Prog file: rogs/pass-by-value/quiz/MyProgram.java -Prog file: rogs/pass-by-value/quiz/ToolBox.java How to run the program: Right click on links and save in a scratch directory To compile: javac MyProgram.java To run: java MyProgram

A quiz on the Pass-by-value mechanism (cont.) Output of the program: Did you understand why the update statements "a = a + 1" and "b = b + 2" did not update the actual parameters x and y ??? 1.0 (the value in x is UNCHANGEDD !) 4.0 (the value in y is UNCHANGEDD !) 8.0 (= )

The quiz explained Notice the similarities between the ToolBox.min and the ToolBox.fun methods: public static double min ( double a, double b ) { double m = 0; if ( a < b ) { m = a; } else { m = b; } return(m); } public static double fun ( double a, double b ) { double m = 0; a = a + 1; b = b + 2; m = a + b; return(m); }

The quiz explained (cont.) Both methods have 2 parameter variables and 1 local variable I have constructed the quiz in such a way that I can re-use the diagrams from the Pass-by-value example above.

The quiz explained (cont.) So according to the Pass-by-value example above, when the ToolBox.min method starts running, the following variables have been created on the System Stack:

The quiz explained (cont.) Notice that: are different variables (they occupy different memory cells !) The local variables x and y in the main method and The parameter variables a and b in the fun method

The quiz explained (cont.) The assignment statements: will change the values of the parameter variables: a = a + 1; b = b + 2;

The quiz explained (cont.)

Notice that: The values in the actual parameters (x and y) are unchanged !!!

The quiz explained (cont.) That's why the statements System.out.println(x); ---> prints 1.0 System.out.println(y); ---> prints 4.0

The quiz with an additional twist... Now, consider the following program:

The quiz with an additional twist... (cont.) We use the same names for actual and formal parameters !!! Questions: What value is printed by the statement System.out.println(a); ? What value is printed by the statement System.out.println(b); ? What value is printed by the statement System.out.println(r); ?

The quiz with an additional twist... (cont.) Example Program: (Demo above code) –Prog file: rogs/pass-by-value/quiz2/MyProgram.java -Prog file: rogs/pass-by-value/quiz2/ToolBox.java How to run the program: Right click on links and save in a scratch directory To compile: javac MyProgram.java To run: java MyProgram

The quiz with an additional twist... (cont.) Output of the program: Did you understand why the update statements "a = a + 1" and "b = b + 2" (that updates the formal parameters) did not update the actual parameters a and b ??? 1.0 (the value in x is UNCHANGEDD !) 4.0 (the value in y is UNCHANGEDD !) 8.0 (= )

The quiz explained Recall that: Different method scopes are always disjoint scopes You can define different variables with the same name in disjoint scopes (See: 0/Syllabus/08/scope.html#disjoint)

The quiz explained (cont.) In other words: are different variables The local variables named a and b defined inside the main method and The parameter variables named a and b defined inside the fun method

The quiz explained (cont.) The following diagram shows the fact that there are 2 different variables with the same name created created on the System Stack:

The quiz explained (cont.) Notice that: are different variables --- it's possible because of the scopes are non-overlapping (furthermore, they use different memory cells !) The blue colored variables named a and b are inside the scope of the main method and The magenta colored variables named a and b are inside the scope of the fun method

The quiz explained (cont.) The assignment statements: will change the values of the parameter variables: a = a + 1; b = b + 2;

The quiz explained (cont.)

Notice that: The values in the actual parameters (a and b) are unchanged !!!

The quiz explained (cont.) That's why the statements System.out.println(a); ---> prints 1.0 System.out.println(b); ---> prints 4.0