Updated on 15 Jul 2021

Using a Python Function in Another Function Exercise

Martin Ganchev
Published on 12 Nov 2018 2 min read

Using a Function in Another Function Exercise:

Define a function that will estimate your daily salary, based on your hourly wage.

Then define a second function that estimates your monthly salary based on your daily salary.

For simplicity, we are ignoring bonuses, fees and taxes, and assuming 22 8-hour-long workdays within the month.

The Tutorial you may need: An Introduction to Python Functions

python function in another function

 

We start by defining a function daily_sal that returns daily salary when given hourly wages. This function would take a single parameter wage and return a value 8 times greater.

def daily_sal(wage):
 
    return 8*wage

Then we define a second function monthly_sal that returns monthly salary when given hourly wages.

In our case, the monthly salary equals 22 times the daily salary. Since we already defined a function that calculates daily salary based on hourly wages, we simply call that function and plug in the parameter value. Therefore, the return line for this function would be as follows: ‘return 22*daily_sal(wage)’, where ‘daily_sal’ is simply the name of the first function we defined.

def monthly_sal(wage):
 
    return 22*daily_sal(wage)

To test the accuracy of our work, we define a variable and assign a numerical value to it. Afterwards, we call the function using this variable and check whether our code works as intended.

Easy.

Okay, good job! For more exercises click here.

Learn data science with industry experts

Try For Free
Martin Ganchev

Instructor at 365 Data Science

Martin holds an MSc degree in Economic and Social Sciences from Bocconi University. His diverse academic and research experience combined with his friendly and explanatory approach to teaching have made him one of the most beloved instructors on our team. Some of the courses he has authored include: SQL, SQL + Tableau, SQL+Tableau+Python, Introduction to Python, Introduction to Jupyter, to name a few.

Top