1 / 31

How do Methods Work?

How do Methods Work?. Let’s write a method that adds two integer values together and returns the result. This specifies the return type of the method. That is, this method will return an integer. Methods can only return one thing. static int add(int n1, int n2) { int sum = n1 + n2;

Télécharger la présentation

How do Methods Work?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. How do Methods Work?

  2. Let’s write a method that adds two integer values together and returns the result.

  3. This specifies the return type of the method. That is, this method will return an integer. Methods can only return one thing. static int add(int n1, int n2) { int sum = n1 + n2; return sum; }

  4. This is the name of the method. static int add(int n1, int n2) { int sum = n1 + n2; return sum; }

  5. These are the method’s parameters. A method can have any number of parameters, including none. The Parameters define the variables passed to the method when it is invoked. static int add(int n1, int n2) { int sum = n1 + n2; return sum; }

  6. 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; } }

  7. 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; } } It is common to put the code for the method following Main( )

  8. 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; } } This is the line of code that actually calls, or invokes, the method. Two values are passed to the method. The method returns a value which is then stored in var3.

  9. Looking at the method call in more detail … The values of 8 and 5 get passed to the method.. int var1 = 8; int var2 = 5; int var3 = add(var1, var2);

  10. Let’s Investigate what happens … 8 var1 5 var2 var3 add We will treat the method as a black box. It is important to note that we can’t see inside.

  11. Let’s Investigate what happens … 8 var1 5 var2 var3 add We know what a method does by reading it’s prologue.

  12. Let’s Investigate what happens … 8 var1 5 var2 var3 add Make a copy of the variables. We don’t want the add method to change the original variables.

  13. 8 Let’s Investigate what happens … 8 var1 5 var2 var3 add Make a copy of the variables.

  14. 8 Let’s Investigate what happens … 8 var1 5 var2 var3 add Make a copy of the variables.

  15. 8 5 Let’s Investigate what happens … 8 var1 5 var2 var3 add Make a copy of the variables.

  16. 8 5 Let’s Investigate what happens … 8 var1 5 var2 var3 add Pass the copies of the variables to the method

  17. 8 Let’s Investigate what happens … 8 var1 5 var2 var3 add Pass the copies of the variables to the method

  18. Inside of the Black Box These are the method’s formal parameters. static int add(int n1, int n2) { int sum = n1 + n2; return sum; } Notice that when inside of the box, we can’t see out.

  19. Inside of the Black Box These variables are local to the method. They only exist inside of the box. n1 static int add(int n1, int n2) { int sum = n1 + n2; return sum; } n2 sum

  20. 5 Inside of the Black Box Here comes the first value passed to the method. n1 static int add(int n1, int n2) { int sum = n1 + n2; return sum; } n2 sum

  21. 5 Inside of the Black Box It gets stored in the local variable n1. 5 n1 static int add(int n1, int n2) { int sum = n1 + n2; return sum; } n2 sum

  22. 8 Inside of the Black Box Here comes the second value passed to the method. 5 n1 static int add(int n1, int n2) { int sum = n1 + n2; return sum; } n2 sum

  23. 8 Inside of the Black Box It gets stored in the local variable n2. 5 n1 static int add(int n1, int n2) { int sum = n1 + n2; return sum; } 8 n2 sum

  24. Inside of the Black Box 5 n1 static int add(int n1, int n2) { int sum = n1 + n2; return sum; } 8 n2 Now the code of the method is executed. 13 sum

  25. 13 Inside of the Black Box 5 n1 We need to pass sum back to the point where the method was called. So, we make a copy of sum. static int add(int n1, int n2) { int sum = n1 + n2; return sum; } 8 n2 13 sum

  26. 13 Inside of the Black Box … and pass it back to the caller. 5 n1 static int add(int n1, int n2) { int sum = n1 + n2; return sum; } 8 n2 13 sum

  27. 13 Inside of the Black Box … and pass it back to the caller. 5 n1 static int add(int n1, int n2) { int sum = n1 + n2; return sum; } 8 n2 13 sum

  28. Looking at the method call in more detail … int var1 = 8; int var2 = 5; int var3 = add(var1, var2); The method returns the value of 13 right here. The value is assigned to var3.

  29. 13 Let’s Investigate what happens … 8 var1 5 var2 var3 add Get the result

  30. 13 Let’s Investigate what happens … 8 var1 5 var2 var3 add Store the returned value in the variable that it was assigned to.

  31. Let’s Investigate what happens … 8 var1 5 var2 var3 13 add We’re Done!

More Related