PROGRAMMING, LOGIC and DESIGN 8th EDITION
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Go down
avatar
Admin
Admin
Posts : 11
Join date : 2018-08-07
Age : 25
https://rachellann.forumotion.com

Creating Methods that Return a Value Empty Creating Methods that Return a Value

Tue Aug 07, 2018 1:55 pm
When you want to retain a value that exists when a method ends, you can return the value from the method to the calling method.
When a method returns a value, the method must have a return type that matches the data type of the returned value.
A return type can be any type, which includes num and string , as well as other types specific to the programming language you are using.
A method can also return nothing, in which case the return type is void , and the method is a void method . (The term void means “nothing” or “empty.”)
A method’s return type is known more succinctly as a method’s type , and it is listed in front of the method name when the method is defined.

Creating Methods that Return a Value Screen11

For example, a method that returns the number of hours an employee has worked might have the following header:
num getHoursWorked()
This method returns a numeric value, so its type is num .
When a method returns a value, you usually want to use the returned value in the calling method, although this is not required.
For example, Figure 9-8 shows how a program might use the value returned by the getHoursWorked() method. A variable named hours is declared
in the main program. The getHoursWorked() method call is part of an assignment statement.
When the method is called, the logical control is transferred to the getHoursWorked() method, which contains a variable named workHours . A value is obtained for this variable, which is returned to the main program where it is assigned to hours . After logical control returns to the main program from the getHoursWorked() method, the method’s local variable workHours no longer exists. However, its value has been stored in the main program where, as hours , it can be displayed and used in a calculation.


Creating Methods that Return a Value Screen12

Figure 9-8, notice the return type num that precedes the method name in the getHoursWorked() method header. A method’s declared return type must match the type of the value used in the return statement; if it does not, the program will not compile.
A numeric value is correctly included in the return statement—the last statement in the getHoursWorked() method. When you place a value in a return statement, the value is sent from the called method back to the calling method.
A method’s return statement can return one value at most. The returned value can be a variable or a constant. The value can be a simple data type or a complex data type.
For example, in Chapter 10 you will learn to create objects, which are more complex data types.
You are not required to assign a method’s return value to a variable to use the value. Instead, you can use a method’s returned value directly, without storing it. You use a method’s value in the same way you would use any variable of the same type. For example, you can output a return value in a statement such as the following: output "Hours worked is ", getHoursWorked()
Because getHoursWorked() returns a numeric value, you can use the method callgetHoursWorked() in the same way that you would use any simple numeric value.
Figure 9-9 shows an example of a program that uses a method’s return value directly without storing it.
The value of the shaded workHours variable returned from the method is used directly in thecalculation of gross in the main program.


Creating Methods that Return a Value Screen13


Figure 9-10.The method accepts three parameters and returns the largest of the values. Although this method works correctly and you might see this technique used, its style is awkward and not structured. . The return statements in Figure 9-10 violate this
convention by leaving decision structures before they are complete.
Figure 9-11 shows the superior and recommended way to handle the problem. In Figure 9-11, the largest value isstored in a variable. Then, when the nested decision structure is complete, the stored value is returned in the last method statement


Creating Methods that Return a Value Screen14

Using an IPO Chart

When designing methods to use within larger programs, some programmers find it helpful to use an IPO chart , a tool that identifies and categorizes each item needed within the method aspertaining to input, processing, or output. For example, consider a method that finds the smallest of three numeric values. When you think about designing this method, you can start by placing each of its components in one of the three processing categories, as shown in Figure 9-12.


Creating Methods that Return a Value Screen15

The IPO chart in Figure 9-12 provides an overview of the processing steps involved in the method. Like a flowchart or pseudocode, an IPO chart is just another tool to help you plan the logic of your programs. Many programmers create an IPO chart only for specific methods in their programs and as an alternative to flowcharting or writing pseudocode. IPO charts provide an overview of input to the method, the processing steps that must occur, and the resulting output.


TWO TRUTHS & A LIE
Creating Methods that Return a Value
1. The return type for a method can be any type, which includes numeric, character, and string, as well as other more specific types that exist in the programming language you are using.
2. A method’s return type must be the same type as one of the method’s parameters.
3. You are not required to use a method’s returned value.

WHICH OF THEM IS A LIE?

avatar
Hyegi
Guru
Guru
Posts : 10
Join date : 2018-08-14

Creating Methods that Return a Value Empty Re: Creating Methods that Return a Value

Tue Aug 14, 2018 8:34 am
What example of program can i use this?
avatar
Hyegi
Guru
Guru
Posts : 10
Join date : 2018-08-14

Creating Methods that Return a Value Empty Re: Creating Methods that Return a Value

Tue Aug 14, 2018 8:56 am
Can I have a return statement in a void method?
avatar
Hyegi
Guru
Guru
Posts : 10
Join date : 2018-08-14

Creating Methods that Return a Value Empty Re: Creating Methods that Return a Value

Tue Aug 14, 2018 8:56 am
Can you return multiple values in Java?
avatar
nicolai
Guru
Guru
Posts : 10
Join date : 2018-08-14

Creating Methods that Return a Value Empty Re: Creating Methods that Return a Value

Tue Aug 14, 2018 8:58 am
Hyegi wrote:Can I have a return statement in a void method?

It does not need to contain a return statement, but it may do so. In such a case, a return statement can be used to branch out of a control flow block and exit the method and is simply used like this: return; If you try to return a value from a method that is declared void , you will get a compiler error.
avatar
nicolai
Guru
Guru
Posts : 10
Join date : 2018-08-14

Creating Methods that Return a Value Empty Re: Creating Methods that Return a Value

Tue Aug 14, 2018 8:59 am
Hyegi wrote:Can you return multiple values in Java?

If you are returning more than 1 value that are related, then it makes sense to encapsulate them into a class and then return an object of that clas. If you want to return unrelated values then you can use java's built-in container classes like Map, List, Set etc. Check the java.util package's JavaDoc for more details.
avatar
nicolai
Guru
Guru
Posts : 10
Join date : 2018-08-14

Creating Methods that Return a Value Empty Re: Creating Methods that Return a Value

Tue Aug 14, 2018 8:59 am
Can I return two values from a function in C++?
avatar
nicolai
Guru
Guru
Posts : 10
Join date : 2018-08-14

Creating Methods that Return a Value Empty Re: Creating Methods that Return a Value

Tue Aug 14, 2018 9:01 am
Are methods and functions the same?
avatar
nicolai
Guru
Guru
Posts : 10
Join date : 2018-08-14

Creating Methods that Return a Value Empty Re: Creating Methods that Return a Value

Tue Aug 14, 2018 9:01 am
What is an overriding method?
avatar
ekkoekko
Guru
Guru
Posts : 10
Join date : 2018-08-18

Creating Methods that Return a Value Empty Re: Creating Methods that Return a Value

Sat Aug 18, 2018 6:56 pm
Can a method return two values?
avatar
ekkoekko
Guru
Guru
Posts : 10
Join date : 2018-08-18

Creating Methods that Return a Value Empty Re: Creating Methods that Return a Value

Sat Aug 18, 2018 6:57 pm
Can you have two return statements in a method?
avatar
ekkoekko
Guru
Guru
Posts : 10
Join date : 2018-08-18

Creating Methods that Return a Value Empty Re: Creating Methods that Return a Value

Sat Aug 18, 2018 6:57 pm

Can a method return an array?
avatar
ekkoekko
Guru
Guru
Posts : 10
Join date : 2018-08-18

Creating Methods that Return a Value Empty Re: Creating Methods that Return a Value

Sat Aug 18, 2018 6:58 pm

What is a return value in Java?
avatar
ekkoekko
Guru
Guru
Posts : 10
Join date : 2018-08-18

Creating Methods that Return a Value Empty Re: Creating Methods that Return a Value

Sat Aug 18, 2018 6:58 pm
Can I return two values from a function in C++?
avatar
ekkoekko
Guru
Guru
Posts : 10
Join date : 2018-08-18

Creating Methods that Return a Value Empty Re: Creating Methods that Return a Value

Sat Aug 18, 2018 6:58 pm
What does the return do in Java?
avatar
ekkoekko
Guru
Guru
Posts : 10
Join date : 2018-08-18

Creating Methods that Return a Value Empty Re: Creating Methods that Return a Value

Sat Aug 18, 2018 7:08 pm
What does the void do in a method?
avatar
janna
Guru
Guru
Posts : 10
Join date : 2018-08-18

Creating Methods that Return a Value Empty Re: Creating Methods that Return a Value

Sat Aug 18, 2018 7:13 pm
ekkoekko wrote:What does the void do in a method?

From Wikibooks, open books for an open world. void is a Java keyword. Used at method declaration and definition to specify that the method does not return any type, the method returns void .
avatar
janna
Guru
Guru
Posts : 10
Join date : 2018-08-18

Creating Methods that Return a Value Empty Re: Creating Methods that Return a Value

Sat Aug 18, 2018 7:15 pm
ekkoekko wrote:
What is a return value in Java?

In computer programming, the return type (or result type) defines and constrains the data type of the value returned from a subroutine or method. In many programming languages (especially statically-typed programming languages such as C, C++, Java) the return type must be explicitly specified when declaring a function.
avatar
janna
Guru
Guru
Posts : 10
Join date : 2018-08-18

Creating Methods that Return a Value Empty Re: Creating Methods that Return a Value

Sat Aug 18, 2018 7:16 pm
ekkoekko wrote:Can I return two values from a function in C++?

A C++ function can return only one value. However, you can return multiple values by wrapping them in a class or struct. Or you could use std::tuple , if that is available with your compiler.
avatar
kathrynmoto
Guru
Guru
Posts : 10
Join date : 2018-08-17

Creating Methods that Return a Value Empty Re: Creating Methods that Return a Value

Mon Aug 20, 2018 2:12 pm
nicolai wrote:Are methods and functions the same?
A function is a piece of code that is called by name while A method is a piece of code that is called by a name but is associated with an object.
avatar
kathrynmoto
Guru
Guru
Posts : 10
Join date : 2018-08-17

Creating Methods that Return a Value Empty Re: Creating Methods that Return a Value

Mon Aug 20, 2018 2:15 pm
nicolai wrote:What is an overriding method?

If subclass (child class) has the same method as declared in the parent class, it is known as method overriding,
avatar
kathrynmoto
Guru
Guru
Posts : 10
Join date : 2018-08-17

Creating Methods that Return a Value Empty Re: Creating Methods that Return a Value

Mon Aug 20, 2018 2:21 pm
ekkoekko wrote:Can you have two return statements in a method?

The question is whether a method may have multiple return statements . The answer may surprise you: In a pure object-oriented world, a method must have a single return statement and nothing else. Yes, just a return statement and that's it. No other operators or statements. Just return. All arguments in favor of multiple return statements go against the very idea of object-oriented programming.
avatar
kathrynmoto
Guru
Guru
Posts : 10
Join date : 2018-08-17

Creating Methods that Return a Value Empty Re: Creating Methods that Return a Value

Mon Aug 20, 2018 2:27 pm
ekkoekko wrote:
Can a method return an array?
Functions shall not have a return type of type array or function, although they may have a return type of type pointer or reference to such things. There shall be no arrays of functions, although there can be arrays of pointers to functions.
avatar
kathrynmoto
Guru
Guru
Posts : 10
Join date : 2018-08-17

Creating Methods that Return a Value Empty Re: Creating Methods that Return a Value

Mon Aug 20, 2018 2:28 pm
ekkoekko wrote:
What is a return value in Java?
The "return value" is the value you're expecting to be returned. In this case the return value is the person's name of the object you pass to the function. you can use this like: string yourName = getName(You);
avatar
kathrynmoto
Guru
Guru
Posts : 10
Join date : 2018-08-17

Creating Methods that Return a Value Empty Re: Creating Methods that Return a Value

Mon Aug 20, 2018 2:31 pm
Hyegi wrote:What example of program can i use this?

example program. finding the minimum value

public class ExampleMinNumber {

public static void main(String[] args) {
int a = 11;
int b = 6;
int c = minFunction(a, b);
System.out.println("Minimum Value = " + c);
}

/** returns the minimum of two numbers */
public static int minFunction(int n1, int n2) {
int min;
if (n1 > n2)
min = n2;
else
min = n1;

return min;
}
}

This will produce the following result −

Output

Minimum value = 6
Sponsored content

Creating Methods that Return a Value Empty Re: Creating Methods that Return a Value

Back to top
Similar topics
Permissions in this forum:
You cannot reply to topics in this forum