2021-02-12 15:08:36 +01:00
# Introduction
2022-05-26 15:47:38 -05:00
A [`function` ][function] is a way to compartmentalize code so it can be called by name from one or more places in the program.
2022-05-26 15:47:59 -05:00
Functions are used to perform specific and repetitive tasks.
2021-10-23 08:51:52 +05:30
2022-05-26 14:24:12 -05:00
More formally: a function is any Python object to which the [`function call` ][calls] operation can be applied.
A function may be used to [`return` ][return] one or more values as a result of some operation(s), or it may be used for one or more [`side effects` ][side effects].
If a function does not specify a return value it will still return `None` .
2021-10-23 08:51:52 +05:30
2022-05-26 16:03:12 -05:00
Following is an example of a function with a side effect:
2021-10-23 08:51:52 +05:30
2022-05-26 14:24:12 -05:00
```python
2022-05-26 15:50:47 -05:00
>>> def hello():
... print("Hello")
...
>>> hello()
2022-05-29 06:45:40 -05:00
Hello
2022-05-26 14:24:12 -05:00
```
2022-05-26 15:55:36 -05:00
In the example above, the [`def` ][def] keyword is used to signal the beginning of the function definition.
2022-05-26 14:24:12 -05:00
`hello` is the name of the function.
The empty parentheses represent that no values are being passed to the function.
The body of the function is the single `print("Hello")` statement.
`print` is also a function.
The `"Hello"` in the `print` function's parentheses is known as an [`argument` ][arguments].
2022-05-26 14:59:54 -05:00
The argument is used by the `print` function to know what to print.
Note that the body of the function is indented.
The indentation is important because Python relies on it to know where that block of code ends.
The function body ends at either the end of the program or just before the next line of code that is _not_ indented.
2022-06-01 11:36:12 -05:00
Since `hello()` does not specify a `return` value, it executes its side effect - which is calling `print()` -- and then returns `None` .
2022-06-01 13:04:21 -07:00
Finally, we call the function by using its name and the parentheses - which signals to the Python interpreter that this is a _callable_ name.
2022-05-26 14:24:12 -05:00
2022-05-26 16:03:12 -05:00
Following is an example of a function with a return value:
2022-05-26 14:24:12 -05:00
```python
2022-05-29 06:45:40 -05:00
>>> def hello():
... return "Hello"
...
2022-05-26 14:24:12 -05:00
print(hello())
2022-05-29 06:45:40 -05:00
Hello
2022-05-26 14:24:12 -05:00
```
2022-06-01 13:04:21 -07:00
The body of this function has been changed from _calling_ `print` to _returning_ the `string` "Hello".
In the end, the code still prints `"Hello"` , but the side effect takes place in the _calling code_ instead of in the `hello` function.
2022-05-26 14:24:12 -05:00
If the parentheses were left off from calling the `hello` function (e.g. `print(hello)` ), then `print` would output something like
```
< function hello at 0x0000026E18E93E20 >
```
2022-06-01 13:04:21 -07:00
It's important that, even if a function accepts no arguments (_i.e. has no parameters_), the empty parentheses must be used to _call_ it correctly.
2022-05-26 14:24:12 -05:00
This is different from a language such as Ruby which does not require empty parentheses for calling a function without any arguments.
A function can define zero or more [`parameters` ][parameters]. A parameter defines what argument(s) the function accepts.
2022-05-26 16:03:12 -05:00
Following is an example of a function which accepts an argument:
2022-05-26 14:24:12 -05:00
```python
2022-05-29 06:45:40 -05:00
>>> def hello(name):
... return f"Hello, {name}"
...
>>>print(hello("Bob"))
Hello, Bob
2022-05-26 14:24:12 -05:00
```
The parameter is defined as `name` .
`"Bob"` is the argument which the program passes to the `hello` function.
2022-06-01 13:04:21 -07:00
The function _returns_ the `string` "Hello, Bob".
2022-05-26 14:24:12 -05:00
What if someone calls `hello` without passing an argument?
2022-06-01 13:04:21 -07:00
The program throws an error:
```python
print(hello())
Traceback (most recent call last):
Input In in < line: 1 >
print(hello())
TypeError: hello() missing 1 required positional argument: 'name'
2022-05-26 16:05:40 -05:00
If we don't want the program to error with no argument (_but want to allow the calling code to not supply one_), we can define a [default argument][default arguments].
2022-05-26 15:53:38 -05:00
A default argument defines what value to use if the argument is missing when the function is called.
2022-05-26 14:24:12 -05:00
2022-05-26 15:57:06 -05:00
Following is an example of a function with a default argument:
2022-05-26 14:24:12 -05:00
```python
2022-05-26 15:53:57 -05:00
>>> def hello(name="you"):
2022-05-29 06:45:40 -05:00
... return f"Hello, {name}"
...
2022-05-26 15:53:57 -05:00
>>> print(hello())
Hello, you
2022-05-26 14:24:12 -05:00
2022-05-26 16:03:12 -05:00
```
2022-05-26 15:57:38 -05:00
The `name` parameter is given the default argument of `"you"` , so the program outputs `Hello, you` , if not passed a `name` argument.
2022-05-26 14:24:12 -05:00
For more about function arguments, please see the [function arguments][function arguments] concept.
[arguments]: https://www.w3schools.com/python/gloss_python_function_arguments.asp
2021-10-23 09:02:25 +05:30
[calls]: https://docs.python.org/3/reference/expressions.html#calls
2022-05-26 14:24:12 -05:00
[def]: https://www.geeksforgeeks.org/python-def-keyword/
[function arguments]: ../function-arguments/about.md
2022-06-05 16:41:26 -07:00
[function]: https://docs.python.org/3/glossary.html#term -function
2022-05-26 14:24:12 -05:00
[parameters]: https://www.codecademy.com/learn/flask-introduction-to-python/modules/learn-python3-functions/cheatsheet
[return]: https://www.geeksforgeeks.org/python-return-statement/
2022-06-01 11:50:32 -05:00
[side effects]: https://python.plainenglish.io/side-effects-in-python-a50caad1169