Function#
Functions: reusable blocks of code that perform specific tasks.
Define a function#
Python function syntax:
def function_name():
# Function body
Tip
(1 more time) Naming convention - PEP8
Function names should be lowercase, with words separated by underscores as necessary to improve readability.
Variable names follow the same convention as function names.
Example
1def func_simple():
2 print("Wake up")
3 print("Learn Python")
4 print("Code")
5 print("Python is fast")
6 print("Don't understand Python")
7 print("Eat")
8 print("Sleep")
Call a function#
To call a function, use the function name followed by parentheses e.g. function_name().
Example:
1 print("Day 1")
2 func_simple()
3 print("Day 2")
4 func_simple()
5 print("...")
Output:
Day 1
Wake up
Learn Python
Code
Python is fast
Don't understand Python
Eat
Sleep
Day 2
Wake up
Learn Python
Code
Python is fast
Don't understand Python
Eat
Sleep
...
Parameters#
Parameters enhance the reusability of a function in computation, modularizing code, etc.
1def add_four_nr(a: int, b: int, c: int, d: int):
2 result: int = a + b + c + d
3 print(result)
Do things a bit more complex
1def complex_four_nr(a: int, b: int, c: int, d: int):
2 result: float = (a * b) - (c / (d ** 2))
3 print(result)
Return#
A function can also return a result that can be stored or used elsewhere in your program, instead of just printing the result out to the console
1def complex_four(a: int, b: int, c: int, d: int) -> float:
2 result: float = (a * b) - (c / (d ** 2))
3 return result
Now the result of complex_four could be stored in a variable, used in other computation.
1 complex_four_result: float = complex_four(67, 23, 44, 20)
2 print("Result / 3.14 = ", complex_four_result / 3.14)
Scope#
Python follows the LEGB rules for scope. Today let’s focus on L and G rule, which is super close to C rules for scoping.
In short
L - Local: Accessible in a code block, function
G - Global: Accessible in the program, script, and module
Docstring#
A docstring (short for documentation string) is a special kind of string used to describe what a function,
class, or module does. It’s placed right after the def or class line and is enclosed in triple
quotes (“”” or ‘’’).
Python reads this string at runtime, which means tools like IDEs, linters, and documentation generators (like Sphinx) can access it to help users understand your code more easily.
1def complex_four_wd(a: int, b: int, c: int, d: int) -> float:
2 """
3 Perform a composite arithmetic operation on four integers.
4
5 This function calculates the result of the expression:
6 (a * b) - (c / (d ** 2))
7
8 :param a: The first integer operand, used in multiplication.
9 :type a: int
10 :param b: The second integer operand, used in multiplication.
11 :type b: int
12 :param c: The third integer operand, used as the dividend in division.
13 :type c: int
14 :param d: The fourth integer operand, squared and used as the divisor.
15 :type d: int
16
17 :return: The result of (a * b) - (c / (d ** 2))
18 :rtype: float
19 """
20 result: float = (a * b) - (c / (d ** 2))
21 return result
Docstrings can also be leveraged to automatically generate documentation using tools like Sphinx, MkDocs, and others.
By including the following directive in your .rst file (restructuredtext file extension), Sphinx will
automatically locate the function and generate nicely formatted documentation based on its docstring:
.. autofunction:: function.four.complex_four_wd
The resulting documentation might look like this:
- function.four.complex_four_wd(a: int, b: int, c: int, d: int) float#
Perform a composite arithmetic operation on four integers.
This function calculates the result of the expression: (a * b) - (c / (d ** 2))
- Parameters:
a (int) – The first integer operand, used in multiplication.
b (int) – The second integer operand, used in multiplication.
c (int) – The third integer operand, used as the dividend in division.
d (int) – The fourth integer operand, squared and used as the divisor.
- Returns:
The result of (a * b) - (c / (d ** 2))
- Return type:
float
Note
Being clear about the types can make teamwork and long-term development much easier. This could be achieved by:
Type hint w/ IDE support
Informative comments & documentation