answersLogoWhite

0


Best Answer

Parameters and return values are a major part of methods. When defining a method, you must include information about the data types of the return value and the parameters. An example of a method definition is this:

public int getSumOfNumbers( int number1, int number2, int number3 )

{ return ( number1 + number2 + number3 ); }

The word "int" right after the word "public" is the return type. It describes what data type will be returned by the method. In this case, it was int, or integer.

The sequence of words in between the parantheses, "int number1, int number2, int number3", is the parameter list. Each of the phrases separated by a comma in the parameter list is a parameter. The first word - in this case "int" - is the data type of the parameter. It describes what type of variable the parameter will be. The second word - "number1", "number2", or "number3" - is the name of the parameter.

Every parameter must have a data type and a name, and every method must have a return type: even a method that returns nothing. For example:

public void evaluateNumber(int number)

{ if ( number > 0 )

{ System.out.println( number + " is positive." ); }

else if ( number < 0 )

{ System.out.println( number + " is negative." ); }

else

{ System.out.println( number + " is zero." ); }

}

When a method does not return data, its return type must be defined as void, as it is above.

User Avatar

Wiki User

6y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What do parameters and return values have to do with methods?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Why the functions of java is called methods?

because it is capable of taking parameters, passing or returning values as in c++. it has it's own identity as return type, and signature of the method body. It can also be overloaded as well.


How are functions invoked in cpp?

If the function is inline expanded then it is not invoked at all -- there is no function call. However, if the function is not or cannot be inline expanded, a procedure call is invoked. This pushes the calling function's local values onto the stack, followed by the return address, followed by the callee's argument values in reverse order. Control is then passed to the address of the function. The function then pops the arguments off the stack and assigns them to its local parameters (parameters that are passed by value will automatically invoke the copy constructors of those parameters). The function then executes. When a return statement is encountered, the return address is popped from the stack, the return value (if any) is pushed onto the stack, and control is passed to the return address. When a function returns, the return value (if any) and the local values are popped from the stack, and execution continues from where it left off.


Can you return multiple values from a function in C?

There are many approaches to this 'problem'. Since a C function is only allowed to return one value as the name of the function you have to provide parameters to the routine if you want multiple values to be returned.For example, traditionally you return a value thus:int Today () ;if (Today() == 2) ....int Today (){/* Some logic */return value ;}So if you want multiple values, send parameters to the routine that can be changed:int Today (int * val1, int * val2, char * charValue) ;Then, call it:int first ;int second ;char third ;if (Today (&first, &second, &third) == 2)In this case first, second, and third can be changed inside the Today routine and return multiple values.int Today (int * val1, int * val2, char * charValue){*val1 = 5 ;*val2 = 10 ;*charValue = 'Y' ;return 2 ;}


Do void methods have parameters?

A void method is just like any other method; it may or may not have parameters.


What type of a variable is used to indirectly return multiple results by a function?

To return multiple values of the same type, return an array. If the values are different types, return a tuple or data structure. To return values indirectly, return a pointer to the results (arrays implicitly convert to pointers, but tuples and data structures do not). A returned pointer must never refer to a local variable of the returning function; upon return, those variables will cease to exist, resulting in undefined behaviour. To avoid this, the caller may provide a user-defined storage location via an output argument, or the function may allocate the return values on the free store (the heap).

Related questions

Why the functions of java is called methods?

because it is capable of taking parameters, passing or returning values as in c++. it has it's own identity as return type, and signature of the method body. It can also be overloaded as well.


How many values you can return by call by value and call by reference at a time?

A function can only return one value, but it can modify its parameters if their type is 'in out' or 'out'.


You can overload methods with differences only in their return type?

False. Overloaded methods must have different parameters defined.A different return type alone would not help the compiler determine which method to choose at compile time.


What is global parameters?

Some ADARUN parameters are global parameters; that is, they must have the same values for all nuclei in a cluster.


How are functions invoked in cpp?

If the function is inline expanded then it is not invoked at all -- there is no function call. However, if the function is not or cannot be inline expanded, a procedure call is invoked. This pushes the calling function's local values onto the stack, followed by the return address, followed by the callee's argument values in reverse order. Control is then passed to the address of the function. The function then pops the arguments off the stack and assigns them to its local parameters (parameters that are passed by value will automatically invoke the copy constructors of those parameters). The function then executes. When a return statement is encountered, the return address is popped from the stack, the return value (if any) is pushed onto the stack, and control is passed to the return address. When a function returns, the return value (if any) and the local values are popped from the stack, and execution continues from where it left off.


Can you return multiple values from a function in C?

There are many approaches to this 'problem'. Since a C function is only allowed to return one value as the name of the function you have to provide parameters to the routine if you want multiple values to be returned.For example, traditionally you return a value thus:int Today () ;if (Today() == 2) ....int Today (){/* Some logic */return value ;}So if you want multiple values, send parameters to the routine that can be changed:int Today (int * val1, int * val2, char * charValue) ;Then, call it:int first ;int second ;char third ;if (Today (&first, &second, &third) == 2)In this case first, second, and third can be changed inside the Today routine and return multiple values.int Today (int * val1, int * val2, char * charValue){*val1 = 5 ;*val2 = 10 ;*charValue = 'Y' ;return 2 ;}


Do void methods have parameters?

A void method is just like any other method; it may or may not have parameters.


What type of a variable is used to indirectly return multiple results by a function?

To return multiple values of the same type, return an array. If the values are different types, return a tuple or data structure. To return values indirectly, return a pointer to the results (arrays implicitly convert to pointers, but tuples and data structures do not). A returned pointer must never refer to a local variable of the returning function; upon return, those variables will cease to exist, resulting in undefined behaviour. To avoid this, the caller may provide a user-defined storage location via an output argument, or the function may allocate the return values on the free store (the heap).


Why method overloading is not possible by return type method?

A method can be used, and the return type discarded. For example, for a method that returns, say, an integer, instead of calling it like this: int x; x = MyMethod(1, 2); You can also call it like this: MyMethod(1, 2); That is, without using the return value. In such a case, if you were to have different methods with the same parameters but different return values, the compiler wouldn't know which version of the method to use.


What is the parameter for 3G drive test?

sir please tell me the parameters of 3g drive test....and its exact values in drive parameters...


How do you overload static methods in java?

Just create two methods with the same name, but with different types or numbers of parameters.


Program in Java to illustrate the function overloading?

Below is an example of method overloading which returns the sum of two number types. Each method performs the calculation and returns a value in a different type. int sum(final int a, final int b) { return a + b; } long sum(final long a, final long b) { return a + b; } float sum(final float a, final float b) { return a + b; } double sum(final double a, final double b) { return a + b; }