Python Basics — Day 15 Functions Basics (def, return, parameters)
Aspiring Full-Stack Developer | Python · Django · React · SQL | documenting my learning journey
Building skills in Python and full-stack development, with a focus on web apps and system design
Junior developer in training — Python, Django, React — preparing for a career in full-stack engineering
From Python basics to full-stack projects, sharing my progress as I grow into a developer
Future full-stack engineer | Learning in public: Python, APIs, Databases, and Web Development
01. What is a Function?
A block of code that performs a specific task
Defined with
def, and executed when called
def say_hello():
print("Hello!")
say_hello() # Call the function
02. Parameters
You can pass values into functions as parameters.
def greet(name):
print(f"Nice to meet you, {name}!")
greet("Sabin") # Nice to meet you, Sabin!
03. The return Statement
Functions can return values.
def add(a, b):
return a + b
result = add(3, 5)
print(result) # 8
04. Returning Multiple Values
In Python, a function can return multiple values (as a tuple).
def calc(a, b):
return a+b, a-b, a*b
s, d, m = calc(10, 5)
print(s, d, m) # 15 5 50
05. Default Parameters
def greet(name="Guest"):
print(f"Hello, {name}")
greet() # Hello, Guest
greet("Sabin") # Hello, Sabin
06. Functions with Loops
def square(n):
return n*n
for i in range(1, 6):
print(square(i))
07. Practice Examples
Example 1: Arithmetic Functions
def add(a, b):
return a+b
def sub(a, b):
return a-b
print("Add:", add(10, 5))
print("Subtract:", sub(10, 5))
Example 2: String Function
def shout(text):
return text.upper() + "!!!"
print(shout("python")) # PYTHON!!!