9 Functions

When writing programs there’s frequently a need to repeat the same functionality in the code, on more than one occasion. This could be done by repeating the same sections of code, but it’s inefficient and error-prone. Instead functions can be used (also known as procedures) which are self-contained blocks of code that are executed (or called) when they are needed. Instead of repeating identical lines of statements to perform the same task, we create one function to do the job and execute it when it’s required.

For example, if you’re writing a program that often uses code to convert a temperature from fahrenheit into celcius, it would be useful to have one function do the task:

def fah2cel(floFah):
floCel = (floFah - 32) * 5/9
return floCel
floTempF = float( input ("Enter a temperature in degrees Fahrenheit: "))
floTempC = fah2cel(floTempF)
print (floTempC)

The function definition is started with the def keyword, followed by the function name, and a value sent to the function which is assigned to the variable floFah (known as a parameter.) Next there’s the indented block of code (or function body) that is executed when the function is called.

So what’s happening here? First the user is asked to input a temperature in fahrenheit. The input function returns a string which can’t be operated on with numbers, so it’s converted to a numeric float value with the float() function (this conversion is known as casting.)

On the next line we execute the function and send it the variable floTempF for it to work with (known as an argument.) The function receives the value of the variable floTempF and assigns it to floFah and then starts running its block of code. The first line converts the fahrenheit temperature into celcius and assigns it to floCel. The second line returns the value of the variable floCel back to the code which called the function. In this case it was floTempC = fah2cel(floTempF) so what happens is that floTempC is assigned the return value of the function, which is the temperature value in celsius.

Note that in the code above the function is only defined and not executed until the line where it’s explicitly called floTempC = fah2cel(floTempF) The start of execution of this program actually begins with the input line. All functions have to be defined before they can be used, and it’s a common practice to place them near the beginning of your program.

VARIABLE SCOPE

The scope of a variable is the parts of your code where the variable exists and is active – not all variables exist in every part of the program. Up until now most of the programs in the tutorial have used global variables, they’re called this because they exist in all parts of your program – their scope is global. In the function definition of the above code there was introduced a variable with a different (smaller) scope, known as a local variable. It’s called this because it’s local to the function, and it only exists within that function. To give an example of this:

def fah2cel(floFah):
intLocalVariable = 1000
floCel = (floFah - 32) * 5/9
return floCel
floTempF = float( input ("Enter a temperature in degrees Fahrenheit: "))
floTempC = fah2cel(floTempF)
print (floTempC)
print (intLocalVariable) #gives an error

Here a local variable is defined in the first line of the function. But when we try to print it on the final line of code, it results in an error because the variable doesn’t exist outside of that function.

<– back to contents