Creating Functions

Overview

Functions are useful blocks of code that you may want to run multiple times. Functions allows code to be structures in a readable and maintainable way.
Functions are created by the "def" keyword, followed by a unique name, brackets that contain any variables to be passed to a function. Then terminated with a colon (:). The code that is contained in the function is required to be indented.

Example 1 - Basic Function with no Variables

This example requires the total cost of a meal, using the input() function.
def bill_total():
    #Gets the user to input the 
    total = input("Please enter the bill amount : ")
    return total

print (bill_total()) # We have to call the function for it to run
    
This returns, we have entered 34 as the meal cost:
Please enter the bill amount : 34 
34 

Example 2 - Passing and Returning Variables

We have built on example 1 to include an extra function which is passed the bill total. Note that this is automatically assigned to a string data type, so we must covert it into a float.
bill_total() is called, this requests a value from the user which the function returns, this is then stored in the variable bill.
The follow line, tip is called and passed the variable bill. This function the calculates 15% of the bill and returns that value. This is stored in the variable tipamount. This is then printed in the next line. note that this is converted to a string to allow it to be concatenated to the other text.
def bill_total():
    #Gets the user to input the 
    total = input("Please enter the bill amount : ")
    return total

def tip(bill):
    tip = float(bill) * 0.15
    return tip
    

bill = bill_total() # We have to call the function for it to run
tipamount = tip(bill)
print ("Tip required is : " + str(tipamount))
Returns:
Please enter the bill amount : 34
Tip required is : 5.1

Example 3 - Returning multiple variables

It is possible to return multiple variables.
def bill_total():
    #Gets the user to input the 
    total = input("Please enter the bill amount : ")
    return total

def tip_tax(bill):
    tip = float(bill) * 0.15
    tax = float(bill) * 0.20
    return tip, tax
    

bill = bill_total() # We have to call the function for it to run
tipamount, taxamount = tip_tax(bill)
print ("Tip required is : " + str(tipamount))
print ("Tax required is : " + str(taxamount))
Returns:
Please enter the bill amount : 34
Tip required is : 5.1
Tax required is : 6.800000000000001
Though for clarity of multiple variables it may be clearer to return this as a list, dictionary or tuple.



Related Sections

Python - Learn the python from the basics up. This fast track example code course will get you creating powerful python programs in no time.
Tkinter - Learn the key features of the tkinter to allow you to create user interfaces for your python programs.


Related Pages

Pickle - The pickle library allows a program to save data in a binary file.
Python - List Comprehension - Create powerful list comprehension expressions.
Python - Generators - Learn about generator statements.
Python - Data types - The learn about the key datatypes in the python language.
Tkinter - Combo Box - This is a short example showing how to use combo boxes in a GUI.
Tkinter - Radio Buttons - A short example of how to create a GUI with radio buttons.
Tkinter - Canvas - Canvas allows for a range of shapes to be plotted, such as circles, squares, lines