Medical Insurance Project - How many ways to call a function?

Hello everyone, I am currently taking a course in Data Science and I am dealing with Python. As part of the python program, there is an excercise where we can use a function to calculate someone’s medical insurance cost. The code looks like this

# 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 cost for ' + name + ' is ' + str(estimated_cost) + ' dollars')
  return estimated_cost

# Estimate Maria's insurance cost
maria_insurance_cost = calculate_insurance_cost(age = 28, sex = 0, bmi = 26.2, num_of_children = 3, smoker = 0, name = 'Maria')

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

and the output is:

The estimated cost for Maria is 5469.0 dollars
The estimated cost for Omar is 28336.0 dollars

my question is: why does the output side show both sentences? Both variables maria_insurance_cost and omar_insurance_cost have not been printed using the print() function, so in my mind, in order to display both sentences, I should add the code below: print(maria_insurance_cost) and print(omar_insurance_cost). However, I cannot understand how this part of the code (see below) is enough to call the function, while if I create a random variable (e.g. dog_name = ‘Fuffy’) it will not be displayed unless I use the print() funcion.

# Estimate Maria's insurance cost
maria_insurance_cost = calculate_insurance_cost(age = 28, sex = 0, bmi = 26.2, num_of_children = 3, smoker = 0, name = 'Maria')

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

Hope the question is clear and thank you very much for your help :slight_smile:

You’re being clear. I get it that it can be confusing at times. :slight_smile:

You defined the function and have the following parameters:
def calculate_insurance_cost(age, sex, bmi, num_of_children, smoker, name):

You have a print & a return statement. You called the function calculate_insurance_cost and passed arguments through it(age, sex, bmi, etc), and assigned then it to a variable, maria_insurance_cost & omar_insurance_cost.
When you call a function, by passing arguments through it, the logic you have supplied inside the function is applied.

1 Like

Thank you for the reply! Just to make sure I understood correctly:

  1. When I assign a variable to a function, with parameters (sex, age, etc) in it, it is automatically called
  2. As the function contains the print statement in it, when called it will be displayed in the output section

Is it correct?

Thank you again for your help :slight_smile: I am very new to the coding world

1 Like

To get a better understanding of what you wrote…try a few things with the code by commenting out parts of the code and see what happens to it.

  • what happens when you comment out the print() statement inside the function (and keep the variables for Maria and Omar)?

  • what happens when you add print(calculate_insurance_cost(age = 28, sex = 0, bmi = 26.2, num_of_children = 3, smoker = 0, name = 'Maria')) after the variable declarations?

  • uncomment that print() inside the function, etc.

  • what happens when you remove the variable maria_insurance_cost = and just call the function?

Etc, etc.

  1. Parameters in a function definition are like empty variables. When you call the function, you passed the arguments (name = "Maria", age = 28, sex = 0, bmi = 26.2, num_of_children = 3, smoker = 0) through it.

  2. You have a variable inside the function called estimated_cost. Your print statement will print out that estimated cost and you then return the estimated cost with the return statement.

I hope that helps! :smiley:

3 Likes

It is very helpful! Thank you very much :slight_smile:

2 Likes