Parameters are specified after the method name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma.
The following example has a method that takes a String
called fname as parameter. When the method is called, we pass along a first name, which is used inside the method to print the full name:
Example
public class Main {
static void myMethod[String fname] {
System.out.println[fname + " Refsnes"];
}
public static void main[String[] args] {
myMethod["Liam"];
myMethod["Jenny"];
myMethod["Anja"];
}
}
// Liam Refsnes
// Jenny Refsnes
// Anja Refsnes
Try it Yourself »
When a parameter is passed to the method, it is called an argument. So, from the example above: fname
is a parameter, while Liam
,
public class Main {
static void myMethod[String fname, int age] {
System.out.println[fname + " is " + age];
}
public static void main[String[] args] {
myMethod["Liam", 5];
myMethod["Jenny", 8];
myMethod["Anja", 31];
}
}
// Liam is 5
// Jenny is 8
// Anja is 31
0 and public class Main {
static void myMethod[String fname, int age] {
System.out.println[fname + " is " + age];
}
public static void main[String[] args] {
myMethod["Liam", 5];
myMethod["Jenny", 8];
myMethod["Anja", 31];
}
}
// Liam is 5
// Jenny is 8
// Anja is 31
1 are arguments.Multiple Parameters
You can have as many parameters as you like:
Example
public class Main {
static void myMethod[String fname, int age] {
System.out.println[fname + " is " + age];
}
public static void main[String[] args] {
myMethod["Liam", 5];
myMethod["Jenny", 8];
myMethod["Anja", 31];
}
}
// Liam is 5
// Jenny is 8
// Anja is 31
Try it Yourself »
Note that when you are working with multiple parameters, the method call must have the same number of arguments as there are parameters, and the arguments must be passed in the same order.
Return Values
The
public class Main {
static void myMethod[String fname, int age] {
System.out.println[fname + " is " + age];
}
public static void main[String[] args] {
myMethod["Liam", 5];
myMethod["Jenny", 8];
myMethod["Anja", 31];
}
}
// Liam is 5
// Jenny is 8
// Anja is 31
2 keyword, used in the examples above, indicates that the method should not return a value. If you want the method to return a value, you can use a primitive data type [such as public class Main {
static void myMethod[String fname, int age] {
System.out.println[fname + " is " + age];
}
public static void main[String[] args] {
myMethod["Liam", 5];
myMethod["Jenny", 8];
myMethod["Anja", 31];
}
}
// Liam is 5
// Jenny is 8
// Anja is 31
3, public class Main {
static void myMethod[String fname, int age] {
System.out.println[fname + " is " + age];
}
public static void main[String[] args] {
myMethod["Liam", 5];
myMethod["Jenny", 8];
myMethod["Anja", 31];
}
}
// Liam is 5
// Jenny is 8
// Anja is 31
4, etc.] instead of public class Main {
static void myMethod[String fname, int age] {
System.out.println[fname + " is " + age];
}
public static void main[String[] args] {
myMethod["Liam", 5];
myMethod["Jenny", 8];
myMethod["Anja", 31];
}
}
// Liam is 5
// Jenny is 8
// Anja is 31
2, and use the public class Main {
static void myMethod[String fname, int age] {
System.out.println[fname + " is " + age];
}
public static void main[String[] args] {
myMethod["Liam", 5];
myMethod["Jenny", 8];
myMethod["Anja", 31];
}
}
// Liam is 5
// Jenny is 8
// Anja is 31
6 keyword inside the method: Dave Braunschweig
Overview
A parameter is a special kind of variable used in a function to refer to one of the pieces of data provided as input to the function. These pieces of data are the values of the arguments with which the function is going to be called/invoked. An ordered list of parameters is usually included in the definition of a function, so that, each time the function is called, its arguments for that call are evaluated, and the resulting values can be assigned to the corresponding parameters.
Discussion
Recall that the modular programming approach separates the functionality of a program into independent modules. To separate the functionality of one function from another, each function is given its own unique input variables, called parameters. The parameter values, called arguments, are passed to the function when the function is called. Consider the following function pseudocode:
Function CalculateCelsius [Real fahrenheit] Declare Real celsius Assign celsius = [fahrenheit - 32] * 5 / 9 Return Real celsius
If the CalculateCelsius function is called passing in the value 100, as in CalculateCelsius[100]
, the parameter is fahrenheit
and the argument is 100
. The terms parameter and argument are often used interchangeably. However, parameter refers to the variable identifier [fahrenheit] while argument refers to the variable value [100].
Functions may have no parameters or multiple parameters. Consider the following function pseudocode:
Function DisplayResult [Real fahrenheit, Real celsius] Output fahrenheit & "° Fahrenheit is " & celsius & "° Celsius" End
If the DisplayResult function is called passing in the values 98.6 and 37.0, as in DisplayResults[98.6, 37.0]
, the argument or value for the fahrenheit parameter is 98.6 and the argument or value for the celsius parameter is 37.0. Note that the arguments are passed positionally. Calling DisplayResults[37.0, 98.6]
would result in incorrect output, as the value of fahrenheit would be 37.0 and the value of celsius would be 98.6.
Some programming languages, such as Python, support named parameters. When calling functions using named parameters, parameter names and values are used, and positions are ignored. When names are not used, arguments are identified by position. For example, any of the following function calls would be valid: