Parameters - PowerPoint PPT Presentation

1 / 35
About This Presentation
Title:

Parameters

Description:

choice = MessageBox.Show('Do you want to exit the program?', ' Quit? ... average = (red green blue) / 3; red = average; green = average; blue = average; ... – PowerPoint PPT presentation

Number of Views:16
Avg rating:3.0/5.0
Slides: 36
Provided by: jonapr
Category:
Tags: green | parameters | red | show

less

Transcript and Presenter's Notes

Title: Parameters


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

3
Youve Seen Parameters
  • Defined
  • private void buttonAdd_Click(object sender,
    EventArgs e)
  • private void FormMain_Load(object sender,
    EventArgs e)
  • private void buttonAddDigit_Click(object sender,
    EventArgs e)
  • Invoked
  • listBoxNames.Items.Add(textBoxInput.Text)
  • choice MessageBox.Show("Do you want to exit the
    program?", "Quit?", MessageBoxButtons.YesNo,
    MessageBoxIcon.Question,
    MessageBoxDefaultButton.Button2)

4
Parameters (A Reminder)
  • Functions cannot see each others 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

5
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

6
Parameter Example
  • Defined
  • 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

7
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

8
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)

9
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.
  • Lets see this traced out

10
By Value Example
MEMORY
Main
0
i
  • 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)

11
By Value Example
MEMORY
Main
4
i
  • 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)

12
By Value Example
OUTPUT
The value of i is 4
MEMORY
Main
4
i
  • 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)

13
By Value Example
OUTPUT
The value of i is 4
MEMORY
IBT
4
x
Main
4
i
  • 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 value in i is copied into the variable x
14
By Value Example
OUTPUT
The value of i is 4
MEMORY
IBT
6
x
Main
4
i
  • 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)

Waiting here
15
By Value Example
OUTPUT
The value of i is 4 The value of x is 6
MEMORY
IBT
6
x
Main
4
i
  • 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)

Waiting here
16
By Value Example
OUTPUT
The value of i is 4 The value of x is 6 Now the
value of i is 4
MEMORY
IBT
6
x
Main
4
i
  • 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)

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

18
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)

19
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.
  • Lets see this traced out

20
By Ref Example
MEMORY
Main
0
i
  • 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)

21
By Ref Example
MEMORY
Main
4
i
  • 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)

22
By Ref Example
OUTPUT
MEMORY
The value of i is 4
Main
4
i
  • 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)

23
By Ref Example
OUTPUT
MEMORY
The value of i is 4
IBT
R
x
Main
4
i
  • 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)

x is a reference to i
24
By Ref Example
OUTPUT
MEMORY
The value of i is 4
IBT
R
x
Main
6
i
  • 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)

Waiting here
25
By Ref Example
OUTPUT
MEMORY
The value of i is 4 The value of x is 6
IBT
R
x
Main
6
i
  • 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)

Waiting here
26
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
R
x
Main
6
i
  • 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)

27
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

28
Another By-Ref Example(notice we need to return
three values, modifying their original values)
  • static 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

29
Out Parameters
  • Weve seen parameters that pass information into
    a function (via copying the value)
  • Weve 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

30
Out Parameters
  • Think of these similar to ref (by reference)
    parameters
  • No initial value passed in
  • Its 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

31
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 Int32.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...

32
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 Int32.Parse(Console.Read())
  • Console.Write(What is your GPA? ")
  • age 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...

33
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 couldnt make use of these parameters
    (read their values) until we placed some value
    into them
  • So the statementConsole.WriteLine("The users
    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

34
The Necessity of Out Parameters
  • Like ref parameters, out parameters are
    useful for returning information out from the
    function
  • Unlike ref parameters, out parameters dont
    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

35
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)
Write a Comment
User Comments (0)
About PowerShow.com