how do methods work?. let’s write a method that adds two integer values together and returns the...

31
How do Methods Work?

Upload: ramiro-scull

Post on 14-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: How do Methods Work?. Let’s write a method that adds two integer values together and returns the result

How do Methods Work?

Page 2: How do Methods Work?. Let’s write a method that adds two integer values together and returns the result

Let’s write a method that adds twointeger values together and returnsthe result.

Page 3: How do Methods Work?. Let’s write a method that adds two integer values together and returns the result

static int add(int n1, int n2){ int sum = n1 + n2; return sum;}

This specifies the return type of the method.That is, this method will return an integer.Methods can only return one thing.

Page 4: How do Methods Work?. Let’s write a method that adds two integer values together and returns the result

static int add(int n1, int n2){ int sum = n1 + n2; return sum;}

This is the name of the method.

Page 5: How do Methods Work?. Let’s write a method that adds two integer values together and returns the result

static int add(int n1, int n2){ int sum = n1 + n2; return sum;}

These are the method’s parameters. A method canhave any number of parameters, including none. TheParameters define the variables passed to the methodwhen it is invoked.

Page 6: How do Methods Work?. Let’s write a method that adds two integer values together and returns the result

A complete program using the add( ) method

using System;

class Program{ static void Main() { int var1 = 8; int var2 = 5; int var3 = add(var1, var2); Console.WriteLine(var3); Console.ReadLine(); }

// the add method // purpose: adds two integers // parameters: the integers to add together // returns: the sum static int add(int n1, int n2) { int sum = n1 + n2; return sum; }}

Page 7: How do Methods Work?. Let’s write a method that adds two integer values together and returns the result

A complete program using the add( ) method

It is common to put thecode for the method followingMain( )

using System;

class Program{ static void Main() { int var1 = 8; int var2 = 5; int var3 = add(var1, var2); Console.WriteLine(var3); Console.ReadLine(); }

// the add method // purpose: adds two integers // parameters: the integers to add together // returns: the sum static int add(int n1, int n2) { int sum = n1 + n2; return sum; }}

Page 8: How do Methods Work?. Let’s write a method that adds two integer values together and returns the result

A complete program using the add( ) method

This is the line of code that actuallycalls, or invokes, the method.Two values are passed to the method.The method returns a value which isthen stored in var3.

using System;

class Program{ static void Main() { int var1 = 8; int var2 = 5; int var3 = add(var1, var2); Console.WriteLine(var3); Console.ReadLine(); }

// the add method // purpose: adds two integers // parameters: the integers to add together // returns: the sum static int add(int n1, int n2) { int sum = n1 + n2; return sum; }}

Page 9: How do Methods Work?. Let’s write a method that adds two integer values together and returns the result

int var1 = 8;int var2 = 5;int var3 = add(var1, var2);

Looking at the method call in more detail …

The values of 8 and 5 get passed to the method..

Page 10: How do Methods Work?. Let’s write a method that adds two integer values together and returns the result

var1

var25

8

add

Let’s Investigate what happens …

We will treat the method as a black box. It isimportant to note that we can’t see inside.

var3

Page 11: How do Methods Work?. Let’s write a method that adds two integer values together and returns the result

var1

var25

8

add

Let’s Investigate what happens …

We know what a method does by reading it’s prologue.

var3

Page 12: How do Methods Work?. Let’s write a method that adds two integer values together and returns the result

var1

var25

8

Make a copy of the variables. We don’t want theadd method to change the original variables.

add

var3

Let’s Investigate what happens …

Page 13: How do Methods Work?. Let’s write a method that adds two integer values together and returns the result

var1

var25

8

Make a copy of the variables.

add

8var3

Let’s Investigate what happens …

Page 14: How do Methods Work?. Let’s write a method that adds two integer values together and returns the result

var1

var25

8

Make a copy of the variables.

8

add

var3

Let’s Investigate what happens …

Page 15: How do Methods Work?. Let’s write a method that adds two integer values together and returns the result

var1

var25

8

Make a copy of the variables.

8add

var35

Let’s Investigate what happens …

Page 16: How do Methods Work?. Let’s write a method that adds two integer values together and returns the result

5

add

Pass the copies of the variables to the method

var1

var25

8

var3 8

Let’s Investigate what happens …

Page 17: How do Methods Work?. Let’s write a method that adds two integer values together and returns the result

5

add

Pass the copies of the variables to the method

var1

var25

8

var3

Let’s Investigate what happens …

Page 18: How do Methods Work?. Let’s write a method that adds two integer values together and returns the result

Inside of the Black Box

static int add(int n1, int n2){ int sum = n1 + n2; return sum;}

These are the method’sformal parameters.

Notice that when inside of the box,we can’t see out.

Page 19: How do Methods Work?. Let’s write a method that adds two integer values together and returns the result

static int add(int n1, int n2){ int sum = n1 + n2; return sum;}

n1

n2

sum

These variables are local to themethod. They only exist insideof the box.

Inside of the Black Box

Page 20: How do Methods Work?. Let’s write a method that adds two integer values together and returns the result

static int add(int n1, int n2){ int sum = n1 + n2; return sum;}

n1

n2

sum

5

Inside of the Black Box

Here comes the first value passed to the method.

Page 21: How do Methods Work?. Let’s write a method that adds two integer values together and returns the result

Inside of the Black Box

static int add(int n1, int n2){ int sum = n1 + n2; return sum;}

n1

n2

sum

8

It gets stored in the local variable n1.

8

Page 22: How do Methods Work?. Let’s write a method that adds two integer values together and returns the result

static int add(int n1, int n2){ int sum = n1 + n2; return sum;}

n1

n2

sum

5

8

Here comes the second value passed to the method.

Inside of the Black Box

Page 23: How do Methods Work?. Let’s write a method that adds two integer values together and returns the result

static int add(int n1, int n2){ int sum = n1 + n2; return sum;}

n1

n2

sum

8

5

Inside of the Black Box

5

It gets stored in the local variable n2.

Page 24: How do Methods Work?. Let’s write a method that adds two integer values together and returns the result

static int add(int n1, int n2){ int sum = n1 + n2; return sum;}

n1

n2

sum

8

5

13

Inside of the Black Box

Now the code of the method is executed.

Page 25: How do Methods Work?. Let’s write a method that adds two integer values together and returns the result

static int add(int n1, int n2){ int sum = n1 + n2; return sum;}

n1

n2

sum

8

5

1313

Inside of the Black Box

We need to pass sum back to the point where themethod was called. So, we make a copy of sum.

Page 26: How do Methods Work?. Let’s write a method that adds two integer values together and returns the result

static int add(int n1, int n2){ int sum = n1 + n2; return sum;}

n1

n2

sum

5

8

1313

Inside of the Black Box

… and pass it back to the caller.

Page 27: How do Methods Work?. Let’s write a method that adds two integer values together and returns the result

static int add(int n1, int n2){ int sum = n1 + n2; return sum;}

n1

n2

sum

5

8

13

13

Inside of the Black Box

… and pass it back to the caller.

Page 28: How do Methods Work?. Let’s write a method that adds two integer values together and returns the result

int var1 = 8;int var2 = 5;int var3 = add(var1, var2);

Looking at the method call in more detail …

The method returns the value of 13 right here.The value is assigned to var3.

Page 29: How do Methods Work?. Let’s write a method that adds two integer values together and returns the result

add

Get the result

var1

var25

8

var3

Let’s Investigate what happens …

13

Page 30: How do Methods Work?. Let’s write a method that adds two integer values together and returns the result

var1

var25

8

Store the returned value in the variable that it was assigned to.

add

var3

13

Let’s Investigate what happens …

Page 31: How do Methods Work?. Let’s write a method that adds two integer values together and returns the result

var1

var25

8

add

var313

Let’s Investigate what happens …

We’re Done!