Difference between revisions of "Passing Arguments using Methods"

From CompSciWiki
Jump to: navigation, search
(Inserted codeblock templates all previous changes were cleaning up piplines)
m (Fixed a typo in argument values example)
 
(3 intermediate revisions by 3 users not shown)
Line 3: Line 3:
 
|Body=
 
|Body=
 
|Chapter_TOC=[[User-Defined_Methods|User-Defined Methods]]
 
|Chapter_TOC=[[User-Defined_Methods|User-Defined Methods]]
|Introduction=When you call a method that has parameters, you must pass values to the method. This section explains the ways in which you may pass values in a method call.
 
|Overview=This chapter explains what an argument is, how arguments relate to parameters, and the types of values that may be used as arguments.
 
}}
 
  
=What is an Argument?=
+
==Introduction==
 +
When you call a method that has parameters, you must pass values to the method. This section explains the ways in which you may pass values in a method call.
 +
 
 +
==What is an Argument?==
 
An '''argument''' is a value passed to a method's parameter during a method call.
 
An '''argument''' is a value passed to a method's parameter during a method call.
  
Line 21: Line 21:
 
     * @return int sum - The sum of x and y.
 
     * @return int sum - The sum of x and y.
 
     */
 
     */
     public static int ''add''(int x, int y)
+
     public static int add(int x, int y)
 
     {
 
     {
 
         int sum;
 
         int sum;
Line 41: Line 41:
 
In the above example, the values ''16'' and ''28'' are '''arguments'''.
 
In the above example, the values ''16'' and ''28'' are '''arguments'''.
  
=Argument Values=
+
==Argument Values==
  
 
Three types of values may be used as arguments:
 
Three types of values may be used as arguments:
Line 48: Line 48:
 
#Variables (the variable must be the same type as the parameter)
 
#Variables (the variable must be the same type as the parameter)
 
#Other method calls (the return value of the passed method call must be the same type as the parameter)
 
#Other method calls (the return value of the passed method call must be the same type as the parameter)
 +
 +
The following example uses all three types of values to call a method.
 +
{{CodeBlock
 +
|Code=
 +
public static void main(String[] args) {
 +
  String s;
 +
  int x;
 +
 +
  s = "Peace out";
 +
  x = 2;
 +
 +
  printValues("Goodbye", 1);            //This call to printValues uses literal values
 +
 +
  printValues(s, x);                    //This call to printValues uses variables
 +
 
 +
  printValues(getString(), getInt());  //This call to printValues uses other method calls
 +
 +
}
 +
 +
public static void printValues(String s, int x) {
 +
  System.out.println("String: " + s);
 +
  System.out.println("int: " + x);
 +
}
 +
 +
public static String getString() {
 +
  return "See you later";
 +
}
 +
 +
public static int getInt() {
 +
  int x = 3;
 +
  return x;
 +
}
 +
}}
 +
 +
Something else to note from the above example is the return statements from getString() and getInt(). Remember that just like you can send a method literal values, variables, or other method calls, you can return any of these from a method as well.
 +
 +
==Nested Method Calls==
  
 
Passing a method call as an argument to another method call creates a '''nested method call'''. For example, we can nest calls to the ''add'' method in this way:
 
Passing a method call as an argument to another method call creates a '''nested method call'''. For example, we can nest calls to the ''add'' method in this way:
Line 62: Line 99:
 
     * @return int sum - The sum of x and y.
 
     * @return int sum - The sum of x and y.
 
     */
 
     */
     public static int ''add''(int x, int y)
+
     public static int add(int x, int y)
 
     {
 
     {
 
         int sum;
 
         int sum;
Line 74: Line 111:
 
   
 
   
 
         // This is our nested method call
 
         // This is our nested method call
         result = ''add''(8, '''''add''(2, 3)''');
+
         result = add(8, '''''add''(2, 3)''');
 
         System.out.println(result);
 
         System.out.println(result);
 
   
 
   
         result = ''add''(8, 5);
+
         result = add(8, 5);
 
         System.out.println(result);
 
         System.out.println(result);
 
     }
 
     }
Line 91: Line 128:
 
}}
 
}}
  
 
+
==Order of Arguments==
=Order of Arguments=
+
  
 
Arguments must be passed to a method call in the same order that the parameters are declared. The following program demonstrates a method call which requires two different argument types:
 
Arguments must be passed to a method call in the same order that the parameters are declared. The following program demonstrates a method call which requires two different argument types:
Line 108: Line 144:
 
     * @return void
 
     * @return void
 
     */
 
     */
     public static void ''printLines''('''String line''', '''int numLines''')
+
     public static void printLines(String line, int numLines)
 
     {
 
     {
 
         for(int i = 1; i <= numLines; i++)
 
         for(int i = 1; i <= numLines; i++)
Line 118: Line 154:
 
     public static void main(String[] args)
 
     public static void main(String[] args)
 
     {
 
     {
         ''printLines''('''"Just a short line."''', '''5''');
+
         printLines("Just a short line.", 5);
 
     }
 
     }
 
  }
 
  }
Line 136: Line 172:
 
The ''printLines'' method requires a ''String'' argument and an ''int'' argument. The arguments are called in the order which the method header declares the parameters. Changing the call to ''printLines'' in the following way will result in a compile-time error:
 
The ''printLines'' method requires a ''String'' argument and an ''int'' argument. The arguments are called in the order which the method header declares the parameters. Changing the call to ''printLines'' in the following way will result in a compile-time error:
  
{{CodeBlock
+
  <code>printLines(5, "Just a short line."); // Error! Program will not compile.</code>
|Code=
+
 
        ''printLines''('''5''', '''"Just a short line."'''); // Error! Program will not compile.
+
}}
+
  
 
This will result in an error because the ''printLines'' method is expecting a ''String'' then an ''int'', not an ''int'' followed by a ''String''.
 
This will result in an error because the ''printLines'' method is expecting a ''String'' then an ''int'', not an ''int'' followed by a ''String''.
 +
}}

Latest revision as of 14:12, 8 December 2011

COMP 1010 Home > User-Defined Methods

Introduction

When you call a method that has parameters, you must pass values to the method. This section explains the ways in which you may pass values in a method call.

What is an Argument?

An argument is a value passed to a method's parameter during a method call.

For example, consider the following method:

 /**
     * This method returns the sum of two integers.
     *
     * @param x, y - The integers to add.
     *
     * @return int sum - The sum of x and y.
     */
     public static int add(int x, int y)
     {
         int sum;
         sum = x + y;
         return sum;
     } 

When you call the above method, you need to pass two int values to the method. Each value is an argument.

 public static void main(String[] args)
    {
        int result = add(16, 28);
    } 

In the above example, the values 16 and 28 are arguments.

Argument Values

Three types of values may be used as arguments:

  1. Literal values, such as the String "Hello world!" or the double 52.8
  2. Variables (the variable must be the same type as the parameter)
  3. Other method calls (the return value of the passed method call must be the same type as the parameter)

The following example uses all three types of values to call a method.

 public static void main(String[] args) {
  String s;
  int x;

  s = "Peace out";
  x = 2;

  printValues("Goodbye", 1);            //This call to printValues uses literal values

  printValues(s, x);                    //This call to printValues uses variables
  
  printValues(getString(), getInt());   //This call to printValues uses other method calls

}

public static void printValues(String s, int x) {
  System.out.println("String: " + s);
  System.out.println("int: " + x);
}

public static String getString() {
  return "See you later";
}

public static int getInt() {
  int x = 3;
  return x;
} 

Something else to note from the above example is the return statements from getString() and getInt(). Remember that just like you can send a method literal values, variables, or other method calls, you can return any of these from a method as well.

Nested Method Calls

Passing a method call as an argument to another method call creates a nested method call. For example, we can nest calls to the add method in this way:

 public class NestedAdd
 {
    /**
     * This method returns the sum of two integers.
     *
     * @param x, y - The integers to add.
     *
     * @return int sum - The sum of x and y.
     */
     public static int add(int x, int y)
     {
         int sum;
         sum = x + y;
         return sum;
     } 
 
     public static void main(String[] args)
     {
         int result;
 
         // This is our nested method call
         result = add(8, '''''add''(2, 3)''');
         System.out.println(result);
 
         result = add(8, 5);
         System.out.println(result);
     }
 } 

In this example, the result of add(2, 3) is used as an argument for another call to the add method. The output of the program is:

 13
13 

Order of Arguments

Arguments must be passed to a method call in the same order that the parameters are declared. The following program demonstrates a method call which requires two different argument types:

 public class ArgumentOrder
 {
    /**
     * This method repeatedly prints a line.
     *
     * @param String line - The String to print.
     * @param int numLines - The number of times to print the line.
     *
     * @return void
     */
     public static void printLines(String line, int numLines)
     {
         for(int i = 1; i <= numLines; i++)
         {
             System.out.println(i + " " + line);
         }
     }
 
     public static void main(String[] args)
     {
         printLines("Just a short line.", 5);
     }
 } 

The output will be:

 1 Just a short line.
2 Just a short line.
3 Just a short line.
4 Just a short line.
5 Just a short line. 

The printLines method requires a String argument and an int argument. The arguments are called in the order which the method header declares the parameters. Changing the call to printLines in the following way will result in a compile-time error:

  printLines(5, "Just a short line."); // Error! Program will not compile.


This will result in an error because the printLines method is expecting a String then an int, not an int followed by a String.