What is an output parameter?

Asked

Viewed 1,189 times

7

Consider the following example:

appendFooter(s);

Analyzing the function signature, we have the following:

public void appendFooter(StringBuffer report)

The parameter s is considered in the above function to be an output parameter. Why?

What is an output parameter?

2 answers

9

An output parameter does not create a new storage location, instead it represents the same storage location as the variable given as argument in method invocation.

Output parameters are used in methods that return multiple values.

  • So it would be the same as passing a parameter by reference, that’s it?

  • I believe so, with the difference that by reference the variable has to be initialized... Unless mistake...

  • Not all output parameters are necessarily by reference, but it is the most common implementation in the languages that are used in practice. Depending also on the language, it is possible to pass a parameter by reference, but force it to be immutable.

7


I will try to answer as correctly as possible, but without knowing the language the answer may not be true, although I am confident that this applies to almost every case.

appendFooter(s);
appendFooter(StringBuffer report)

s would be an argument and we name the parameter for report.

It is possible to say that the parameter in the case report is output as much as it is input.

Input parameter

void metodo(int x) {
    print x;
}

This is a case where the parameter is input. Anything that is sent to this method goes in one direction, the object enters in the method.

Input and output parameter

void metodo(array x) {
    x[0] = 1;
}

In this case the object is a type by reference, so it sends to the method only a pointer form that gives access to the object. Any change to the object will be reflected in the argument. Then it is possible to transfer a value to the argument through the parameter.

It goes in two directions, enters a value in the method and can exit a value of this method.

In the example element 0 of array will be set to 1 even when the method shuts down. It is a way to return a value by the parameter itself.

If the argument is not a variable, this return cannot be used for anything later, therefore there will be the exit, but it will be discarded.

It’s possible in some languages to prevent the output in reference types, something like that:

void metodo(const array x) {
    x[0] = 1;
}

In such a case any change in x will not be reflected in the argument, so there is no exit.

Output parameter

void metodo(out int x) {
    x = 1;
}

Some languages have a purely output parameter. In this form the argument must be a variable to receive the value that the method sends to it. It is not possible to send a value to the method, so if the variable has a value, it will not be sent as an input parameter. It has only one direction, the exit.

In this example x will have the value 1 at the end of the method and whoever called the method will receive this value, something like this:

var int a;
metodo(a);
print a; //imprimirá 1

I put in the Github for future reference.

This is a useful resource to have multiple returns in the method, since the normal is that it only returns one value. Some languages have resource to return multiple values, which requires not having a unique output parameter.

Completion

I imagine the guy StringBuffer is a type by reference, then report is an input and output parameter.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.