Sub Routines, Procedures, Functions

Sub Routines, Procedures, Functions

Sub Routines, Procedures & Functions are a great way of organising your code. You can create a Sub Routines, Procedures & Function and run it as many times as you want anywhere in your code. 

Variables in Sub Routines, Procedures & Functions

Variables you create in a function are called local variables and only work in that function. There are ways to force a global variable to work in a function but that is not a good way to code. 

Where to put functions

Functions should be defined at the top of your code

Sub Routines, Procedures & Function Differences

All three are sections of code that do something. They can be run as many times as you like. They are run by typing their name.  Procedures and functions can have parameters. These are values added to the name that change how the code runs. Functions can also additionally pass  a value back into the main programme. In Python we tend to call them all functions.

Lets have a look at some. This is a very simple one with no parameters which does not pass information back into the main programme.

def pause():

    input("Press return to continue")

print("The monster creeps towards you")

pause()

#def creates the function

#Contents of the function pause()

#Further on in the code

#Runs pause() function

This would run as

>>>

The monster creeps towards you

Press return to continue

>>>

Parameters

Functions with inputs in the brackets but no useable return

Traffic Light program

import time

from time import sleep

from tkinter import *

tk=Tk()

win=Canvas(tk, width=55, height=200)

win.pack()

#Functions

def red(a):

    for i in range(a):

        red=win.create_oval(5,5,50,50, fill="red")

        tk.update()

        time.sleep(0.05)

def redb(a):

    for i in range(a):

        red=win.create_oval(5,5,50,50, fill="black")

        tk.update()

        time.sleep(0.05)

def amber(a):

    for i in range(a):

        amber=win.create_oval(5,55,50,100, fill="orange")

        tk.update()

        time.sleep(0.05)

def amberb(a):

    for i in range(a):

        amber=win.create_oval(5,55,50,100, fill="black")

        tk.update()

        time.sleep(0.05)

def green(a):

    for i in range(a):

        green=win.create_oval(5,105,50,150, fill="green")

        tk.update()

        time.sleep(0.05)

def greenb(a):

    for i in range(a):

        green=win.create_oval(5,105,50,150, fill="black")

        tk.update()

        time.sleep(0.05)

def lights():

    red=win.create_oval(5,5,50,50, fill="black")

    amber=win.create_oval(5,55,50,100, fill ="black")

    green=win.create_oval(5,105,50,150, fill="black")

#run functions

lights()

red(30)

redb(1)

amber(10)

amberb(1)

green(30)

greenb(1)

tk.mainloop()

Function with inputs in the brackets and a return

This function called multi multiplies two numbers and returns the answer

def multi(a,b):

    answer = a * b

    return answer

q1 = multi(5,100)

print(q1)

#a and b are spaces that must be filled by two bits of data.

#Times a and b and putting it into local variable answer

#Returning variable answer to main program

#Create variable q1 and put function multi with 5 replacing a and 100 replacing b into q1. Finally print q1

This would run as

>>>

500

>>>

NOTE The function returns the answer into itself which is why we need to put the function into a variable to use it.

This diagram may help to explain it more

This program uses the same function but gets the user to input the numbers.

def multi(a,b):

    answer = a * b

    return answer

num1 = int(input("Enter a whole number"))

num2 = int(input("Enter a second whole number"))

q1 = multi(num1,num2)

print(q1)

#Creates function called multi

#User puts a number into variable num1

#User puts a number into variable num2

#num1 and num2 are fed into multi in place of a and b and the answer is returned into q1. Finally q1 is printed

Global and local variables within a function

Global variables can be used anywhere in the programme, both within and outside a function. In this example although both variables are called fun one is created inside the function and is a local variable and the other is outside the function and is a global variable. They are completely independent of each other.

fun = 6

def multi():

    fun = 6

    fun = fun * 3

    print("I am a local variable called fun inside multi=",fun)

multi()

print("I am a global variable called fun outside multi=",fun)

#Global variable


#Local variable


#Print local variable


#Print global variable

This would output as

>>>

I am a local variable called fun inside multi= 18

I am a global variable called fun outside multi= 6

>>>

NOTE The global variable remains 6 despite the local variable being multiplied by 3

Using a global variable inside a function

fun = 6

def multi():

    global fun

    fun = fun * 3

    print(fun)

multi()

print(fun)

#Global variable fun setup with 6 inside it

#Function called multi setup

#Function is told that it is using global variable called fun

#Fun is multiplied by 3

#Fun is printed within function

#Function is run

#Fun is printed outside the function

This would output as

>>>

18

18

>>>

The global variable has been changed within the function. This is only needed if there are both global and local variables named the same thing.