# Python: Functions ([home](https://github.com/alexhkurz/introduction-to-programming/blob/master/README.md) ... [previous](https://hackmd.io/@alexhkurz/Bk1byMf2L) ... [next](https://hackmd.io/@alexhkurz/SJN2udq3I)) In the previous session, we tested the code below for different values of `n`. if n % 2 == 0: print(n,'is even') elif n % 2 == 1: print(n,'is odd') else: print(n, 'is not an integer') This was quite tedious as we replaced by hand the name `n` by different numbers. Everything that is boring and tedious in programming should be automatized. Do it once by hand. Do it a second time by copy-paste. The third time write a program doing it for you. One great way of not having to copy-paste code are functions. So instead of replacing the name `n` by hand we now treat it as a variable and use it to define a function that we can call and reuse instead of changing the code by hand each time. def parity(n): if n % 2 == 0: print(n,'is even') elif n % 2 == 1: print(n,'is odd') else: print(n, 'is not an integer') The function `parity` checks whether a number is even or odd. Paste the code above into the Python REPL. Then call the function `parity` with various arguments such as parity(2) parity(3) parity(2.5) ... Instead of working with the REPL, you can also download the code [`parity.pl`](https://github.com/alexhkurz/introduction-to-numbers/blob/master/src/parity.pl) and run with different values of `n`. Notice that thanks to having a function, you need to change the value of `n` in one place only. **Homework/Activity:** - Write a function `hello()` that prints `Hello, here is Python`. - Implement the mathematical function $f(x)=x^2+2x+1$ in Python. Use the Python function to compute $f(-2)$, $f(-1)$ and $f(0)$. - Invent some of your own functions.