A variable that receives a value that is passed into a method is known as a(n)

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:

In Java, you need to declare all your variables before using them. That means you simply state its type and name:

int num;

In this case you declared a variable called num, which can hold an int or integer.

Once its been declared, you can assign it to a value:

 num = 5;

You can also combine these steps:

int num = 5;

You can change the value of a variable at any point:

 num = 72;

num is now 72 instead of 5.

But in Java it cannot be set to the wrong type:

num = "word"; //error!

Since num was declared as an int, you cannot assign it the String "word".

In the previous challenge [and future ones], you coded a solution within a piece of code known as a method:

public static int doStuff[int a, int b]{
    //your code here - you could then type:
    return a + b;
}

A method is a block of code that can be called by other code. In this example, the code at the bottom [that we said you could ignore], calls the method

 num = 5;
2 and it does stuff. By putting pieces of your code into method blocks, you can keep things organized, and you can call the same code multiple times from different parts of your program.
Note that you will usually want to name your methods with a more descriptive name than "do stuff".

In many cases, methods can be used like parts of a logical flowchart seen in Algorithm ways.

Look at the top of that method again:

 doStuff[int a, int b]{

The method takes in two variables,

 num = 5;
3 and
 num = 5;
4. Variables that are taken in by a method are called parameters. They are declared in the opening parentheses of a method and are assigned a value whenever the method is called. If the method is called again, they are assigned new values.

So other code can pass two numbers to

 num = 5;
2 to do stuff with. In this example, it will add two numbers, so:

 num = 5;
6 // will return 4
 num = 5;
7] // will return 10
 num = 5;
8 //will cause an error! This is because, the method declares
 num = 5;
3 and
 num = 5;
4 as integers so they cannot receive Strings!

In the challenges on this site, the code on the bottom of each challenge passes in numbers to the method

 num = 5;
2 [which get assigned to the parameters
 num = 5;
3 and
 num = 5;
4]. You need to fill in
 num = 5;
2 so it "does something" with
 num = 5;
3 and
 num = 5;
4 and returns the correct answer to the code that called it.

Challenge

As in the previous challenge, you will be given the method

 num = 5;
2 which takes in two parameters
 num = 5;
3 and
 num = 5;
4. [You should replace
 num = 72;
0 with your own code.] For this challenge, you are to return the average of the two numbers, rounded down to the nearest integer.

Guideline: Create a new variable to store the sum of

 num = 5;
3 and
 num = 5;
4. To get the average of the two numbers, simply divide that sum by 2 with
 num = 72;
3, and return that result.
 num = 72;
3 will automatically round the answer down.

What is a value passed into a method called?

Arguments are the actual values that are passed in when the method is invoked. When you invoke a method, the arguments used must match the declaration's parameters in type and order.

What is a method variable?

A method variable is essentially a variable that has had a function assigned to it, "binding" the function to an instance and enabling you to use the variable to refer to the function - much like you use a runtime function name to refer to a built-in GML function.

When we pass an object into a method what information is being sent to the method?

When passing an argument by reference, the method gets a reference to the object. A reference to an object is the address of the object in memory. Now, the local variable within the method is referring to the same memory location as the variable within the caller.

How is an argument passed to a method?

Arguments are passed by value. When invoked, a method or a constructor receives the value of the variable passed in. When the argument is of primitive type, "pass by value" means that the method cannot change its value.

Chủ Đề