COMP3200 Anonymous Questions and Answers Keyword Index |
This page provides a keyword index to questions and answers. Clicking on a keyword will take you to a page containing all questions and answers for that keyword, grouped by year.
To submit a question, use the anonymous questions page. You may find the keyword index and/or top-level index useful for locating past questions and answers.
Keyword reference for return-vs-print
2024 |
Submission reference: IN3657
I've been trying to understand the concept of the return keyword, and while I've done some research, I'm still a bit confused about when and how to use it. Here's what I think I've understood so far:
The return keyword is used within a method to send a value back to where the method was called. For example, if I have a method add(int a, int b) that adds two numbers, I can return the result of that addition. The value that gets returned is then stored in a variable or used immediately in the calling code.
For instance: int x = y.add(5, 3);
In this case, the numbers 5 and 3 are passed into the add method, which performs the addition, and the result is "returned" and assigned to the variable x.
I understand that using System.out.println() would display the result on the terminal but not store it, as the printed output is erased after the program finishes running. In contrast, the return value remains available in the variable it was stored in, and it doesn't necessarily have to be stored in a new attribute.
However, I am still a little confused about the difference between return and print, and when it's more appropriate to use one over the other. Could you confirm if my understanding is correct and clarify when exactly I should use return instead of just printing the value?
Thank you for your help!
Your understanding is very good. You have described the differences well.
Printing is only useful for human readers interacting with a program. It is much more common for objects to return values from methods rather than print data out. Values that are returned can then be used in the program to do other things. At this stage of the course, we are only looking at relatively small programs, so the significance of returning will not be completely clear. But here is a bit of more advanced code that hopefully shows a little bit of what you can do with return values.
Suppose we want to calculate the average module mark for a student. If a Student object stores a list of their module marks then we can write code that asks the Student object to return the list of marks, and then we can do some calculations with them. The code might look something like this:
int total = 0; int numberOfModules = aStudent.getNumberOfModulesTaken(); // Loop over all the module marks to find the sum (total) of them. for(int moduleMark : aStudent.getModuleMarks()) { total = total + moduleMark; } int average = total / numberOfModules; System.out.println("The average mark is " + average);
The methods getNumberOfModulesTaken and getModuleMarks both return results that are then used in the bit of code that called them. That allows the code to do some calculations and finally print the average for a human reader.
Keywords: return , print , return-vs-print
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License. |