returning a value from a method.
posted 9 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
ok i understand how to pass a value to a method,its pretty easy...but have i got returning values from methods right?
when you want to return a value leave out the void keyword
are you always returning a calculation of something?
for example a calculation of two sums etc like age + age
is it always returned as a variable?
i still dont really understand returning a value,as if i create a variable in one class and instantiate that
i can still use its variables...can somebody please break this down into idiot speak for me so i can move along
thanks
when you want to return a value leave out the void keyword
are you always returning a calculation of something?
for example a calculation of two sums etc like age + age
is it always returned as a variable?
i still dont really understand returning a value,as if i create a variable in one class and instantiate that
i can still use its variables...can somebody please break this down into idiot speak for me so i can move along
thanks
posted 9 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
you don't just leave out the "void" keyword...you have to replace it with what your method will return. It could be a primitive or an object.
You are not always returning a calculation. You might return an object that is a connection to a database. Or a String. Or just about any kind of object.
it does not have to be a variable. If I have a method declared to return an int, i could do this:
return 7;
or a boolean method could have this:
return true;
In some ways, a method can be thought of as asking a question, and the thing returned is the answer.
You are not always returning a calculation. You might return an object that is a connection to a database. Or a String. Or just about any kind of object.
it does not have to be a variable. If I have a method declared to return an int, i could do this:
return 7;
or a boolean method could have this:
return true;
In some ways, a method can be thought of as asking a question, and the thing returned is the answer.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
posted 9 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
One of simplest is (usually) a getter. A typical getter looks like this:
I used "int" for the type here but it could be anything.
I used "int" for the type here but it could be anything.
jon ninpoja
Ranch Hand
Posts: 424
3
posted 9 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
i have a little program i wrote on lunch today
and i instantiate and call the getTotal method from my main in theshop class (didnt think it was necessary to post it here)
now this works...and i get the total of whatever two numbers i pass in
and the method prints out the total too,so i dont need to return the value to the main method.
what can i add to this program that would require it to return a value?
heres another one,i tried returning a value with this code:
but i just have intelisense saying invalid method declaration,return type required
im sure im mssing something obvious i prob have red 5 times...any help appreciated
and i instantiate and call the getTotal method from my main in theshop class (didnt think it was necessary to post it here)
now this works...and i get the total of whatever two numbers i pass in
and the method prints out the total too,so i dont need to return the value to the main method.
what can i add to this program that would require it to return a value?
heres another one,i tried returning a value with this code:
but i just have intelisense saying invalid method declaration,return type required
im sure im mssing something obvious i prob have red 5 times...any help appreciated
posted 9 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
When the tool says "Return type required" it means that it is expecting a return type but couldn't find one. On line 6, what is the return type of the method that you declared?
jon ninpoja
Ranch Hand
Posts: 424
3
posted 9 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
ooooh ok added int and its working fine now...
i thought because it was taking in int it would know to return it
this really makes sense to me know
if you want a return (even if taking in values) you need to specify it
also when would this program need to return something
i thought because it was taking in int it would know to return it
this really makes sense to me know
if you want a return (even if taking in values) you need to specify it
also when would this program need to return something
posted 9 years ago
A method must return something whenever the method's return type is declared as something other than "void".
For example,
must return an int, and
must return an instance of MyType.
No, it is not necessary to return a variable. You can return anything that resolves to the correct type. So instead of
You could eliminate the variable 'value' and just say
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
when would this program need to return something?
A method must return something whenever the method's return type is declared as something other than "void".
For example,
must return an int, and
must return an instance of MyType.
it always returned as a variable?
No, it is not necessary to return a variable. You can return anything that resolves to the correct type. So instead of
You could eliminate the variable 'value' and just say
posted 9 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Another thing is that the getTotal method does not get a total. You cannot get a total from it. The method is named incorrectly; it should be called displayTotal. Either print the total or return it. Don't do both in the same method.
jon ninpoja
Ranch Hand
Posts: 424
3
posted 9 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
i tried another little program:
heres main
but why isnt it returning? shouldnt it print out in the console the age?
i know i can print this in the method itself? but doesnt that defeat the purpose?
also i cant see any of the variables in the method...of course this is due to variable scope
so what do i have to do
i will do what somebody on here said a few posts ago...regarding something to try on a program i have already written
was just on lunch and was giving this a go....oh i hate my job...its really getting in the way
heres main
but why isnt it returning? shouldnt it print out in the console the age?
i know i can print this in the method itself? but doesnt that defeat the purpose?
also i cant see any of the variables in the method...of course this is due to variable scope
so what do i have to do
i will do what somebody on here said a few posts ago...regarding something to try on a program i have already written
was just on lunch and was giving this a go....oh i hate my job...its really getting in the way
posted 9 years ago
This doesn't print anything out and it doesn't set the "customerAge" field. It has a variable "birthYear" but this is really age.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
jon ninpoja wrote:
This doesn't print anything out and it doesn't set the "customerAge" field. It has a variable "birthYear" but this is really age.
posted 9 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
When a method has a return type, it can act like a variable of that type. So for instance,
But there's an easier way. You don't need to set a variable to use the return type:
But there's an easier way. You don't need to set a variable to use the return type:
All things are lawful, but not all things are profitable.
Fred Kleinschmidt
Bartender
Posts: 732
10
posted 9 years ago
This is not the birth year, nor even the age, but rather the negative of the age.
-
1 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
This is not the birth year, nor even the age, but rather the negative of the age.
| Well don't expect me to do the dishes! This ad has been cleaned for your convenience: Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |










