Files
python/concepts/functions/introduction.md

109 lines
4.5 KiB
Markdown
Raw Normal View History

# Introduction
A [`function`][function] is a way to compartmentalize code so it can be called by name from one or more places in the program.
Functions are used to perform specific and repetitive tasks.
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`.
2022-05-26 16:03:12 -05:00
Following is an example of a function with a side effect:
2022-05-26 14:24:12 -05:00
```python
>>> def hello():
... print("Hello")
...
>>> hello()
"Hello"
2021-10-02 19:42:59 +05:30
2022-05-26 14:24:12 -05:00
hello()
```
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-05-26 14:24:12 -05:00
Since the `hello` function does not specify a `return` value, it executes its side effect - which is calling the `print` function - and returns `None`.
Finally, we call the function by using its name and the parentheses.
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
def hello():
return "Hello"
print(hello())
```
The body of the function has been changed from calling `print` to returning `"Hello"`.
The program still prints `"Hello"`, but the side effect takes place in the calling code instead of in the `hello` function.
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>
```
It's important that, even if a function accepts no arguments, the empty parentheses must be used to call it correctly.
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
def hello(name):
return f"Hello, {name}"
print(hello("Bob"))
```
The parameter is defined as `name`.
`"Bob"` is the argument which the program passes to the `hello` function.
The program outputs `Hello, Bob`.
What if someone calls `hello` without passing an argument?
The program would error with a report that an argument is missing.
2022-05-26 16:03:12 -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].
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
Following is an example of a function with a default argument:
2022-05-26 14:24:12 -05:00
```python
>>> def hello(name="you"):
2022-05-26 16:03:12 -05:00
return f"Hello, {name}"
>>> print(hello())
Hello, you
2022-05-26 14:24:12 -05:00
2022-05-26 16:03:12 -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-02 19:42:59 +05:30
[built-in functions]: https://docs.python.org/3/library/functions.html
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/
[default arguments]: https://www.geeksforgeeks.org/default-arguments-in-python/
2021-10-02 19:42:59 +05:30
[first-class objects]: https://realpython.com/lessons/functions-first-class-objects-python/
[function]: https://docs.python.org/3/glossary.html#term-function
2022-05-26 14:24:12 -05:00
[function arguments]: ../function-arguments/about.md
2021-10-23 09:02:25 +05:30
[function-definitions]: https://docs.python.org/3/reference/compound_stmts.html#function-definitions
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/
[side effects]: https://runestone.academy/ns/books/published/fopp/Functions/SideEffects.html