Python Functions: Medical Insurance project - What's the point in the "return" statement?

https://www.codecademy.com/journeys/data-scientist-ml/paths/dsmlcj-22-data-science-foundations/tracks/dsmlcj-22-python-fundamentals-for-data-science-part-i/modules/dsf-python-functions-41b0d686-60b0-469e-81aa-091a8f1cc02d/projects/ds-python-functions-project

Hi everyone…

There is an instruction to include a “return” statement in the code. I’ve just done the above project and I’ve noticed that the code works fine even when the “return” statement is removed. Why? Also, why should it be in there even though the code seems to work without it?

Any help will be soo helpful…Many thanks!

Can you paste your formatted code so we can see?

The return statement does just that–when the function is called it returns a value or an object to the caller. The return does not work outside the function.

ex:

def cat(name): #defining the function with one parameter
    return "This is my cat, " + name  #returning this when the function is called

cat("Bea")   #calling the function, passing the argument

>>This is my cat, Bea

If you try to use the return statement outside the function, it won’t work/you’ll get an error:

return "This is my cat " + name

>>File "<ipython-input-3-dc74106e388c>", line 1
    return "This is my cat " + name
    ^
SyntaxError: 'return' outside function

Print gives a value to the user as some output, whereas return, returns an object to the program when you call the function.

Additionally, if you haven’t already, you could also do some basic research on what a return does as well. It’s in the docs, etc.

Thanks for the reply lislisaj.

# Create calculate_insurance_cost() function below: def calculate_insurance_cost(age, sex, bmi, num_of_children, smoker,name): estimated_cost = 250*age - 128*sex + 370*bmi + 425*num_of_children + 24000*smoker - 12500 print("The estimated insurance cost for " + name + " is " + str(estimated_cost) + " dollars") return estimated_cost def double_the_age(age, sex, bmi, num_of_children, smoker,name): age_adjusted_cost = 250*age*2 - 128*sex + 370*bmi + 425*num_of_children + 24000*smoker - 12500 print("The estimated insurance cost for " + name + " is " + str(age_adjusted_cost) + " dollars, if " + name + " was double her age!") return age_adjusted_cost # Estimate Maria's insurance cost maria_insurance_cost = calculate_insurance_cost(28,0,26.2,3,0,"Maria") maria_insurance_cost_age = double_the_age(28,0,26.2,3,0,"Maria") # Estimate Omar's insurance cost Omar_insurance_cost = calculate_insurance_cost(35,1,22.2,0,1,"Omar") My_insurance_cost = calculate_insurance_cost(31,1,27.1,0,0,"Pablo")

The above runs as expected with or without the “return estimated_cost” & “return age_adjusted_cost” statements. Wondering why that is?

please provide the code to help you

Thanks for replying cvvalencia1991 - Just edited my last message with the code.

As I said above, it would be helpful if you also read up on the difference between print vs. return…following the links above b/c they don’t do the same things (even though on the surface it may look like they do).

The return statement exits the function and returns a value to the caller. The return statement in your function is returning the result of the calculation, estimated_cost.
When you use print() it just prints (or writes) the value; it does not return a value. You can also save a returned value, you can’t do that with a printed value.

Here’s another explanation and here and here in the forums.

Appreciate your help @lisalisaj !

1 Like

Hello @byte3678128268,

Here’s another way to look at your question…

The two functions currently print a summary of the information that was needed to produce each cost – estimated_cost and age_adjusted_cost – but if you remove the “return” statement, those numbers exist on your screen and nowhere else.

What happens when you need one or both of these numbers for a calculation?

For instance:

You need to produce an email listing the total cost AND also include information about payment plans to purchase the insurance – single payment, semi-annual payments, and quarterly payments.

Calling either function will print the cost to the screen, but you also need that number so you can do additional “stuff”.

You could change each function to do the additional “stuff”, but you’d be doing it twice – once for each function.

OR…

You could create a 3rd function that takes the “returned” value from either function and produces the email along with the numbers for the three types of payments.

This is one example of why you might need a function to return a value in addition to simply printing something to the screen.