Difference between revisions of "Calling a User-Defined Method"

From CompSciWiki
Jump to: navigation, search
(Method Calling)
m (code block not properly formatted)
 
(17 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 +
{{
 +
1010Topic
 +
|Body=
 +
|Chapter_TOC=[[User-Defined_Methods]]
 +
 +
==Introduction==
 +
Now that you've written your first method, this section will show you how to use your method. Executing a method is known as '''calling''' the method. A method call consists of two methods: the method which performs the call, and the method which is called.
 +
 
==Method Calling==
 
==Method Calling==
  
 
Two methods are involved in every method call:
 
Two methods are involved in every method call:
  
* '''Called method''' - the method to execute
+
*The '''Called method''' is the method to execute.
* '''Calling method''' - the method that does the calling
+
*The '''Calling method''' or '''caller method''' is the method that requests execution of the called method.
  
 
To call a method:
 
To call a method:
  
#Enter the name of the class containing the method, if the calling method is from a different class.
+
#Enter the name of the method followed by a left parenthesis '''('''.
#Enter the name of the method followed by the character '''('''.
+
 
#For each parameter in the called method, enter a variable or value of the same type as the parameter.
 
#For each parameter in the called method, enter a variable or value of the same type as the parameter.
#Enter the character ''')'''.
+
#Close the method call using a right parenthesis ''')''' and a semicolon ''';'''.
  
 
'''NOTE''': If the called method has no parameters, you will still need to follow the method name with parentheses.
 
'''NOTE''': If the called method has no parameters, you will still need to follow the method name with parentheses.
 
If the called method has a return value, make sure to call the method in a place where the return value is used. Usually you will save the return value in a variable, then use the variable later.
 
  
 
Example:
 
Example:
 +
{{CodeBlock
 +
|Code=
 +
methodName();
 +
}}
  
<pre>
 
int sum;
 
  
// The sum of firstNum and secNum will be saved in the sum variable
+
If the called method has a return value make sure to call the method in a place where the return value is used. Usually you will save the return value in a variable.
sum = calculateSum(firstNum, secNum);
+
The program will still work and compile if you don't store the returned value in a variable, so watch out!
System.out.println(sum);
+
</pre>
+
  
 
 
* If the called method and the calling method are in the '''same''' class, use the name of the method along with the parameters in parentheses.
 
 
Example: <pre>methodName(firstNum, secNum);</pre>
 
 
 
* If the called method is in a '''different''' class than the calling method, write the name of the class before the method name, separated by a period, then the parameters following.
 
  
 
Example:
 
Example:
  
<pre>OtherClass.methodName(firstNum, secNum);</pre>
+
{{CodeBlock
 +
|Code=
 +
int sum;
 +
 +
// The sum of firstNum and secNum will be saved in the sum variable
 +
sum = calculateSum(firstNum, secNum);
 +
System.out.println(sum);
 +
}}
  
When you attempt to call a method without stating the class name first, Java will search for the method in the same class that is being run. However, Java will not search other classes for the method, so you will need to tell Java which class contains the method if you did not declare the method in the running class.
+
In [[Calling_Methods|Chapter 4]], you have learned how to call [[String_Methods|string methods]], [[Math_Methods|math methods]], and [[JOptionPane_Methods|JOptionPane methods]]. These are methods that are prefixed with a '''class''' and a dot ('''.'''). Consider the following code which you may be familiar with:
  
 +
{{CodeBlock
 +
|Code=
 +
int firstValue = 10;
 +
int secondValue = 14;
 +
int maximum = Math.max(firstValue, secondValue);
 +
}}
  
* To call a method which has no parameters, simply follow the method name with empty parentheses.
+
In this statement you are telling Java to call the '''max''' method in the '''Math''' class. Do not worry about what classes are as they is not covered in this course.
  
Example:
+
==Method Calling Code Example==
<pre>methodName()</pre>
+
  
 +
{{CodeBlock
 +
|Code=
 +
public class myClass
 +
{
 +
 +
public static void main(String[] args)
 +
{
 +
int first = 23;
 +
int second = 68;
 +
int great;
 +
 +
great = getGreaterInt(first, second); // Method call here
 +
System.out.println("Greater value of " + first + " and " + second + ": " + great);
 +
}
 +
 
 +
 
 +
public static int getGreaterInt( int value1, int value2 )
 +
{
 +
int biggest; //Declare a variable to hold the biggest int
 +
 +
if ( value1 > value )
 +
{
 +
biggest = value1;
 +
}
 +
else
 +
{
 +
biggest = value2;
 +
}
 +
 +
return biggest;
 +
}
 +
 
 +
}
 +
}}
  
Code Example:
+
Note the following line in the example above:
  
<pre>
+
{{CodeBlock
 +
|Code=
 +
great = getGreaterInt(first, second); // Method call here
 +
}}
  
public class myClass
+
This line contains a '''method call'''.
{
+
*The '''calling method''', or '''caller method''', is the ''main'' method.
 
+
*The '''called method''' is ''getGreaterInt''.
public static void main(String[] args)
+
*The method ''getGreaterInt'' accepts two ''int'' parameters.
{
+
**In the method call two ''int'' values are passed to ''getGreaterInt''. These values are known as '''arguments'''. Method arguments are explained in the next section.
int first = 23;
+
}}
int second = 68;
+
int great;
+
 
+
great = getGreaterInt(first, second);
+
System.out.println("Greater value of " + first + " and " + second + ": " + great);
+
}
+
 
+
 
+
public static int getGreaterInt( int value1, int value2 )
+
{
+
int biggest; //Declare a variable to hold the biggest int
+
 
+
if ( value1 > value )
+
{
+
biggest = value1;
+
}
+
else
+
{
+
biggest = value2;
+
}
+
 
+
return biggest;
+
}
+
+
}
+
</pre>
+

Latest revision as of 02:48, 8 December 2011

COMP 1010 Home > User-Defined_Methods

Introduction

Now that you've written your first method, this section will show you how to use your method. Executing a method is known as calling the method. A method call consists of two methods: the method which performs the call, and the method which is called.

Method Calling

Two methods are involved in every method call:

  • The Called method is the method to execute.
  • The Calling method or caller method is the method that requests execution of the called method.

To call a method:

  1. Enter the name of the method followed by a left parenthesis (.
  2. For each parameter in the called method, enter a variable or value of the same type as the parameter.
  3. Close the method call using a right parenthesis ) and a semicolon ;.

NOTE: If the called method has no parameters, you will still need to follow the method name with parentheses.

Example:

 methodName(); 


If the called method has a return value make sure to call the method in a place where the return value is used. Usually you will save the return value in a variable. The program will still work and compile if you don't store the returned value in a variable, so watch out!


Example:

 int sum;
 
 // The sum of firstNum and secNum will be saved in the sum variable
 sum = calculateSum(firstNum, secNum);
 System.out.println(sum); 

In Chapter 4, you have learned how to call string methods, math methods, and JOptionPane methods. These are methods that are prefixed with a class and a dot (.). Consider the following code which you may be familiar with:

 int firstValue = 10;
 int secondValue = 14;
 int maximum = Math.max(firstValue, secondValue); 

In this statement you are telling Java to call the max method in the Math class. Do not worry about what classes are as they is not covered in this course.

Method Calling Code Example

 public class myClass 
 {
 
 	public static void main(String[] args)
 	{
 		int first = 23;
 		int second = 68;
 		int great;
 
 		great = getGreaterInt(first, second); // Method call here
 		System.out.println("Greater value of " + first + " and " + second + ": " + great);
 	}
   
   
 	public static int getGreaterInt( int value1, int value2 )
 	{
 		int biggest; //Declare a variable to hold the biggest int
 
 		if ( value1 > value )
 		{
 			biggest = value1;
 		}
 		else
 		{
 			biggest = value2;
 		}
 
 		return biggest;
 	}
  
 } 

Note the following line in the example above:

 great = getGreaterInt(first, second); // Method call here 

This line contains a method call.

  • The calling method, or caller method, is the main method.
  • The called method is getGreaterInt.
  • The method getGreaterInt accepts two int parameters.
    • In the method call two int values are passed to getGreaterInt. These values are known as arguments. Method arguments are explained in the next section.