Python Beginner: I need a basic explanation on returns

I’m simply at the starting point of my coding journey. I’m trying to understand returns as a function but I simply cannot grasp the explanations I’ve seen.

Is there anyone here who would be able to give me a layman’s explanation or even guide to some resource for people in my position?

Thanks!

Sure, welcome to the forums!

I think of functions as magic boxes that do things and sometimes return outputs.
In math you might see something like y=2x be expressed as f(x)=2x.

In this case, f(x) is a function and it takes an input of x. So if I put one book into this magic box, the box will give me 2 of that book. If i put 10 dollars, it’ll give me 20 dollars (love this box).

In code that might look like:

def double_your_thing(x):
   return 2*x

print(double_your_thing("fun"))
#output 'funfun'

Sometimes there are functions that don’t return things, but when you use them they perform an action (which you might see or not see). For example, if I put money in the bank, I don’t see the physical money anymore. The bank performs the action of storing the money.

savings =0
def deposit_money(cash):
  savings += cash

deposit_money(10)
print("$" + savings)
#output: $10

Finally, sometimes you have functions that don’t output anything and also don’t take input, they just perform an automatic action.

def greeting():
  print("Hello there!")

greeting()
#output "Hello there!"
#note since greeting() already prints, if you write
#print(greeting()) you won't get your desired output
3 Likes

A function that takes in a one inch thick chuck steak and returns a tray of 1" X 1" cubes of meat.

def cube_steak (steak):
    # trim excess fat
    # remove any bone
    # cut into 1" strips
    # cut strips into 1" cubes
    # place cubes on a tray and return to prep table

A function to grind trimmed and cubed meat to make ground chuck

def meat_grinder(meat):
    # place cube in grinder receiver
    # press down with wooden press tool
    # turn handle slowly
    # let ground meat fall onto a tray
    # repeat above steps until all cubes are ground
    # return tray of ground meat to the prep table

Both of these functions return the outcome of the steps carried out in each one.

2 Likes

Now I’m just hungry…

3 Likes

Y’all are amazing, thank you!!! I was able to get myself unstuck. I would like to share the exercise I was working on and what I was able to do in order to finish the project. I was struggling to get my_age to hold it’s value.

Instructions:

  1. The function calculate_age in script.py creates a variable called age that is the difference between the current year, and a birth year, both of which are inputs of the function. Add a line to return age .
  2. Outside of the function, call calculate_age with values 2049 ( current_year ) and 1993 ( birth_year ) and save the value to a variable called my_age .
  3. Call calculate_age with values 2049 ( current_year ) and 1953 ( birth_year ) and save the value to a variable called dads_age .

Print the string "I am X years old and my dad is Y years old" to the console, with my_age where the X is and dads_age where the Y is.`
-------------------------------------code-------------------------------------
I am adding below the code I did:

def calculate_age(current_year, birth_year):
age = current_year - birth_year
return (age)
current_year = 2049

#originally put as ‘birth_year: 1993’ to calculate my own birth year, then I couldn’t figure out how to get it to hold the value as my_age so I changed the birth year to 1953 in order to calculate dads_age
birth_year = 1953

#my age - uses birth year 1993 ( later changed to 1953 to get dads_age so I added a result to hold the value
my_age = (current_year - birth_year)
my_age_result = 56
print(str(my_age_result))

#dads
dads_age = (current_year - birth_year)
print(str(dads_age))
dads_age_result = 96

#printing statement
print ("I am ", my_age_result, "years old and my dad is ", dads_age_result, “years old!”)

What I got:
56
96
I am 56 years old and my dad is 96 years old!


Did I format the code in the correct way? Especially when changing my_age to dads_age and having to use both in the final statement…I was struggling pretty hard with understanding why I couldn’t just get the code to remember my_age

Thank you for your contributions!! Without you all, I would’ve still been stuck!

1 Like

Hi, great!

When you past your code, you have to press the Screen Shot 2020-12-18 at 7.22.47 AM button and paste it between the tick marks Screen Shot 2020-12-18 at 7.22.53 AM to preserve formatting (otherwise it’s hard to tell what’s happening, specially in python).

In python it’s always a consideration to consider how indentation affects the scope of your code.

For example:

def something():
  number = 1000

print(number)
#this will throw an error because number doesn't exist outside the scope of something()
1 Like