Back to the lesson plan

4. Functions

Learning outcomes

At the end of this lesson, you will be able to:

  • Understand the syntax of function declaration
  • Create python functions for various applications

Function declaration

The invention of functions was a major milestone in the history of programming languages. Functions gave birth to the procedural programming paradigm during the 1950s-1970s. You can find more info about this interesting topic here: https://en.wikipedia.org/wiki/Procedural_programming.

What's a function? A function is a block of code that can be invoked on demand. To understand the importance of functions, let say you want have hundreds of lines code that you want to invoke repeatedly in your program. If you wish to use these lines of code, then you can copy-paste them wherever you want to invoke them. However, if you need them in multiple places in the code, then copy-pasting might not be the most efficient way to write code.

Alternatively, we place these lines of code into a python function. The syntax for function definition is as follows:

def my_function():
    some_statements

Here we defined a new python function that is named my_function. This function can be called by simply typing

my_function()

This function definition has three parts: a function name, the () and some_statements. When you call the function, the computer will execute the statements in some_statements block. Let's have an example.

def print_hello():
    print('Hello, I need my coffee!')

print_hello()

Result:

Hello, I need my coffee!

So that's how we call a function: we use its name, and add the () to it. The result is: the block of statements, here containing just one print statement, inside the function is executed.

Next, what is () for anyway? This is how we can input data into the function, so that the function can process this data to do something useful (hopefully!) with it.

We use () by putting an argument list inside it, as follows:

def my_function(x):
    some_statements

Here, x is a variable that will be used by the function. Let's write a function that receives some variable x and then prints it out:

def print_x(x):
    print(x)

print_x(2)

Result:

2

The function print_x receives an argument x and passes it to the print function. We can also pass several arguments to a function, and we must separate them by the comma. For example, this function receives two arguments x and y, adds them up and prints the answer.

def print_sum(x,y):
    print(x+y)

print_sum(2,3)

Result:

5

If a function is defined to receive a specific number of arguments, we have to provide that same number of arguments to the function when we call it. Otherwise we will upset python: we get an error!

The return statement

So far, each of the functions we've created performs a task then stops. Functions can also perform something very useful: they can return data. For example, a function can receive arguments, process them, and then return the result of processing. Let me introduce the return statement in python functions.

def my_function(x):
    some_statements
    return something

For example, let's write a function that receives two arguments and then returns their sum:

def the_sum(x,y):
    return x+y

print(the_sum(2,3))

Result:

5

A function can also return multiple things, separating them by commas. For example, the function the_math() below returns the sum and difference of the two arguments.

def the_math(x,y):
    return x+y,x-y

print(the_math(2,3))

Result:

(5, -1)

So you recognise that parentheses? Functions that return more than one value will return a tuple of values.

Default values

I said above that, when passing values into the arguemnts list, we have to respect the order and number of arguments.

Sometimes you want to give some default value. Doing this will let us ignore passing values for those arguments.

For example, let's write a function that receives a string s and a number n to return the slice of the s starting from

index n. If the user doesn't provide n, the function will set n to zero by default. Here is the code.

def print_n(s,n=0):
    print(s[n:])

print_n('Hi there!')

Result:

Hi there!

We didn't have to put a value for n here, since the function can safely assume a value for it by setting n=0.

Scope

When you declare a variable in python, you can use that variable at any part of your code after declaring the variable.

If your variable is declared within a the block of a control statement, for example, it can still be seen outside of the block.

However, when you define a variable inside a function's block the situation will be different. A function in python defines a scope for variables. Variables declared inside the function block are only accessible within the block i.e. scope.

Here is an example of a code that will give an error.

def a():
    g=3

print(g)

Result:

None

You can also define a function inside a funtion. The inner function also lives in the scope of the outer function. So the code below returns an error.

def a():
    def b():
        pass

print(b)

Useful string functions

  • s.upper() and s.lower() turn the string to upper case and lower case, respectively
  • s.strip() strips the string from any spaces the lie to the left of to the right of non-space characters
  • s.split() splits string s into words, and returns them as element in a list
  • s.replace(a,b) replaces all ocurrances of a with b in the string s
  • s.join() joins all members of a collection into one string, separated by the the string s
  • s.isnumeric() checks if s is numeric

Here is a demonstration of these functions.

a = 3
print('Is a an int?',isinstance(a,int))

a = 'this is some text'
print(a.upper(),a.lower())

a = ' lots of trailing spaces. '
print(a)
print(a.strip())
print(a.split())

a = 'abcddefdd'
print(a.replace('d','x'))

print('abc'.join(['x','y','z']))

Result:

Is a an int? True
THIS IS SOME TEXT this is some text
 lots of trailing spaces. 
lots of trailing spaces.
['lots', 'of', 'trailing', 'spaces.']
abcxxefxx
xabcyabcz