Simple Python Programs Using Functions
These are very simple Python programs to help beginners understand how functions, parameters,
and arguments work.
Example 1: Function with one parameter
def greet(name): # 'name' is a parameter
print("Hello", name)
greet("Afia") # "Afia" is an argument
Output: Hello Afia
Example 2: Function with two parameters
def add_numbers(a, b): # a and b are parameters
sum = a + b
print("Sum =", sum)
add_numbers(5, 3) # 5 and 3 are arguments
Output: Sum = 8
Example 3: Function returning a value
def square(num):
return num * num
result = square(4)
print("Square =", result)
Output: Square = 16
Example 4: Function with default parameter
def wish(name="Student"):
print("Good morning,", name)
wish() # uses default value
wish("Ash") # uses argument
Output: Good morning, Student Good morning, Ash
Example 5: Function with multiple arguments
def details(name, age, place):
print("Name:", name)
print("Age:", age)
print("Place:", place)
details("Meera", 20, "Kozhikode")
Output: Name: Meera Age: 20 Place: Kozhikode
Example 6: Function calling another function
def add(a, b):
return a + b
def square_of_sum(x, y):
total = add(x, y)
return total * total
result = square_of_sum(2, 3)
print("Square of sum =", result)
Output: Square of sum = 25
Quick Recap for Students
Function: A block of code that performs a task
Parameter: Variable in function definition
Argument: Actual value passed when calling
Return: Sends result back to caller