return is how a function’s result is passed back to the caller.
Most programs are organized into functions (sometimes called procedures or subroutines). You might have the following command:
x = compute_average(list)
computer_average() might look like this:
int compute_average(List list)
total = 0
count = 0
for each item in list
total = total + item
count = count + 1
return total / count
In this case, return is saying “I’m all done, here is the average of the list you gave me.
Latest Answers