Using functions
In previous installments of the course notes, we’ve seen the print
and input
functions.
Functions make up the main building blocks of our programs—they are re-usable pieces of code that are nicely wrapped up for us. For instance, the implementation of the print
function may have 50 lines of code, it may only have 2. We don’t know and we don’t care. To use it, we just have to write print("my fantastic message")
. When we use a function, we say that we “call” that function.
We can give the general format for using functions in two ways:
# functions with return values
return_value = function_name(parameters)
# functions without return values
function_name(parameters)
Take a look at some examples using the input()
and print()
functions that we’ve already visited. Remember that the input()
function does have a return value and the print()
function does not.
# an example with a return value
result = input("here's a prompt? ")
# an example without a return value
print("here's a sentence")
If we forget the parentheses, the function will not run. What will happen in this case depends on if it is a built-in function like input()
and print()
or a function that you have written yourself.
Parameters/Arguments
You’ll notice that we use the words “parameters” and “arguments” pretty much interchangeably to refer to the same thing. This is ok!
Parameters: the values that a function takes and uses to perform its task. (further refined later in this chapter)
For instance, print("my message")
will print out the text my message
.
Some functions have a fixed number of parameters that they take. For example, the len()
function only takes 1 parameter.
Some functions can take a variable number of parameters. For example, we can write print()
, print("hello")
, and print("hello", "there")
. When you use a function with a different number of parameters, it’s behavior will be slightly different.
Notice that when we provide multiple parameters to a function, we use a comma (,
) to separate them.
Functions without return values
Our best example of a function without a return value is print()
.
We call functions without return values like:
function_name(parameters)
Out of curiousity, let’s investigate this lack-of-return-value more.
This shows us that if a function doesn’t have a return value and we try to save it anyways, we get a special python value called None
.
None
is a value that isn’t any of our 4 basic types (int
, float
, string
, boolean
). In fact, it is its own type. None
is a special value that literally represents nothing. We’ll get into this more later in the course.
Functions with return values
A great example of a function with a return value is input()
. len()
also has a return value. In fact, most functions that we will use in this course, and especially python’s built-in functions have return values.
Remember: if you do not save a return value in a variable, you cannot use it later!
When we call functions with return values, we use the format:
return_value = function_name(parameters)
String functions
Of course, it would be too simple if all functions worked exactly the same way.
Since strings are objects, when we call a string function, what we are doing is saying “hey, string! Give me the information that I want!”. What this means is that we need a string that has already been created (an instance of a string) to call string functions.
The general format for string functions is:
string_var = "any string value"
return_value = string_var.function_name(parameters)
Let’s look at an example: the string islower()
function.
my_string = "I am a string!"
result = my_string.islower()
print(result)
What this string is doing is saying “Hey, my_string
variable! I want you to tell me if you are lower case!”
Another example: the string lower()
function.
my_string = "I am a string!"
result = my_string.lower()
print(result)
What this string is doing is saying “Hey, my_string
variable! I want you to give me back the lower case version of yourself!”