<jupyter_start><jupyter_text>Why are functions necessary? MotivationOne of the basic concepts of mathematics is that of functions. A function $ f $ assigns to each element $ x $ ofa set $ D $ one element $ y $ of a set $ D $. The following examples show some well known functions.Examples:* $ f (x) = x ^ 2 \text{ for } x \in \mathbb{N} $* $ s (x, y) = x + y $ for $ x \in \mathbb{R} $A function is therefore clearly defined by its parameters and the associated mapping rule(s). Functions in programmingA function in programming is structured similarly to a function in mathematics. A function in programmingusually consists of a *name*, a series of *parameters* and a set of *instructions*. Furthermore,functions usually yield some result. The main goals of functions in programming are: - to structure programs- to make programs more readable- to enable reuse. As an example consider the following small program:<jupyter_code>names = ["Christian", "Stephan", "Lukas"]

for name in names:
    print("Hallo", name)<jupyter_output><empty_output><jupyter_text>In this program the `print()` function is used to create some output. Using the`print()` function has several advantages:1. The `print` function is reusable. We already used it in several of our programs1. The `print` function makes the program more readable     - as it gives a name to some functionality (*print* in this case)    - because the details of the implementation of the functionality are hiddenThe content of this week focuses on writing functions in Python and using them to structure programs.  Functions in PythonAs already mentioned in the previous section we have already used some functions from the Python standard library. Forexample, we already used the function [`print()`](https://docs.python.org/3/library/functions.htmlprint) to createoutput or the function [`int()`](https://docs.python.org/3/library/functions.htmlint) to convert other datatypes intoan integer.Of course, as in other programming languages, it is also possible to define own functions in Python. The following cellshows the definition of a simple function `double()`. This function doubles the value of a passed parameter and returnsthe result.<jupyter_code>def double(x):
    """
    Doubles the value x
    """
    return x * 2<jupyter_output><empty_output><jupyter_text>Once the function is defined it can be called as shown in the following cell.<jupyter_code>d = double(21)
print(d)

print(double("Hello "))<jupyter_output><empty_output><jupyter_text>Functions in Python are defined using the following syntax:<jupyter_code>def function_name(parameter_list):
    """
    docstring
    """
    statements
    return output_value(s)<jupyter_output><empty_output><jupyter_text>A function in Python consists of the following components:- The keyword `def` followed by a *function name*. The function name can be used to call the function- An optional *parameter list*. The parameter list can, therefore, be empty or contain multiple parameters. Several  parameters are separated by commas- An optional *docstring (between the `"""`)*. It can be used to add a documentation for the function- The function body. The function body is a code block consisting of statements and optional return values    - As with all code blocks in Python the functional body is indented    - The function body must contain at least one statement    - The return values of the function follow the keyword `return`. Return values are optionalThe individual components a function comprises are explained in detail in the following sections. First, however, welearn how a function is called.  Function calls The cell below contains a Python program consisting of several parts. First, a function `greet()` is defined. Next, thisfunction is called two times with different parameters.<jupyter_code>def greet(name):
    return "May the force be with you, " + name


n = "Luke"
greeting = greet(n)
print(greeting)

print(greet("Christian"))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07332332332332332, 'fraction_numerical': 0.002002002002002002, 'mean_word_length': 4.818049490538574, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '504245', 'license': 'CC0-1.0', 'n_tokens_mistral': 1013, 'n_tokens_neox': 986, 'n_words': 518, 'path': 'week_5/week_5_unit_1_whyfunctions_notebook.ipynb', 'repo_name': 'ceedee666/opensap_python_intro', 'size': '3996'}