In programming, a function is a named sequence of statements that performs a desired computation. You can write a function by defining it in the Python language, which involves giving it a name and specifying the computations. Later, you call the function by its name.
The ability to write functions is perhaps one the elements of more active programming, as opposed to just using Python. There are several reasons to write functions:
On DataCamp, you learned about how to import packages and call functions from this packages. For example:
import math x=math.sqrt(9) print(x)
from math import sqrt x=sqrt(9) print(x)
Your first function that will print the current date on the screen. For this, we will need to import the time package so that we can use it inside the function:
We write the function as follows. The empty parentheses after the function name indicate that it takes no inputs. Note that the second line in indented with a tab (or four space). This makes Python understand that the indented block of code is part of the function. Jupyter adds the indentation automatically.
def today(): print('Today is ' + time.strftime("%d/%m/%Y"))
To call the function:
Today is 18/08/2016
By defining the function and running the corresponding the cell, Python will load it in the the memory for repeatead use. The type function from the standard Python library allows us to verify the type of each object that we create or load in our session:
Functions can take one or multiple arguments:
def hello(name): print('Hello ' + name) def sum_two_numbers(a,b): print('The sum is ' + str(a+b))
You should test these two functions in a new cell. It is also possible to specify default values for arguments, such that that they become optional.
def hello_default(name='nobody'): print('Hello ' + name) hello_default('Marcel') hello_default()
Hello Marcel Hello nobody
The functions that we created so far are called void functions, because they do not return any results (unlike the square root function from before). To return a result we define a function as follows.
def get_today(): return time.strftime("%d/%m/%Y") date=get_today() print(date)
A function will stop executing as soon as it reaches a return statement, which does not need to be the last line in the function. A function can have multiple return statements, as the following example illustrates.
def is_positive(number): if number>0: return True else: return False print(is_positive(1)) print(is_positive(-1))
The above function uses an if/else block, which is a very important programming concept that you will study in more detail in the next module.
Write a function name right_justify that takes a string named S as a parameter and prints the string with enough leading spaces so that the last letter of the string is column 70 of the display (Think Python 3-1). Use the hints below.
Write a function distance that takes as an input four numbers $x_1$, $x_2$, $y_1$, $y_2$ and returns the number given by the formula
For example, when $x_1=2$, $x_2=-1$, $y_1=-2$, and $y_2=2$ the result is $5$.
A function object is a value that you can assign to a variable or pass as an argument. For example, the do_twice function below is a function that takes a function object as an argument and calls it twice:
def do_twice(f): f() f()
Here is an example that uses do_twice to call a function named print_spam twice.
def print_spam(): print('spam') do_twice(print_spam)
(a) Type this example into a cell and test it.
(b) Modify do_twice such that it takes two arguments, a function object and a value, and calls the function twice, passing the value as an argument.
(c) Define the print_twice function as in the cell below.
(d) Use the modified version of do_twice to call print_twice twice, passing 'spam' as an argument.
(e) Define a new function called do_four that takes a function object and a value and calls the function four times, passing the value as a parameter. There should be only two statemens in the body of this function, not four.
(Think Python 3-2)
def print_twice(word): print(word) print(word)
Many mathematical functions that appear in statistics have a recursive structure. We can also define recursive functions in programming. Based only on what we have seen in this notebook, write a function to compute the factorial of an integer number: