Function Parameters
A Parameter is a variable that receives data from a function call and allows the function to work with different inputs.
Functions become much more powerful when they can accept data from outside. This is done using:
Parameters help functions become:
- Flexible
- Reusable
- Dynamic
- Efficient
What are Parameters?
Parameters are values that you pass into a function so it can use them to do a job.
-
Inside the Parentheses: They are always written inside the ( ) after the function name.
-
Dynamic Variables: They act like variables that only exist inside that specific function.
-
Flexible: Instead of writing ten different functions to add different numbers, you write one function with parameters!
Real-World Parameter Scenarios
-
Calculator: When you type
5 + 10, the numbers 5 and 10 are parameters sent to the "Add" function. -
Online Forms: When you type your name into a website, your name is a parameter sent to the "Save User" function.
-
Banking App: When you check your balance, your Account Number is the parameter used to find your money.
-
Video Games: When you pick up a "Power-Up," the amount of extra strength you get is passed as a parameter to your character.
Syntax
def function_name(parameter):
statements
Why Function Parameters are Important
Without parameters, a function always works with fixed values.
With parameters, the same function can work with different values.
Example 1: A Personalized Greeting
def greet(name):
print("Hello", name)
# Passing "Ram" as the input
greet("Ram")
Output: Hello Ram
Example 2: Adding Two Numbers
def add(a, b):
print(a + b)
# Passing 10 and 20 as parameters
add(10, 20)
Output: 30
Calling Function Multiple Times
One function can work with many values.
def greet(name):
print("Welcome", name)
greet("Sudhakar")
greet("Dhoni")
greet("Sachin")
Output
Welcome Sudhakar
Welcome Dhoni
Welcome Sachin
Parameters vs Arguments
Many beginners get confused between Parameters and Arguments.
| Parameters | Arguments |
|---|---|
| Variables inside function definition | Values passed during function call |
| Receive data | Send data |
| Created during function definition | Provided during function call |
Flow of Parameters and Arguments
Argument
↓
Function Call
↓
Parameter Receives Value
↓
Function Executes
↓
Output
Default Parameters in Python
Sometimes we provide default values for parameters.
If the user does not pass a value, Python uses the default value.
Syntax
def function_name(parameter="default"):
statements
Advantages of Function Parameters
- Make functions reusable
- Accept different values
- Reduce code duplication
- Improve flexibility
- Support dynamic programming
Common Beginner Mistakes
| Mistake | Problem |
|---|---|
| Missing arguments | TypeError |
| Wrong number of arguments | Runtime error |
| Confusing parameter and argument | Logic confusion |
| Incorrect order of arguments | Wrong output |
Summary:
-
Multi-Tasking: A single function can take multiple parameters at once (just separate them with a comma!).
-
Logic Power: Parameters make your code logical and stop you from writing the same code over and over.
-
Efficiency: They allow your program to handle thousands of different inputs using just one small block of code.