Python Coding Day 14 | Python Functions: Inputs, Parameters & Arguments
Python Functions: Inputs, Parameters & Arguments
Watch the lesson tutorial 🔻
In this article, we are going to break down how to make your Python functions more dynamic. We will move from basic functions to passing specific data using parameters and arguments.
1. Functions with Inputs (Basic Structure)
A function is a block of code that only runs when it is called. In its simplest form, you define a function to perform a specific set of actions every time you use it.
Here, we define a function called greet. Every time we call this function, it executes the three print statements inside it.
Practical Example
def greet():
print("Hello")
print("Good Morning")
print("Sri Lanka")
# Calling the function
greet()
Explanation:
def greet():: This defines the function. The code block following it (indented) belongs to this function.greet(): This is the "function call." Without this line, the code inside the function will never run.
Output:
Hello
Good Morning
Sri Lanka
2. Parameters & Arguments
To make functions useful, we often need to pass data into them. This allows the function to do the same task but with different information.
This is where people often get confused between Parameters and Arguments.
Parameter: The variable listed inside the parentheses in the function definition (e.g.,
name).Argument: The actual value you send to the function when you call it (e.g.,
"ChiruCodes").
Practical Example
def test(name):
# 'name' is the Parameter
print(f"Hello {name}")
print(f"Good Morning {name}")
print(f"Sri Lanka {name}")
# Calling the function with an Argument
test("ChiruCodes")
Explanation:
In this example, we defined the parameter name. When we called test("ChiruCodes"), the string "ChiruCodes" was passed into the function and replaced the name variable inside the print statements.
Output:
Hello ChiruCodes
Good Morning ChiruCodes
Sri Lanka ChiruCodes
3. Positional Arguments
When a function has multiple parameters, Python needs to know which value goes to which parameter. By default, Python uses Positional Arguments.
This means the order matters. The first argument matches the first parameter, the second argument matches the second parameter, and so on.
Practical Example
def new(name, age):
print(f"My name is {name}")
print(f"My age is {age}")
# Calling the function
# "Chirana" is assigned to 'name' (1st position)
# 20 is assigned to 'age' (2nd position)
new("Chirana", 20)
Explanation:
We defined two parameters:
nameandage.When calling
new("Chirana", 20), Python assigns"Chirana"tonamebecause it came first.It assigns
20toagebecause it came second.Note: If you swap them (
new(20, "Chirana")), the code will print "My name is 20", which is logically incorrect!
Output:
My name is Chirana
My age is 20
- by Chirana Nimnaka

Comments
Post a Comment