Local Variables A local variable is a variable that is declared within a method declaration. Local variables are accessible only from the method in which.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

DONT PANIC!! Lots of new notions coming in these slides Dont worry if not all of it makes perfect sense Well meet most of this stuff again in detail later.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four Defining Your Own Classes.
Objects & Methods Defining Classes. Slide 2 Reference Variables Revisited Remember: Object variables are references (aka pointers) Point to “null” by.
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
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.
Chapter 7: User-Defined Functions II
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.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 7 More on Defining Classes Overloading.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four: Defining Your Own Classes *Instantiable.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 7 More on Defining Classes Overloading.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 7 Defining Your Own Classes Part 2.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 7 Defining Your Own Classes Part 2.
Chapter Four Defining Your Own Classes continued.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Why Programmer-Defined Classes Using just the String,
SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.
Runtime Environments Compiler Construction Chapter 7.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Methods F Hello World! F Java program compilation F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters by value F Overloading.
Parameters… Classes Cont Mrs. C. Furman October 13, 2008.
ECE122 Feb. 22, Any question on Vehicle sample code?
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
1 Chapter 5: Methods FIntroducing Methods FDeclaring Methods FCalling Methods FLocal Variables in Methods FPass by Value FOverloading Methods FMethod.
DCS 2133 Object Oriented Programming Defining Your Own Classes Part 2.
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Methods.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
Methods and Parameters. Slide 2 Internal “Convenience” Methods External Methods ways to respond to requests from external “users” e.g., deposit( int ),
Chapter 7- Defining Your Own Classes Part 2 : Objectives After you have read and studied this chapter, you should be able to –Describe how objects are.
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
Functions, Part 2 of 2 Topics Functions That Return a Value
Chapter 10: Void Functions
FIGURE 4-10 Function Return Statements
User-Defined Functions
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Methods and Parameters
Chapter 4 Defining Instantiable Classes
More Object Oriented Programming
FIGURE 4-10 Function Return Statements
User Defined Functions
Defining Your Own Classes
4-2 Functions in C In C, the idea of top–down design is done using functions. A C program is made of one or more functions, one and only one of which.
Variables and Their scope
Lecture 18 Arrays and Pointer Arithmetic
Functions, Part 2 of 3 Topics Functions That Return a Value
Introduction to Object-Oriented Programming with Java--Wu
FIGURE 4-10 Function Return Statements
Function “Inputs and Outputs”
Chapter 7: User-Defined Functions II
Chapter 9: Value-Returning Functions
FIGURE 4-10 Function Return Statements
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
Presentation transcript:

Local Variables A local variable is a variable that is declared within a method declaration. Local variables are accessible only from the method in which they are declared. Memory space for local variables are allocated only during the execution of the method. When the method execution completes, memory space will be deallocated. Grukkee … out to reality LocalVariables.java

Parameters The formal parameters defined in the method header are also local variables Formal parameters receive copies of the actual parameters passed in the call The values are copied in order, 1st to 1st, 2nd to 2nd, etc Ooodles … out to reality Parameters.java

Sample Method private static double fromDollar(double dollar) { double amount, fee; fee = exchangeRate - feeRate; amount= dollar * fee; return amount; } Parameter Local Variables

Memory Allocation for Local Variables - 1 Code State of Memory A A A. A. Local variables do not exist before the method execution At before fromDollar A A finalAmount = fromDollar(200); private static double fromDollar( double dollar) { double amount, rate; rate = EXCHANGE_RATE - FEE_RATE; amount = dollar * rate; return(amount); }

Memory Allocation for Local Variables - 2 Code B B State of Memory B. B. Memory space is allocated for the local variables and parameter. After is executed B B dollar amount rate finalAmount = fromDollar(200); private static double fromDollar( double dollar) { double amount, rate; rate = EXCHANGE_RATE - FEE_RATE; amount = dollar * rate; return(amount); }

Memory Allocation for Local Variables - 3 Code C C State of Memory C. C. Computed values are assigned to the local variables. After is executed C C dollar amount rate finalAmount = fromDollar(200); private static double fromDollar( double dollar) { double amount, rate; rate = EXCHANGE_RATE - FEE_RATE; amount = dollar * rate; return(amount); }

Memory Allocation for Local Variables - 4 Code D D State of Memory D. D. Memory space is deallocated upon exiting the fromDollar method. At after fromDollar D D finalAmount = fromDollar(200); private static double fromDollar( double dollar) { double amount, rate; rate = EXCHANGE_RATE - FEE_RATE; amount = dollar * rate; return(amount); }

Pass-By-Value Scheme - 1 State of Memory private static void myMethod(int one, float two) { one = 25; two = 35.4f; } A A A. A. Local variables do not exist before the method execution At before myMethod A A Code x = 10; y = 20; myMethod( x, y ); xx 10 y 20

Pass-By-Value Scheme - 2 State of Memory B. B. The values of arguments are copied to the parameters. Values are copied at B B Code x = 10; y = 20; myMethod( x, y ); B B xx 10 y 20 one 10 two f private static void myMethod(int one, float two) { one = 25; two = 35.4f; }

Pass-By-Value Scheme - 3 C C State of Memory C. C. The values of parameters are changed. After is executed C C Code x = 10; y = 20; myMethod( x, y ); xx 10 y 20 one 1025 two f private static void myMethod(int one, float two) { one = 25; two = 35.4f; }

Pass-By-Value Scheme - 4 Code D D State of Memory D. D. Parameters are erased. Arguments remain unchanged. At after myMethod D D x = 10; y = 20; myMethod( x, y ); xx 10 y 20 private static void myMethod(int one, float two) { one = 25; two = 35.4f; }

Actual Parameters Formal and actual parameters need not (but can) have the same name. Changes to the formal parameters have no effect in the calling code because they are local! Actual parameters need not be variables. Ack … out to reality NonVariable Parameters.java

Arguments & Parameters: Points to Remember 1. Arguments are passed to a method using the pass-by-value scheme. 2. Arguments are matched to the parameters from left to right. The data type of an argument must be assignment compatible to the data type of the matching parameter. 3. The number of arguments in the method call must match the number of parameters in the method definition. 4. Parameters and arguments do not have to have the same name. 5. Local copies, which are distinct from arguments, are created even if the parameters and arguments share the same name. 6. Parameters are input to a method, and they are local to the method. Changes made to the parameters will not affect the value of corresponding arguments.