Resources
Lesson Objectives
Why do we need to reuse code?
What is a Function and how to create it?
Create Function with Arguments, Parameters and Returning Value.
Are you Ready?
Summary
The next key concept in Python
It's time to talk about Functions in python.
Functions allow you reuse code by wrapping it in a container that can be easily reused. This is an absolute must for anyone looking to write clean and efficient code.
If you start copy-pasting your code, then you are doing something wrong…
Programming is all about efficiency and that means reusing your code whenever possible. So, if you catch yourself copying code, take a step back and think how can you make it into a reusable function.
Take your piece of code
Wrap it in a function
Give it a Name
And then call this name to reuse
Sometimes you might have minor differences you would want to change. And that's where arguments come to rescue.
Arguments let you customize a function's behavior, acting as dynamic inputs for each use.
When you create functions, you can specify arguments that can be provided when you use that function.
This way you will dynamically provide different data to execute with your reusable code.
Think of Python's built-in print()
function.
It doesn't always print the same message, does it? Instead, you specify an argument when you use it. And you can achieve the same with you custom functions too.
Let's look into the syntax so you understand how it actually works.
Basic Syntax: Functions
Let's start with the basics.
Here is how to define a simple function and call it to trigger the code inside of it:
You create a function by using
def
keyword (stands for 'define')Provide a name of your function
Add Parenthesis for Arguments (can be empty)
And your code is written as a code block (with identation)
And that's a simple function.
Calling a Function
When you define a function, it's just a template.
You have to "call" the function to execute it. For example:
This will print "Hello, BIM World!" three times.
Adding Arguments to a Function
If you would want to add arguments to your function, you can specify a name of your arguments inside parenthesis. This will act as a variable that can be reused inside your function.
For Example:
Now we can use the same function, but provide different names.
Using Default Arguments
You also have an option to specify a default value for your arguments.
This way you can also execute your function without providing any arguments at all.
Parameters vs. Arguments
Also, you will hear a lot that people use Parameters and Arguments very similar. Let me break down the difference
Parameters: Variables defined in the function definition in parenthesis, which is used inside the function itself.
Arguments: Actual values passed when calling the function.
Overall they refer to the same thing, but in different context.
Returning Values from a Function
The next thing you need to know is that you can return values from your functions.
Let's say you want to provide 2 numbers and add them together. You also want to display the equation in the console and get the results.
Here is how you would do that with the function:
This way you can call your function and assign the result to another variable.
Functions Example
Alright let's summarize what we've learnt.
Let's create a function to read data from a dictionary of rooms or areas, calculate the square area, and then display results in a console.
I'm going to call this function spatial_calculator
and make it like this:
Now if you need to make any changes, you just do it in one place, and it will be applied everywhere you use it. That's the power of reusing your code.
HomeWork
It can still be a little confusing, but you need to practice creating functions.
So, here is your simple task:
Take a piece of code from previous lessons
Turn it into a function.
Try adding arguments or default values.
Experiment with returning values.
Call your function multiple times with different inputs.
Keep it simple.
Functions are a game-changer in programming. Practice them, and you’ll see how much easier and cleaner your code becomes.
⌨️ Happy Coding!