Welcome Friends!
Today we are going to discuss Functions. Functions are a set of instructions or a program that performs a specific task. Then one may ask, how is it different from what we were doing until now? Well, a function is used to segregate the tasks and avoid duplication of activity. Let's take a simple example to illustrate its use.
Assume, if I want to calculate how much money we can save at the end of 2 years. The program will ask the user for the principal amount and rate of interest and program will calculate simple interest and will display the amount and then will again ask the user again for the amount invested for the second year and will display the amount saved by end of 2nd year. A typical approach will be,
Get the rate of interest from the user for 1st Year
Calculate Simple Interest
Calculate the Amount
Display the amount and Interest
Get the principal from the user for 2nd Year
Get the rate of interest from the user for 2nd Year
Calculate Simple Interest
Calculate the Amount
Display the amount and Interest
Calculate Simple Interest
Calculate the Amount
Display the amount and Interest
Get the principal from the user for 2nd Year
Get the rate of interest from the user for 2nd Year
Calculate Simple Interest
Calculate the Amount
Display the amount and Interest
Let's see how a function can help us here.
Call function 1 to get user principal and rate of interest
call function 2 to calculate and display simple interest and Amount
Call function 1 to get user principal and rate of interest
call function 2 to calculate and display simple interest and Amount
function 1:
Get the principal from user
Get the rate of interest from the user
function 2:
Calculate interest
Calculate the Amount
Display Amount and Interest
call function 2 to calculate and display simple interest and Amount
Call function 1 to get user principal and rate of interest
call function 2 to calculate and display simple interest and Amount
function 1:
Get the principal from user
Get the rate of interest from the user
function 2:
Calculate interest
Calculate the Amount
Display Amount and Interest
Syntax:
The function is defined using the keyword def, and it will return the value with the return keyword
The function is defined using the keyword def, and it will return the value with the return keyword
def function_name(arguments):
do something here ...
return value
Let's now write our program using the function.
do something here ...
return value
Let's now write our program using the function.
def get_details_from_user(): principal_amount = float(input("Enter the principal amount: ")) rate = float(input("Enter the rate of interest: ")) return principal_amount, rate def calculate_and_display_simple_interest(principal_amount, rate): interest = principal_amount * rate * 1/100 amount = interest + principal_amount print("Your interest on $" + str(principal_amount) + " is " + str(interest)) print("Your amount at the end of year is: " + str(amount) + "\n") def main(): # get user details for 1st year from user principal_amount, rate = get_details_from_user() # Calculate and display the amount and simple interest calculate_and_display_simple_interest(principal_amount, rate) # get user details for 2nd year from user principal_amount, rate = get_details_from_user() # Calculate and display the amount and simple interest calculate_and_display_simple_interest(principal_amount, rate) main()
I hope you have enjoyed this. In our next post, we will discuss exception handling. Stay tuned.
No comments:
Post a Comment