🚧Summaries For Lessons 24+ Are Still Work In Progress!

“Whoever is patient has great understanding…" Proverbs 14:29

Function Basics in Python

Description here...

Function Basics in Python

Description here...

Function Basics in Python

Description here...

- 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.

#⚙️ Define a function
def say_hello():
    print("Hello Python World!")
#⚙️ Define a function
def say_hello():
    print("Hello Python World!")
#⚙️ Define a function
def say_hello():
    print("Hello Python World!")

Now if you want to use it, we need to call it anywhere in your script by using function's name and parenthesis.

say_hello()
say_hello()
say_hello()
say_hello()
say_hello()
say_hello()
say_hello()
say_hello()
say_hello()

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.

Sign-Up For Future Updates✨

Be among the first people to hear about
New Python Courses or Useful Resources!

Once there's enough demand I might start a Python Newsletter
with even more Tips and Tricks to help you learn it better!


Want To Donate? Click here.

Sign-Up For Future Updates✨

Be among the first people to hear about
New Python Courses or Useful Resources!

Once there's enough demand I might start a Python Newsletter
with even more Tips and Tricks to help you learn it better!


Want To Donate? Click here.

Sign-Up For Future Updates✨

Be among the first people to hear about
New Python Courses or Useful Resources!

Once there's enough demand I might start a Python Newsletter
with even more Tips and Tricks to help you learn it better!


Want To Donate? Click here.

💪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:

# Function with a parameter
def greet(name):
    print(f"Hello, {name}!")
# Function with a parameter
def greet(name):
    print(f"Hello, {name}!")
# Function with a parameter
def greet(name):
    print(f"Hello, {name}!")

Now we can use the same function, but provide different names.

# Call the function with different arguments
greet("Erik")
greet("Kristina")
greet("Ricky")
greet("Klaus")
# Call the function with different arguments
greet("Erik")
greet("Kristina")
greet("Ricky")
greet("Klaus")
# Call the function with different arguments
greet("Erik")
greet("Kristina")
greet("Ricky")
greet("Klaus")

⚠️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.

# Function with a default argument
def greet(name="Erik"):
    print("Hello, {}!".format(name))
# Function with a default argument
def greet(name="Erik"):
    print("Hello, {}!".format(name))
# Function with a default argument
def greet(name="Erik"):
    print("Hello, {}!".format(name))

Now, providing argument is optional when you call the function

# Call the function
greet()            # Outputs: Hello, Erik ! <- Default Value
greet("Kristina")  # Outputs: Hello, Kristina!
# Call the function
greet()            # Outputs: Hello, Erik ! <- Default Value
greet("Kristina")  # Outputs: Hello, Kristina!
# Call the function
greet()            # Outputs: Hello, Erik ! <- Default Value
greet("Kristina")  # Outputs: Hello, Kristina!

🆎 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

# Function that returns a value
def add_numbers(a, b):
    total = a + b
    print(f'{a} + {b} = {total}')
    return total

# Use the returned value
result = add_numbers(5, 10)  # This will print: '5 + 10 = 15'
print(result)                # Outputs: 15
# Function that returns a value
def add_numbers(a, b):
    total = a + b
    print(f'{a} + {b} = {total}')
    return total

# Use the returned value
result = add_numbers(5, 10)  # This will print: '5 + 10 = 15'
print(result)                # Outputs: 15
# Function that returns a value
def add_numbers(a, b):
    total = a + b
    print(f'{a} + {b} = {total}')
    return total

# Use the returned value
result = add_numbers(5, 10)  # This will print: '5 + 10 = 15'
print(result)                # Outputs: 15

Now we can return result from a function and assign it to a variable.

And that's the basics of functions in python.

Happy Coding!

🙋‍♂️ See you in the next lesson.
- EF

Happy Coding!

🙋‍♂️ See you in the next lesson.
- EF

Happy Coding!

🙋‍♂️ See you in the next lesson.
- EF

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.

⌨️ Happy Coding!

⌨️ Happy Coding!

⌨️ Happy Coding!

If you have any questions, leave them in the YouTube Comments.

If you have any questions, leave them in the YouTube Comments.

If you have any questions, leave them in the YouTube Comments.

Have a Question?

Have a Question?

Have a Question?

Sign-Up For Future Updates✨

Be among the first people to hear about
New Python Courses or Useful Resources!

Once there's enough demand I might start a Python Newsletter
with even more Tips and Tricks to help you learn it better!


Want To Donate? Click here.

Sign-Up For Future Updates✨

Be among the first people to hear about
New Python Courses or Useful Resources!

Once there's enough demand I might start a Python Newsletter
with even more Tips and Tricks to help you learn it better!


Want To Donate? Click here.

Sign-Up For Future Updates✨

Be among the first people to hear about
New Python Courses or Useful Resources!

Once there's enough demand I might start a Python Newsletter
with even more Tips and Tricks to help you learn it better!


Want To Donate? Click here.

PS. Python can change your career and how you think about problems.
Be Careful 🙂

PS. Python can change your career and how you think about problems.
Be Careful 🙂

PS. Python can change your career and how you think about problems.
Be Careful 🙂

Sposored by LearnRevitAPI.com