Guys, I’m new to programming and I just don’t understand yet what the return function is doing here… what is it doing here and why is it only needed in the withdraw method? Why have it at all if deposit works just fine without the return function or why doesn’t the deposit method need a return function? Also, why is deposit void and the withdraw method an int? I mean, I see it as both are whole numbers so why shouldn’t both be ints… doesn’t make any sense to me. Help!
Hello @design6013459543, welcome to the forums! The
return keyword hands data back to the caller-it basically gives the program (code not in the function) the data that was made inside the function. Think of a program like an online delivery website. A function in this analogy is the order form. When you fill it out and submit it, you get a confirmation message. This is like
System.out.println(). The data of your order, however, is sent to the the company to be processed-that is like
return. Here is some more reading on
return in Java.
Back to your other question:
In Java, when you declare a function, you have to specify what type of object it
returns. If it returns an integer, you set
int, if it returns a string, you set
string. If the function does not return anything, you set
void-which tells the program not to expect the function to return anything.
So, in answer to the question,
voidas it does not return anything.
returns an integer (whole number). Here is an article that explains return/return types.
checkBalance do not need a
return as they simply alter a variable-they don’t need to provide any data to the calling function (the part of the program that calls them).
withdraw needs to give some data back to the calling function, and it therefore requires the
return. This is some general reading on Java functions.
I hope this helps!
P.S. If there is any other question about this lesson, could you provide the link to the exercise?
OK, so I understand the void and int types and what they mean for the method. However, I don’t understand why the deposit method wouldn’t be returning anything and the withdraw would. The way I see it is they are both doing just about the same thing, apart from one subtracting from the balance an one adding to it. They both are manipulating the balance, printing out a string with the change being made to the balance, and they are both printing out the updated balance. Why should there be any difference between the two? Why shouldn’t they both be int? Or both be void and get rid of the return statement under withdraw… Thanks.
The reason for one returning something and the other not will have something to do with the criteria that the functions must match-if you could provide a link to the CC exercise, I my be able to help more.
Sorry, new here so I think I have the right link this time. Thought I had the right one before. Let me try to clarify what I’m not understanding a little bit. I can run the code the way it is and it will work. I can run the code with both methods (deposit and withdraw) as “void” and it will work, without return. I can even run both as ints and it will work. Whats the point of it? Why not just run all methods as void? Why mess around with int, double, string, etc. return types?
It’s all about what you want/need to do. You can think of functions as human actions.
Sometimes it’s good enough change something mentally (eg. oh, this type of bread is delicious, I will remember its name
good_bread = baguette, and sometimes you need to do things verbally (someone asks you what was that new bread: you tell them baguette
return good_bread). And other times you want to buy that baguette (
from store, purchase(good_bread))
All those things (remember, tell, purchase) acted on a different type (bread, but can be different things: int, double, string). For example purchase might have 2 parameters, food type and quantity
purchase(good_bread, 4) would buy 4 baguettes.
It’s a little silly of an analogy but I hope it gets the point across.
The types are important because to say 5 + 5 should equal 10 (not 55), but to say hi + how are you does mean “hi how are you”. Java needs to be explicitly told this.
Thanks for the reply pita, but I still am having some trouble with the example I linked. Lets say I change “public int withdraw” to “public void withdraw” and delete the return amountToWithdraw line. You will get the same output as if you had the return statement with the int return type. How could the return type of int be doing anything meaningful here if the output doesn’t change? Obviously the two versions of code are different (one as void and one as int with the return statement) so what is happening? It seems to me on the surface nothing changes. Whats happening underneath in the logic of the code, or the semantics? In what case would it make a difference?
Lets say I change “public int withdraw” to “public void withdraw” and delete the return amountToWithdraw line. You will get the same output as if you had the return statement with the int return type.
Not quite the same output. For this banking example you have to think a little bit outside this code, as to what a bigger simulator would look like.
- Notice, checkBalance doesn’t actually affect any value, only prints.
- Deposit does change a value which depends on your input parameter. Just like when you deposit something in the bank, you don’t get anything back.
- Withdraw changes a value, and you get something back. In a fuller program you might have a class Wallet that takes in this value. For this reason you need it to return a value. In the context of this local code it doesn’t make a difference, but in the sense of how a bank works, it makes sense.
My analogy was a long-winded way of trying to say the same thing. But only because abstractions can help in seeing the “big picture” of what your code is going to do.