⬅ Previous Topic
Python FunctionsNext Topic ⮕
Python main() Function⬅ Previous Topic
Python FunctionsNext Topic ⮕
Python main() FunctionIn Python, functions allow you to group a block of code under a name and reuse it whenever needed. But functions become truly powerful when they can accept input in the form of parameters. These inputs allow functions to behave dynamically, depending on the data passed to them.
Without parameters, a function is just a repetitive block of code. With parameters, it becomes reusable, flexible, and adaptable. Think of parameters like ingredients in a recipe—you can create the same dish with different flavors, depending on what you add.
You define parameters inside the parentheses when writing a function. These are placeholders for values (called arguments) you'll pass when calling the function.
def greet(name):
print("Hello, " + name + "!")
Explanation: The function greet
takes one parameter called name
. When you call it, you must pass a value for name
.
greet("Alice")
greet("Bob")
Hello, Alice!
Hello, Bob!
Note: The values "Alice"
and "Bob"
are the actual arguments passed to the function. They replace the name
parameter temporarily when the function runs.
You can define more than one parameter by separating them with commas.
def add(a, b):
result = a + b
print("The sum is:", result)
add(10, 5)
add(-3, 7)
The sum is: 15
The sum is: 4
Check: Ensure that you pass two values when calling this function. If you forget or pass the wrong number of arguments, Python will throw a TypeError
.
name
, a
, b
)."Alice"
, 10
).Sometimes, you want to make a parameter optional. You can do this by assigning it a default value.
def greet(name="stranger"):
print("Hello, " + name + "!")
greet("Sam")
greet()
Hello, Sam!
Hello, stranger!
Verification: Default values are used only when no argument is provided. You can override them by passing your own value.
x
, y
unless in math contexts).print()
or return
to observe and verify behavior.In many real-world scenarios, you may want the function to send back a value instead of printing it directly.
def multiply(x, y):
return x * y
result = multiply(4, 5)
print("Result:", result)
Result: 20
Why return? Returning a value gives you flexibility. You can store it, use it in calculations, or pass it to another function.
Functions with parameters are the backbone of reusable code in Python. They let you pass data into functions, perform operations, and optionally return results. Remember to use meaningful parameter names and test your functions with different inputs to build strong coding habits early on.
In the upcoming tutorials, you'll learn about keyword arguments, variable-length arguments (*args
, **kwargs
), and how Python handles argument matching in function calls.
⬅ Previous Topic
Python FunctionsNext Topic ⮕
Python main() FunctionYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.