Vizard 7 » Tutorials & Examples » Python Programming » Functions
7.7

Functions

The Python language also allows you to create your own functions. Functions are like miniature programs within your script. They come in handy when you're going to do the same thing repeatedly because you can just call that function anytime you want it.

 

For example, let's create a function that multiplies variables by 3. This function will let us drop any variable we want into it and it will return the answer. Copy the following lines of code into your script:

#These first lines define a new function named newlitter.
#This function accepts one #variable which will be called 'animal'
#within the body of the function.
def newlitter(animal):
    #Multiply the given variable by three.
    newnumber = animal*3
    #Return a value.
    return newnumber

#Call the function and put the variable "pigs" into it.
# The variable "new_pigs" will be equal to the value that was returned from the function.
new_pigs = newlitter(pigs)

#Call the function again, but this time put the variable cows into it.
new_cows = newlitter(cows)

#Print the products of the function.
print("The new number of pigs is", new_pigs)
print("The new number of cows is", new_cows)

Once a function has been written into a script, you can call it anytime you want to use it.

Global and Local Variables

In the code above the variable newnumber is a local variable because it was defined inside the newlitter function. One important concept to keep in mind when you're working with functions is the difference between global and local variables. Global variables are known throughout the script while local variables are only known within a given function. In other words, a function's local variables stay within that function. The global/local distinction is important because if you try to call local variables within the main body of the script (or another function) you will get an error. Try adding the following line to the end of the script and run it again:

print(newnumber)

Because newnumber was defined inside the function it’s local to that function and when we refer to it outside of the function, the main script doesn’t know what we’re talking about. If we want a variable defined globally, we’ll need to specify that it’s a global variable at the beginning of our function:

def newAnimal():
    global goats
    goats = 7

newAnimal()
print(goats)

Along these lines, it’s important to remember that although you can access global variables within a function, any changes you make to that variable will stay within the function unless you specify that the variable is global within the function. So, try running the block of code below. Notice that although the function can access the global variable goats, the changes that it makes to that variable do not affect the global variable:

def changeVariable():
    goats = 10

changeVariable()
print(goats)

If, however, we use the global statement at the beginning of our function, the changes we make to the variable within our function will be made global:

def changeVariable():
    global goats
    goats = 10

changeVariable()
print(goats)

Speaking Vizard's language

A quick note: Tips on scripts

Looping logic

If-then logic

Functions

An example