Function return

New to Codeacademy so I do apologize if this is on the wrong part of the forum.

I think I somewhat understand the return line of code in the functions exercise but, can someone clarify it in Barney terms what exactly that does ? Return is just the end product of the finished function once it has completed it’s steps ? This is someone coming from a zero experience background in this field. Sorry if this was not exactly clear.

Thanks

I suggest posting these type of questions in the Get-Help category actually. Maybe adding a language specific tag as well.

If you are asking what return does, when a function is run, it will produce something. The return line just returns that “something”.

def return_number_four():
  return 4
print(return_number_four())#4

def return_number_times_6(num):
  return num*6
print(return_number_times_6(4))#24

def return_list_of_numbers():
  lst=[]
  for i in range(10):
    lst.append(i)
  return lst
print(return_list_of_numbers()) 
#[0,1,2,3,4,5,6,7,8,9]

Those examples are in Python, but it is the same principle for any language.

1 Like

Think of a simple meat grinder. We put chuck steak (for example) in the top, add a little downward pressure with the wooden utensil for that purpose, hand crank for a few revolutions and out the spout comes ground chuck.

The argument is the steak; the function the grinding; and, the return is the ground meat.

def return_a_list_of_numbers(n):
    return list(range(n))

to cite the earlier example. We feed in a value for n, how many numbers we want in the list, range() returns a range object with that many data points, and list() converts that object to a list which is given back to the caller.

Also note that caller scope and function scope are not the same. The argument goes down into the function and the return comes back up to the caller.

1 Like