Functions#

In Python, we can define functions using the def keyword. Functions are a way to group code that belongs together and can be reused. Functions can take arguments and return values.

Here is an example of a function that takes two arguments and prints their sum:

def addition(a, b):
    print(a + b)

We can call the function by passing two arguments:

addition(3, 4)
7

Return#

We can also return values from functions using the return keyword. Here is an example of a function that returns the sum of two numbers:

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

We can call the function and store the result in a variable:

result = addition(3, 4)
print(result)
7

Default arguments#

Assigning default values to arguments is a common practice in Python. This allows us to call the function without passing all the arguments. Here is an example of a function that takes two arguments and has default values for both:

def addition(a=0, b=0):
    return a + b

Now when calling the function without arguments, it will use the default values:

print(addition())
0

Or when calling the function with one argument:

print(addition(3))
3

Keyword arguments#

When calling a function, we can use keyword arguments to specify the value of each argument. This allows us to pass arguments in any order. Here is an example of a function that takes two arguments:

print(addition(b=4, a=3))
7

This is also extremely useful when we have functions with many arguments. It makes the code more readable and less error-prone. For example in NumPy we have similar functions that take the same arguments in different orders:

import numpy as np

#here the first argument defines the upper bound of the random number
randIs = np.random.randint(1000)
print(randIs)

#here the first argument defines the size of the array
randNs = np.random.randn(3)
print(randNs)
957
[ 1.34062107 -0.62741773  0.05777933]

Variable number of arguments#

Sometimes we want to define functions that can take a variable number of arguments. We can use the *args syntax to achieve this. Here is an example of a function that takes any number of arguments and returns their sum:

def addition(*args):
    return sum(args)

Now we can just pass any number of arguments to the function:

print(addition(1, 2, 3))
6

This is extremely useful when we don’t know how many arguments we will need to pass to the function. For example, the print function in Python can take any number of arguments and print them all:

print(1, 2, 3)
1 2 3

Exercise#

We have generated the following data using a linear function:

def linear(x, a=1, b=0):
    return a * x + b

dataX = (np.random.randn(100) * 2) + 10
dataY = linear(dataX, 2, 1) + np.random.randn(100) * 7

Of course in this case we know the function that generated the data, but in general we might not know it. We can define a function that takes the data and returns the parameters a and b of the linear function that best fits the data. This will be our task for today. What do we need, what do we know, and what do we want to find?

Task: Find a way to try different values of a and b and calculate the error between the data and the linear function. The error can be calculated as the sum of the squared differences between the data and the function. The function that returns the smallest error is the best fit for the data. Plot the fit and the data in the end and print the values of a and b.

In this case we don’t want to use ready to use libraries, but implement the solution ourselves.