Function Parameters
Think of a function like a juice blender. The blender is the function, but the parameters are the fruits you put inside. If you put in strawberries, you get strawberry juice. If you put in mangoes, you get mango juice! Parameters let your function work with different data every time you use it.
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.
How it Looks (Syntax & Examples)
The Pattern:
def function_name(parameter):
# The code uses the parameter here
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
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.