What are functions and how can we use them?
functiondefinition in Python Terminology.
Let's use a function that's built into Python: Python's sum function.
If we type sum and hit the Enter key, we'll see what the variable sum points to:
>>> numbers = [2, 1, 3, 4, 7, 11, 18]
>>> sum
<built-in function sum>
We're not actually using the function here, we're referring to the function object that the variable sum points to.
To use this function, we need to put parentheses after it. Putting parenthesis after a function will call the function.
>>> sum()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: sum() takes at least 1 positional argument (0 given)
When calling this function we got an error because the sum requires at least one argument but we didn't pass it any arguments.
To use the sum function we have to pass it an iterable of numbers as an argument.
To pass an argument to a function, you put the argument inside the parentheses when calling the function.
Arguments are basically the inputs to a function.
Functions have inputs (as arguments) and an output as a return value.
The integer 46 is the return value of this function:
>>> sum(numbers)
46
We saw 46 printed out at the Python REPL, but this sum function didn't actually print 46, it returned 46.
If we put this function call on the right-hand side of an = sign, we can assign the variable total to whatever the return value of calling sum with numbers was (in this case, it was 46).
>>> total = sum(numbers)
>>> total
46
NoneNot all functions have returned values.
For example, the print function doesn't have a return value.
If we assign a variable to the return value of a print call:
>>> name = print("Trey")
Trey
We'll see text (Trey) printed out, but we'll also see that the variable name is None:
>>> name
>>> print(name)
None
None is a special value that basically means this function doesn't have a return value.
In Python, we typically assume that functions either perform some action or return something, but not both.
The print function performs an action (it prints something to the screen) whereas the sum function returns a value.
To use a function, we "call" that function.
To call a function in Python, write the function name followed by parentheses. If the function accepts arguments, pass the arguments inside the parentheses as you call the function.
If that function has a return value, you can capture that return value into a variable or pass that return value straight into another function call.
Sign up for my free 5 day email course and learn essential concepts that introductory courses often overlook: iterables, callables, pointers, duck typing, and namespaces.
Python, like many programming languages, has functions. A function is a block of code you can call to run that code.
Python's functions have a lot of "wait I didn't know that" features. Functions can define default argument values, functions can be called with keyword arguments, and functions can be written to accept any number of arguments.
To track your progress on this Python Morsels topic trail, sign in or sign up.
Sign up for my 5 day email course and learn essential concepts that introductory courses often overlook!
Sign in to your Python Morsels account to track your progress.
Don't have an account yet? Sign up here.
Sign up for my free 5 day email course and learn essential concepts that introductory courses often overlook: iterables, callables, pointers, duck typing, and namespaces. Learn to avoid beginner pitfalls, in less than a week!
Ready to level up? Sign up now to begin your Python journey the right way!