You've heard the word function many times before, and you probably already identified some functions you've been using in this course. Here are some of them:
print()is used to display output to the console. It takes a Python object as input and writes its string representation to your console. Theprint()function returnsNone.type()is used to find out the data type of an object. The object is the argument you pass to it, and the data type is the function's return value.str(),int(),float(),list(), etc., are functions that allow you to explicitly convert a value from one data type to another. They take the value as their input and return the converted value.input()is used to collect user input through the CLI. The function takes a string as its input that will be used as a prompt for the user. It then collects the user input and returns it as a string.open()allows you to create file objects that you can read from and write to. It takes a file path and a mode as its input and returns a file object.
You didn't have to define any of these functions because they're already part of the Python language. But they aren't special beyond the fact that they come pre-installed, and you could write your own versions of any of these functions.
Fruitful Functions
When you think about what most of these functions have in common, you might notice that you've mostly used them on the right side of assignment statements:
nstr = "42"
n = int(nstr)
You can do that because you are interested in the return value of the function.
Instead of allowing the return value to float off into some digital abyss, you capture it and assign it to a variable, in this case, n.
Most functions that you'll work with, and most functions that you'll write, will return a value that you want to use somewhere else in your program. In this course, you'll read about functions that have a return value as fruitful functions.
Void Functions
Functions that don't return a value you defined always return None in Python. These functions are also called void functions.
You might wonder what the point of a void function is. But just because a function doesn't return anything doesn't mean that it can't still have an effect.
Void Function Example
A good example of a void function is the built-in print(). You've used it many times, and you saw that it always has the effect of displaying some output to your console.
However, print() is a void function and returns None. The output that is printed to your console is not its return value; it's what is sometimes called a side effect.
If you assign the output of a call to print() to a variable, as you did with the fruitful functions discussed above, and print out the value of your new variable, you'll get the proof:
result = print("hello void!")
print(result) # None
You'll see that the message gets printed to your console just as you're used to. When printing the value of the result variable, however, you'll see that the print() function returned None. It's a void function that does something in its function body but doesn't explicitly return a value.
Most of the time, you'll want to write fruitful functions, and there is even a programming paradigm called functional programming that aims to only use functions that have no side effects and always create the same output when given the same input.
You can write programs in a functional style in Python, but you don't have to. For now, you'll stick with getting stuff to work without worrying about programming paradigms.
In a future lesson, you'll take a second look at the different parts that make up a function definition before starting to write your own functions.
Function Output Practice
- Print out the return value of each of the functions you've gotten to know.
- What does their output tell you about them?
Summary: Familiar Python Function Definition
- You've already encountered and used a few built-in functions throughout this course, e.g.,
print()andtype(). - Functions can be fruitful if they return a specific value.
- Functions are void if they return the default
None.