The order of return value in a function

`Hi everyone,
What cause the difference when I change the line of “return estimated_cost” in the below function?

1/

# Create calculate_insurance_cost() function below: 
def calculate_insurance_cost(name, age, sex, bmi, num_of_children, smoker):
  estimated_cost = 250*age - 128*sex + 370*bmi + 425*num_of_children + 24000*smoker - 12500
  return estimated_cost
  print('The estimated insurance cost for' + name + 'is' + str(estimated_cost) + 'dollars.')

# Estimate Maria's insurance cost
calculate_insurance_cost(name = 'Maria', age = 28, sex = 0, bmi = 26.2, num_of_children = 3, smoker = 0)
# Estimate Omar's insurance cost 
calculate_insurance_cost(name = "Omar", age = 35, sex = 1, bmi = 22.2, num_of_children = 0, smoker = 1)

=> generate no value

# Create calculate_insurance_cost() function below: 
def calculate_insurance_cost(name, age, sex, bmi, num_of_children, smoker):
  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
  
# Estimate Maria's insurance cost
calculate_insurance_cost(name = 'Maria', age = 28, sex = 0, bmi = 26.2, num_of_children = 3, smoker = 0)

# Estimate Omar's insurance cost 
calculate_insurance_cost(name = "Omar", age = 35, sex = 1, bmi = 22.2, num_of_children = 0, smoker = 1)

=> generate value

return passes the data back to whatever called it, meaning that whenever a return is reached in a function, the function ends, and no more code within the function will execute:

def func():
  return "this ends the function"
  print("this won't print")

def another_func():
  if True:
    return "This will only end the function if the condition is true"
1 Like