Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions concepts/functions/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ Functions are used to perform specific and repetitive tasks.

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`.
If a function does not specify a return value it will still return `None`.

Following is an example of a function with a side effect:

```python
>>> def hello():
... print("Hello")
... print("Hello")
...
>>> hello()
Hello
Expand All @@ -28,7 +28,7 @@ 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.
Since `hello()` does not specify a `return` value, it executes its side effect - which is calling `print()` -- and then returns `None`.
Since `hello()` does not specify a `return` value, it executes its side effect - which is calling `print()` - and then returns `None`.
Finally, we call the function by using its name and the parentheses - which signals to the Python interpreter that this is a _callable_ name.

Following is an example of a function with a return value:
Expand Down Expand Up @@ -61,7 +61,7 @@ Following is an example of a function which accepts an argument:
>>> def hello(name):
... return f"Hello, {name}"
...
>>>print(hello("Bob"))
>>> print(hello("Bob"))
Hello, Bob

```
Expand All @@ -81,6 +81,8 @@ Traceback (most recent call last):
print(hello())

TypeError: hello() missing 1 required positional argument: 'name'
```

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.

Expand All @@ -103,7 +105,8 @@ For more about function arguments, please see the [function arguments][function
[arguments]: https://www.w3schools.com/python/gloss_python_function_arguments.asp
[calls]: https://docs.python.org/3/reference/expressions.html#calls
[def]: https://www.geeksforgeeks.org/python-def-keyword/
[function arguments]: ../function-arguments/about.md
[default arguments]: https://www.geeksforgeeks.org/default-arguments-in-python/
[function arguments]: https://exercism.org/tracks/python/concepts/function-arguments
[function]: https://docs.python.org/3/glossary.html#term-function
[parameters]: https://www.codecademy.com/learn/flask-introduction-to-python/modules/learn-python3-functions/cheatsheet
[return]: https://www.geeksforgeeks.org/python-return-statement/
Expand Down
Loading