Lecture 11 C Parameters Richard Gesick.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
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,
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs.
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
Parameters. Overview A Reminder Why Parameters are Needed How Parameters Work Value Parameters Reference Parameters Out Parameters.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
1 Workin’ with Pointas An exercise in destroying your computer.
CSE 1301 Lecture 6 Writing Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Lecture 08. Since all Java program activity occurs within a class, we have been using classes since the start of this lecture series. A class is a template.
Building java programs, chapter 3 Parameters, Methods and Objects.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Classes - Intermediate
OOP Basics Classes & Methods (c) IDMS/SQL News
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
CSE 501N Fall ‘09 03: Class Members 03 September 2009 Nick Leidenfrost.
Methods, classes, and Objects Dr. Jim Burns. Question  Which of the following access modifiers is the default modifier?  public  private  protected.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Information and Computer Sciences University of Hawaii, Manoa
User-Written Functions
Chapter 7 User-Defined Methods.
This technique is Called “Divide and Conquer”.
Lecture 4 D&D Chapter 5 Methods including scope and overloading Date.
Lecture 14 Writing Classes part 2 Richard Gesick.
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Lecture 11 B Methods and Data Passing
More Object Oriented Programming
Pass by Reference, const, readonly, struct
Chapter 3 Introduction to Classes, Objects Methods and Strings
User Defined Functions
Variables ICS2O.
6 Chapter Functions.
Group Status Project Status.
Learning Objectives Classes Constructors Principles of OOP
Building Java Programs
Classes and Objects.
Methods and Data Passing
Why did the programmer quit his job?
Building Java Programs
Object Oriented Programming Review
Scope of variables class scopeofvars {
Object Oriented Programming in java
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Java Programming Language
Submitted By : Veenu Saini Lecturer (IT)
In this class, we will cover:
Building Java Programs
Names of variables, functions, classes
Building Java Programs
Lecture 8-2: Object Behavior (Methods) and Constructors
Building Java Programs
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Methods/Functions.
Visibilities and Static-ness
Corresponds with Chapter 5
Methods and Data Passing
Object-Oriented Design AND CLASS PROPERTIES
Presentation transcript:

Lecture 11 C Parameters Richard Gesick

Overview A Reminder Why Parameters are Needed How Parameters Work Value Parameters Reference Parameters Out Parameters

Parameters (A Reminder) Functions cannot see each other’s variables (scope) Special variables used to “catch” data being passed This is the way functions have to communicate with each other Located between parentheses ( ) If no parameters are needed, leave the parentheses empty

Why Do We Need Parameters Functions are good Reuse Modularity Good design Scope is limited A variable declared in a function is only visible inside that function Using global variables is a bad idea So we need a way of passing data between functions

Parameter Example Defined: Invoked: private static void DoSomething(int x, double y, string name) Invoked: DoSomething(42, 3.14, “Bob Smith”); Notice above that there is a 1-1 matching, and the types must match too

Parameter Details By default, all primitive types are passed by value Remember primitive types are all non-class types (int, float, double, string, char, etc.) The function gets a copy of the variable So if you change the variable in the function, the original value in the caller will remain unchanged

By Value Example public static void IncreaseByTwo(int x) { x += 2; Console.WriteLine("The value of x is " + x); } static void Main(string[] args) int i; i = 4; Console.WriteLine("The value of i is " + i); IncreaseByTwo(i); Console.WriteLine("Now the value of i is " + i);

The Output The value of i is 4 The value of x is 6 Now the value of i is 4 The variable “i” is not changed because the function gets a copy of the value. Let’s see this traced out…

By Value Example MEMORY public static void IncreaseByTwo(int x) { x += 2; Console.WriteLine("The value of x is " + x); } static void Main(string[] args) int i; i = 4; Console.WriteLine("The value of i is " + i); IncreaseByTwo(i); Console.WriteLine("Now the value of i is " + i); Main i

By Value Example MEMORY public static void IncreaseByTwo(int x) { x += 2; Console.WriteLine("The value of x is " + x); } static void Main(string[] args) int i; i = 4; Console.WriteLine("The value of i is " + i); IncreaseByTwo(i); Console.WriteLine("Now the value of i is " + i); Main i 4

By Value Example OUTPUT The value of i is 4 MEMORY public static void IncreaseByTwo(int x) { x += 2; Console.WriteLine("The value of x is " + x); } static void Main(string[] args) int i; i = 4; Console.WriteLine("The value of i is " + i); IncreaseByTwo(i); Console.WriteLine("Now the value of i is " + i); Main i 4

By Value Example OUTPUT The value of i is 4 MEMORY public static void IncreaseByTwo(int x) { x += 2; Console.WriteLine("The value of x is " + x); } static void Main(string[] args) int i; i = 4; Console.WriteLine("The value of i is " + i); IncreaseByTwo(i); Console.WriteLine("Now the value of i is " + i); IBT x 4 Main i 4 The value in i is copied into the variable x

By Value Example OUTPUT The value of i is 4 MEMORY public static void IncreaseByTwo(int x) { x += 2; Console.WriteLine("The value of x is " + x); } static void Main(string[] args) int i; i = 4; Console.WriteLine("The value of i is " + i); IncreaseByTwo(i); Console.WriteLine("Now the value of i is " + i); IBT x 6 Main i 4 Waiting here

By Value Example OUTPUT The value of i is 4 The value of x is 6 MEMORY public static void IncreaseByTwo(int x) { x += 2; Console.WriteLine("The value of x is " + x); } static void Main(string[] args) int i; i = 4; Console.WriteLine("The value of i is " + i); IncreaseByTwo(i); Console.WriteLine("Now the value of i is " + i); IBT x 6 Main i 4 Waiting here

By Value Example OUTPUT The value of i is 4 The value of x is 6 Now the value of i is 4 MEMORY public static void IncreaseByTwo(int x) { x += 2; Console.WriteLine("The value of x is " + x); } static void Main(string[] args) int i; i = 4; Console.WriteLine("The value of i is " + i); IncreaseByTwo(i); Console.WriteLine("Now the value of i is " + i); IBT x 6 Main i 4

Ref Parameters We can alter how parameters are handled using the “ref” keyword This makes the parameter a “By Reference” parameter Now the parameter IS NOT a copy of the value; it refers to the original

By Reference Example public static void IncreaseByTwo(ref int x) { x += 2; Console.WriteLine("The value of x is " + x); } static void Main(string[] args) int i; i = 4; Console.WriteLine("The value of i is " + i); IncreaseByTwo(ref i); Console.WriteLine("Now the value of i is " + i);

The Output The value of i is 4 The value of x is 6 Now the value of i is 6 The variable “i” is changed because the function refers to the original variable i. Let’s see this traced out…

By Ref Example MEMORY public static void IncreaseByTwo(ref int x) { x += 2; Console.WriteLine("The value of x is " + x); } static void Main(string[] args) int i; i = 4; Console.WriteLine("The value of i is " + i); IncreaseByTwo(ref i); Console.WriteLine("Now the value of i is " + i); Main i

By Ref Example MEMORY public static void IncreaseByTwo(ref int x) { x += 2; Console.WriteLine("The value of x is " + x); } static void Main(string[] args) int i; i = 4; Console.WriteLine("The value of i is " + i); IncreaseByTwo(ref i); Console.WriteLine("Now the value of i is " + i); Main i 4

By Ref Example OUTPUT MEMORY The value of i is 4 public static void IncreaseByTwo(ref int x) { x += 2; Console.WriteLine("The value of x is " + x); } static void Main(string[] args) int i; i = 4; Console.WriteLine("The value of i is " + i); IncreaseByTwo(ref i); Console.WriteLine("Now the value of i is " + i); Main i 4

By Ref Example OUTPUT MEMORY The value of i is 4 IBT x R public static void IncreaseByTwo(ref int x) { x += 2; Console.WriteLine("The value of x is " + x); } static void Main(string[] args) int i; i = 4; Console.WriteLine("The value of i is " + i); IncreaseByTwo(ref i); Console.WriteLine("Now the value of i is " + i); Main i 4 x is a reference to i

By Ref Example OUTPUT MEMORY The value of i is 4 IBT x R public static void IncreaseByTwo(ref int x) { x += 2; Console.WriteLine("The value of x is " + x); } static void Main(string[] args) int i; i = 4; Console.WriteLine("The value of i is " + i); IncreaseByTwo(ref i); Console.WriteLine("Now the value of i is " + i); Main i 6 Waiting here

By Ref Example OUTPUT MEMORY The value of i is 4 The value of x is 6 IBT x R public static void IncreaseByTwo(ref int x) { x += 2; Console.WriteLine("The value of x is " + x); } static void Main(string[] args) int i; i = 4; Console.WriteLine("The value of i is " + i); IncreaseByTwo(ref i); Console.WriteLine("Now the value of i is " + i); Main i 6 Waiting here

By Ref Example OUTPUT MEMORY The value of i is 4 The value of x is 6 Now the value of i is 6 IBT x R public static void IncreaseByTwo(ref int x) { x += 2; Console.WriteLine("The value of x is " + x); } static void Main(string[] args) int i; i = 4; Console.WriteLine("The value of i is " + i); IncreaseByTwo(ref i); Console.WriteLine("Now the value of i is " + i); Main i 6

The Necessity of Ref Parameters What if a function needs to return a value when it is complete? Use the “return” statement and declare the function of a particular type Ex: public static int sum(int x, int y) { return x + y; } But what if the function should return more than one value? Create a collection of values to return and return the collection Use ref (by reference) parameters to allow multiple items to be updated/modified

Another By-Ref Example (notice we need to return three values, modifying their original values) public void RGBToGrey (ref byte red, ref byte green, ref byte blue) { byte average; average = (red + green + blue) / 3; red = average; green = average; blue = average; }

Out Parameters We’ve seen parameters that pass information into a function (via copying the value) We’ve seen parameters that pass information into a function and receive information out of the function (via changed values to a reference) What about having values come out of a function? No initial values passed in

Out Parameters Think of these similar to ref (by reference) parameters No initial value passed in It’s the job of the function to fill in a value for this type of parameter When the function ends, the calling method will retain the value stored in the parameter

Out Parameter Example public static void getUserInfo(out int age, out string name, out double gpa) { Console.Write(“What is your name? "); name = Console.Read(); Console.Write(“What is your age? "); age = int.Parse(Console.Read()); Console.Write(“What is your GPA? "); gpa = double.Parse(Console.Read()); } static void Main(string[] args) int user_age; string user_name; double user_gpa; getUserInfo(out user_age, out user_name, out user_gpa); // more code follows...

Out Parameter Example public static void getUserInfo(out int age, out string name, out double gpa) { Console.Write(“What is your name? "); name = Console.Read(); Console.Write(“What is your age? "); age = int.Parse(Console.Read()); Console.Write(“What is your GPA? "); gpa = double.Parse(Console.Read()); } static void Main(string[] args) int user_age; string user_name; double user_gpa; getUserInfo(out user_age, out user_name, out user_gpa); // more code follows...

Out Parameters “Output” Information Notice in the preceding example, the “age”, “name” and “gpa” variables must be filled in with values within the funtion After this is done, the calling method (Main) can make use of these values via the “user_age”, “user_name” and “user_gpa” variables How is this different from ref (by-reference) parameters? Notice we couldn’t make use of these parameters (read their values) until we placed some value into them So the statement Console.WriteLine("The user’s age is" + age); at the beginning of the function (first line of the function) would be illegal because this parameter does not have a value assigned to it yet

The Necessity of Out Parameters Like “ref” parameters, “out” parameters are useful for returning information out from the function Unlike “ref” parameters, “out” parameters don’t receive values from the caller No values passed in – the parameters are “empty” Use “out” parameters when the function is generating the values and returning them to the calling method Just use the function return type and the “return” statement if the function returns only one value Use “out” parameters when more than one value is to be generated and returned to the calling method

Conclusion You should have a better/stronger feel on how parameters work You should understand the difference between by-value, by-reference, and out parameters Given a section of code, you should be able to “trace through” what the computer would do (and what output would be generated)