PYTHON FUNCTIONS

Functions are a very important aspect of Python programming. They provide a way to structure your code meaningfully and also to make parts of it reusable.

Think of a function as being like an instruction manual sitting on the shelf. It contains instructions, but they are not used until the book is taken off the shelf and someone reads and applies them. So when you are writing a function, it is really a definition of a sub-program (a program within a program) which can be run (called) from elsewhere in the overall program.

PROCEDURES WITH PYTHON

Many syllabuses for Computer Science make a distinction between functions and procedures. These are closely related but there is a difference. Since procedures are arguably conceptually simpler than functions, we will start by looking at these.

Type the following code into your Python editor, spend a moment considering what you think it might do, and then run it. You may omit the comments if you like.

def my_proc():
    print("Am I sentient?")


print("About to call procedure.")
my_proc()  # We "call" a function or procedure using its name followed by ()
print("I'm back.")

What do you think happened? Can you describe it in words?

Output:

About to call procedure.
Am I sentient?
I'm back.

When my_proc was called by the statement my_proc(), the program jumped to the definition of my_proc and ran the code there. When it got to the end of the definition, program execution continued from after the function call.

Are you a teacher?

Check out my PYTHON FUNCTIONS AND PROCEDURES TEACHING PACK

Python functions teaching pack

PYTHON FUNCTIONS

Here’s a different program. Same procedure (no confusing pun intended) as before: type, think, run (but not out of the classroom!).

def my_func():
    return "Ibrahim"


# Output the value returned by calling my_func
print(my_func())

Functions are different from procedure in that they return a value. In this example, the string “Ibrahim” is returned by my_func when my_func is called by the command my_func(). This is different from what happened with my_proc.

Output:

About to call function and display the return value.
Ibrahim
I'm back.

Notice how earlier the string “Am I sentient?” was printed by the print statement within the definition of my_proc. What do you think would be the output of the following code:

def my_func():
    return "Ibrahim"


# Output the value returned by calling my_func
print("About to call function and display the return value.")
my_func()
print("I'm back. Did something happen?")

Here, when we called my_func with the command my_func(), the string “Ibrahim” was returned, but nothing was done with it. No print statement displayed it, for example.

It is important before you proceed further that you really understand the difference between the 3 examples given so far. If you don’t, you are liable to get even more confused as we move forward, so take the time to really grok what’s we’ve done here. To help with this, you can write your own functions and procedures, and call them to see if they behave as expected. If you are stuck for ideas, try some simple mathematical operators. For example, a procedure could print the result of adding 7 and 8, or a function could return this result and your could display it using a print statement.

PYTHON FUNCTION/PROCEDURE ARGUMENTS

The real power of functions becomes apparent when we start to use function arguments. These allow us to apply the same function to different inputs each time we use it.

Type and run the following code.

def my_proc(arg1, arg2):
    print("arg1:", arg1, "arg2:", arg2)


my_proc("rabbit", "kangaroo")

What do you expect the output to be?

How do you think this happened?

When we called my_proc with the specific arguments of "rabbit" and "kangaroo", the procedure assigned these values to arg1 and arg2 for the duration of the execution of the procedure.

It is as if we had done this instead:

def my_proc():
    arg1 = "rabbit"
    arg2 = "kangaroo"
    print("arg1:", arg1, "arg2:", arg2)


my_proc()

but that would be a lot less powerful, as we can’t reuse the procedure with different arguments.

Here’s a slightly more useful example to help you see how function/procedure arguments work:

def add_them_proc(a, b):
    print(a, "+", b, "=", a + b)


add_them_proc(3, 7)

Are you starting to see how useful arguments can be? If we just change the arguments in the call to add_them, to, say, 20 and 15, like so: add_them_proc(20, 15), we get

20 + 15 = 35

The difference with functions vs procedures is the same as before – functions simply return a value while procedures don’t.

What do you think will be the output for the following function definition and function call?

def add_them_func(a, b):
    return a + b


print("3 + 7 = ", end="")  # end="" just keep output from going to the next line.
add_them_func(3, 7)

Did you predict correctly?

Here the return value was not displayed, as we didn’t explicitly tell the program to do something with it. To print the answer, we would have needed to use print(add_them_func(3, 7)) instead of just add_them_func(3, 7).


There is quite a lot of material covered here, and it is really fundamental to computer programming so it is important that you keep working with the concepts and techniques involved with procedures and functions until you are confident with how they work. Practice is the key, so keep on trying things out, predicting results, making changes and trying again until you have really got this. Once you have, you will find functions and procedures will be incredibly useful tools for you as a budding Python programmer.

Happy computing!

Sharing is caring!

Leave a Reply

Your email address will not be published. Required fields are marked *