
- Why do we need to reuse code?|
- What is a Function and how to create it?
- Arguments vs Parameters
- Return Values
Functions in Python:
It's time to learn about Functions to understand how to write clean and efficient code.
Functions allow you to wrap a piece of code in a container, give it a name and reuse across your script in multiple places.

⚠️ If you start copy-pasting your code, then you are doing something wrong… 
Take a step back and think if you can create a reusable function instead.
🟨 Function Arguments
But Erik! What if I want to make little changes for each use?
Well, that's what arguments are for.
Arguments allow you to customize function's behavior, acting as dynamic inputs for each use.

Think of Python's built-in print()function for a moment. 
It doesn't always print the same message, right? Instead it prints different messages we provide. And we can also define custom arguments in our function to make changes on each use.
Let's actually dive into Function's syntax and you'll understand it all.
🔷 Basic Syntax: Functions
Let's start with the basic syntax.
To define a function we need to use a special keyword def  followed by function's name and parenthesis () . Then, place a colon :  to create a code-block that will be executed every time you use the function.
Now if you want to use it, we need to call it anywhere in your script by using function's name and parenthesis.
This will execute your function 3 times and make 3 print-statements.
The best part - if you make any changes in your function, it will be applied everywhere. That's how you start reusing your code.
💪Adding Parameters to a Function
Now, let's add parameters to a function so we can have a little different behavior. For that create variable names inside parenthesis of a function (e.g. name ). 
This variable can be used inside the scope of your function and the value will be provided whenever the function is called.
For example, let's create a function to greet someone by name:
Now we can use the same function, but provide different names.
⚠️This argument is mandatory, so you'll get an error if you forget to add it. Try it.
🪄Setting Default Value of Parameters
We also have an option to set a default value for parameters. It will be used if nothing is provided whenever function is called. Just assign default data to a parameter right in the parenthesis.
Now, providing argument is optional when you call the function
🆎 Parameters vs. Arguments
You might have noticed that I talk about Parameters and Arguments whiel referring to kind of the same thing. And many people get confused and think it's the same. But there is a difference.
Parameters: Variable names defined inside function definition in parenthesis, which is used inside the function itself. (e.g.
name)Arguments: Actual values passed when calling the function (e.g.
'Erik').
Overall, they refer to the same thing, but in the different context . One is inside the function definition, the other is when you use the function.
➡️ Returning Values from a Function
Now we can return result from a function and assign it to a variable.
And that's the basics of functions in python.

And guess what? Time to practice!
First of all, follow along the lesson and try the code to better understand function logic . Don't be afraid to experiment on your own too.
Then, I want to challenge you. Go to previous lessons (GuessingGame, MixingColors...) and try to create functions from existing code and use it. This way since the code already works - you'll focus specifically on the function's concept, and not the whole python code.
Sposored by LearnRevitAPI.com



