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
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 theory and exercises enroll in our Introduction to Python course.