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
kathrynmoto
Guru
Guru
Posts : 10
Join date : 2018-08-17

Creating Methods that Return a Value - Page 2 Empty Re: Creating Methods that Return a Value

Mon Aug 20, 2018 2:33 pm
ekkoekko wrote:Can a method return two values?

You can't return two values. However, you can return a single value that is a struct that contains two values. You can return only one thing from a function. Either you make a struct which contains all the things you want to return, or you pass some function parameters by reference
avatar
JUNGKOOK
Guru
Guru
Posts : 10
Join date : 2018-08-20

Creating Methods that Return a Value - Page 2 Empty Re: Creating Methods that Return a Value

Mon Aug 20, 2018 4:03 pm
Hyegi wrote:Can I have a return statement in a void method?
Hala, sa pagkaka alam ko lang Oo hahaha
avatar
johnarnold
Guru
Guru
Posts : 10
Join date : 2018-08-22

Creating Methods that Return a Value - Page 2 Empty Re: Creating Methods that Return a Value

Wed Aug 22, 2018 3:21 pm
example.
var x = myFunction(4, 3); // Function is called, return value will end up in x

function myFunction(a, b) {
return a * b; // Function returns the product of a and b
}

The result in x will be:
12
avatar
johnarnold
Guru
Guru
Posts : 10
Join date : 2018-08-22

Creating Methods that Return a Value - Page 2 Empty Re: Creating Methods that Return a Value

Wed Aug 22, 2018 3:24 pm
Creating Methods that Return a Value - Page 2 Java_m10
avatar
Admin
Admin
Posts : 11
Join date : 2018-08-07
Age : 25
https://rachellann.forumotion.com

Creating Methods that Return a Value - Page 2 Empty Re: Creating Methods that Return a Value

Wed Aug 22, 2018 3:27 pm
JUNGKOOK wrote:
Hyegi wrote:Can I have a return statement in a void method?
Hala, sa pagkaka alam ko lang Oo hahaha

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
Admin
Admin
Posts : 11
Join date : 2018-08-07
Age : 25
https://rachellann.forumotion.com

Creating Methods that Return a Value - Page 2 Empty Re: Creating Methods that Return a Value

Wed Aug 22, 2018 4:18 pm
janna wrote:
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 .

Void doesn't return anything; it tells the compiler the method doesn't have a return value. void means it returns nothing.
Sponsored content

Creating Methods that Return a Value - Page 2 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