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

Slides:



Advertisements
Similar presentations
Programming Methodology (1). Implementing Methods main.
Advertisements

Execute Blocks of Code Multiple Times Svetlin Nakov Telerik Corporation
Functions Function: The strength of C language is to define and use function. The strength of C language is that C function are easy to define and use.
Spring Semester 2013 Lecture 5
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
1) Scope a] Ada scope rules b] C scope rules 2) Parameter passing a] Ada parameter modes b) Parameter passing mechanisms COMP205 IMPERATIVE LANGUAGES 13.
Call By Address Parameters (Pointers) Chapter 5. Functions that “return” more than a single value What if we need more than one value to be returned from.
Method Parameters and Overloading. Topics The run-time stack Pass-by-value Pass-by-reference Method overloading Stub and driver methods.
Execute Blocks of Code Multiple Times Telerik Software Academy C# Fundamentals – Part 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,
Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters.
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Passing information through Parameters ( our setter methods or mutators) Formal Parameter – parameter in the method. public void SetA(int x){ (int x) is.
Functions ROBERT REAVES. Functions  Interface – the formal description of what a subprogram does and how we communicate with it  Encapsulation – Hiding.
Translate, Rotate, Matrix Pages Function Function Definition Calling a function Parameters Return type and return statement.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
Introduction to Computers and Programming Strings Professor: Evan Korth New York University.
Chapter 10 Implementing Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Semantics of Call and Return The subprogram call and return.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Macro & Function. Function consumes more time When a function is called, the copy of the arguments are passed to the parameters in the function. After.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
Parameters. Overview A Reminder Why Parameters are Needed How Parameters Work Value Parameters Reference Parameters Out Parameters.
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008.
Method Parameters and Overloading Version 1.0. Topics The run-time stack Pass-by-value Pass-by-reference Method overloading Stub and driver methods.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
L what are predefined functions? l what is? n function name n argument(s) n return value n function call n function invocation n nested function call l.
Week 8 - Friday.  What did we talk about last time?  Static methods.
1 Week 8 l Methods l Parameter passing Methods. 2 Using Methods l Methods are actions that an object can perform. l To use a method you invoke or call.
Creating and Using Class Methods. Definition Class Object.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
Classes - Intermediate
Methods.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
Testing It is much better to have a plan when testing your programs than it is to just randomly try values in a haphazard fashion. Testing Strategies:
Implementing Subprograms
User-Written Functions
Predefined Functions Revisited
Method Parameters and Overloading
Implementing Subprograms
Lecture 11 C Parameters Richard Gesick.
While Loop Design ENGI 1020 Fall 2018.
The this Reference The this reference allows an object to refer to itself That is, the this reference, used inside a method, refers to the object through.
6 Chapter Functions.
Classes, Encapsulation, Methods and Constructors (Continued)
Lecture 18 Arrays and Pointer Arithmetic
Parameter Passing in Java
CS2011 Introduction to Programming I Arrays (II)
Initializing variables
Why did the programmer quit his job?
Week 4 Lecture-2 Chapter 6 (Methods).
CS149D Elements of Computer Science
Predefined Functions Revisited
Functions Imran Rashid CTO at ManiWeber Technologies.
Corresponds with Chapter 5
Implementing Subprograms
C Parameter Passing.
Presentation transcript:

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.

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

static int add(int n1, int n2) { int sum = n1 + n2; return sum; } 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.

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

A complete program using the add( ) method It is common to put the code for the method following Main( ) 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; }

A complete program using the add( ) method 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. 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; }

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

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

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

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

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

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

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

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

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

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

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

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.

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

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

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.

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

static int add(int n1, int n2) { int sum = n1 + n2; return sum; } n1 n2 sum Inside of the Black Box 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; } n1 n2 sum Inside of the Black Box … and pass it back to the caller.

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

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.

add Get the result var1 var25 8 var3 Let’s Investigate what happens … 13

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

var1 var25 8 add var3 13 Let’s Investigate what happens … We’re Done!