Functions
Last updated
Last updated
One of the principles of writing clean code is DRY:
Don't Repeat Yourself (DRY)
But, inevitably we will repeat ourselves. Since up to now, code is linear. We run code and move onto the next section after it.
This is where functions come in.
Functions are little boxes you put something in and get a result out. They can be used multiple times, preventing us from repeating ourselves if used correctly.
We input something into the box like 2, the box does something with our input and it outputs something like 4.
In this case, the box squares the input number.
If we wanted to square a bunch of numbers, we would have to write something like:
But what if, eventually, we wanted to square more numbers? Or if we wanted to square a users input? We cannot possibly cover all cases. So let's put it into a function.
Now we can square any number we want with this function.
The function is the black box.
Every function in Python is defined by the keyword def
(which means, literally, define).
After the def
keyword we name the function. Let's call it Skidy.
Next up we add the function arguments. Arguments are variables we input into the function.
Remember our magical box? We had inputs into it. The function's arguments are the inputs.
Let's say the function skidy
returns "Hello, DorkStar" if the name is "dork" else it will return "Goodbye, {name}".
We need one of the inputs to be a name.
And then finally, as we saw with if statements we need to include a : on the end of the definition. This means that our next line will be tabbed by 4 spaces.
Now we check to see if the name is "dork" and return "Hello, Dorkstar".
Remember the magic box from earlier? It output something. The return
statement is the output.
Now, we want to return "Hello, {name}" if their name isn't Dork:
We now have our function! But you may have noticed something unusual. What's that f"Goodbye, {name}"
?
This is called an f-string
in Python. It stands for "Formatted String".
Strings are kinda boring. They don't really convey information. If we rewrote the code:
What if Cmnatic does not run our program? What if it's someone else?
We need a way to take in a variable, and change what we print based on the variable.
We do this with formatted strings.
To make an f-string, start the string with the letter f
.
And now to use a variable in the string, include it in some curly braces {
.
Let's go back to our code.
This code is messy. Do you know why? Hint: f-strings
.
Python has a poem called The Zen of Python
which details what makes Pythonic code.
This code is both nested, complex and isn't the most obvious way.
We can rewrite our code using one line if statements and f-strings like so:
To include quotation marks in strings, we have 2 options.
We use the other kind of quotation marks. So if we do print(" 'hi' ")
we would have to use '
. Same for the other way around.
We use triple quotation marks print("""Hello "this is a good example" """)
.
And now our code is clean!
Refactoring code is a very important part of programming. As the code grows, we'll have new functions and new ways to make the code clean.
How do you think we can call our new function? Hint: we have been using functions from the start. print()
is a function.