Pass by Reference, const, readonly, struct

Slides:



Advertisements
Similar presentations
Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
Advertisements

Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
CS201 – Introduction to Computing – Sabancı University 1 First Midterm Exam l November 22, 2008, Saturday, 10:40 – 12:20, max 100 minutes l One A4 size.
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.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Introduction to arrays Data in economy size packages.
ECE122 L16: Class Relationships April 3, 2007 ECE 122 Engineering Problem Solving with Java Lecture 16 Class Relationships.
Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding.
Review of C++ Programming Part II Sheng-Fang Huang.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Topic 22 arrays - part 2 Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
CSIS 113A Lecture 8 Parameters.  Two methods of passing arguments as parameters  Call-by-value  ‘copy’ of value is passed  Call-by-reference  ‘address.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Reference parameters (6.2.3) We might need a function to return more than one value Find roots of a quadratic 2 return values. What are they? Get first.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Classes Methods and Properties. Introduction to Classes and Objects In object-oriented programming terminology, a class is defined as a kind of programmer-defined.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Review of Function Overloading Allows different functions to have the same name if they have different types or numbers of arguments, e.g. int sqr(int.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Struct s (7.4) Used as data aggregates for an entity can be different types of data e.g. for student id, name, GPA, address,... Similar to classes, but.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
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.
CHAPTER 07 Arrays and Vectors (part II). OBJECTIVES In this part you will learn:  To pass arrays to functions.  Basic searching techniques.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Pointers and Dynamic Arrays
C# for C++ Programmers 1.
Chapter 7 User-Defined Methods.
Building Java Programs
Methods Attributes Method Modifiers ‘static’
Pointers and Pointer-Based Strings
Pointers Revisited What is variable address, name, value?
Java Arrays. Array Object An array :a container object holds a fixed number of values of a single type. The length of an array is established when the.
Lecture 6 C++ Programming
Pointers and References
Templates.
More Object Oriented Programming
CSE 143 Lecture 9 References and Linked Nodes reading: 3.3; 16.1
Advanced Programming Chapter 8: Arrays
Reference parameters (6.2.3)
Building Java Programs Chapter 7
Building Java Programs
7 Arrays.
Classes & Objects: Examples
Defining methods and more arrays
Object Oriented Programming in java
Building Java Programs
Introduction to Programming
References, const and classes
Parameters and Overloading
Grouped Data Arrays, and Array Lists.
7 Arrays.
Pointers and Pointer-Based Strings
Suggested self-checks: Section 7.11 #1-11
Class.
CSS161: Fundamentals of Computing
Java Programming Language
Building Java Programs
Corresponds with Chapter 5
Presentation transcript:

Pass by Reference, const, readonly, struct

Pass by Reference Parameters The parameters we have seen so far are value parameters their arguments are passed by value if you change the value of a parameter in function, corresponding argument does NOT change in the caller function Let’s see an example: PassByRef.cs.

Pass by Reference (ref & out) Need to assign an initial value to the parameter out No need to assign an initial value to the parameter static void FunctionOutRef(ref int c, out int d) { d = c + 1; c = c + 1; } static void Main(string[] args) int num3 = 5, num4; FunctionOutRef(ref num3, out num4);

Underlying Mechanisms For value parameters, the arguments’ values are copied into parameters arguments and parameters have different memory locations double Average (int a, int b) Average(num1, num2) 10 15 copy value copy value main function 10 15

Underlying Mechanisms For reference parameters, the parameter and the argument share the same memory location parameter is an alias of argument double average (ref int a, int b) average(ref num1, num2) 15 refers to the same memory location copy value main function 10 15

Example: Swap Write a function that swaps the values of its two integer parameters

Example: Swap void swap(ref int a, ref int b) { int temp; temp = a; a = b; b = temp; } How do we use this in main? int a=5, b=8; swap(ref a, ref b); // a becomes 8, b becomes 5 swap(ref a, ref 5); // syntax error, arguments must be variables

Passing Arrays as Parameters To pass an array argument to a method, specify the name of the array without any brackets. For a method to receive an array reference through a method call, the method’s parameter list must specify an array parameter. When an argument to a method is an entire array or an individual array element of a reference type, the called method receives a copy of the reference. When an argument to a method is an individual array element of a value type, the called method receives a copy of the element’s value. To pass an individual array element to a method, use the indexed name of the array as an argument in the method call. Example: ArrayReferenceTest.cs

const Sometimes very useful provides self documentation re-use the same value across the class or program avoid accidental value changes Like variables, but their value is assigned at declaration and can never change afterwards declared by using const before the type name (any type is OK) public const double PI = 3.14159; const int LENGTH = 6; later you can use their value Console.WriteLine(MathClass.PI * 4 * 4); but cannot change their value MathClass.PI = 3.14; causes a syntax error

const Cannot use const with reference types. Exception: string public const string CLASSNAME = “IT 528”;

readonly const is set at compile time readonly is set at runtime readonly can be used for reference types as well Constructor can set readonly fields but not any other methods public class GradeBook { private readonly string courseName = “”; public GradeBook(string name) courseName = name; }

struct (structure) struct is very similar to a class Like classes, struct’s can have methods and properties with access modifiers. Struct members are accessed via (.) operator similarly. So they are almost the same with one difference: Class is a reference type whereas struct is a value type. Example: mystruct.cs