This page was generated from source/Jupyter/tutorials/tutorial-4-Functions in Python.ipynb. Interactive online version: Binder badge Slideshow: Binder badge

13.3.4. Functions in Python

What you will learn here:

  1. How to write simple functions in Python

  2. How to call functions

  3. How to pass variables and parameters to functions

  4. How to use return in functions

  5. How to integrate functions together

These packages are necessary later on. Load all the packages in one place for consistency

[1]:
import numpy as np
import pandas as pd

Let’s say you want a function that simply prints Hello World, you define the function:

[2]:
def your_printer():
    print('Hello World')

So it is very simple to define the function but when you run this cell nothing happens because the function has not been called. This is the handy part of functions in Python (or any other programming languages). You define a function once and then you can all it whenever you want. To call the function simply:

[3]:
your_printer()
Hello World

Congrats! you just wrote and called the first function in Python.

The application of functions goes beyond printing. You can pass variables into function and they do the job for you. For example, if you modify your_printer function to print what we pass to it:

[4]:
def your_printer(your_word):
    print(your_word)

Now let’s call this function:

[5]:
your_printer('This is your word!')
This is your word!

You can call a function multiple times, saving you time and space in coding:

[6]:
your_printer('first word')
your_printer('second word')
your_printer('third word')
first word
second word
third word

More commonly you pass a variable to a function, the function does some mathematical operations and returns the results for future use. In this case, you use return in the function. For example

[8]:
def sum_two_number(number1,number2):

    return number1+number2

This function, receives two number and returns the sum. You can use it as follows

[9]:
sum_numbers=sum_two_number(1,2)
print(sum_numbers)
3

You can use the result of the function to do other stuff. For example:

[10]:
print(sum_two_number(sum_numbers,4))
7

Now a more complex example, using the following equations:

\(q_1=x^2+x\)

\(q_2=\sin(q_1)+\frac{1}{q_1+1}\)

\(q_3=q_2*q_1+x^3\)

\(q_4=q_1+q_2+q_3+x\)

Given a value for \(x\), you want to find the value of \(q_4\). This is where functions come in handy. Let’s implement this:

[11]:
############################## q1

def cal_q1(x):

    return x**2+x

############################## q2

def cal_q2(q1):

    return np.sin(q1)+1/(q1+1)

############################## q3

def cal_q3(q1,q2,x):

    return q2*q1+x**3

############################## q4

def cal_q4(x):

    q1=cal_q1(x)
    q2=cal_q2(q1)
    q3=cal_q3(q1,q2,x)

    return q1+q2+q3+x

now we can calculate values of \(q_4\) given any arbitrary value for \(x\)

[12]:
cal_q4(12)
[12]:
1758.5598148460792
[13]:
cal_q4(1)
[13]:
7.727892280477045
[14]:
cal_q4(5)
[14]:
130.3710196531213