Skip to main content

Command Palette

Search for a command to run...

Python Basics — Day 15 Functions Basics (def, return, parameters)

Updated
1 min read
S

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!!!

Python Basics

Part 15 of 21

A collection of study notes from my university and self-learning journey. This series covers Python fundamentals step by step — from setup to core concepts — to help both myself and others build a solid foundation in coding.

Up next

Python Basics — Day 16 Advanced Functions in Python

01. Default Parameters You can assign default values to parameters. If no argument is passed, the default value is used. def greet(name="Guest"): print(f"Hello, {name}!") greet() # Hello, Guest! greet("Sabin") # Hello, Sabin! 02...

More from this blog

S

Sabin’s DevLog

21 posts

I plan to upload everything I study and prepare here. I hope it can be helpful to others, and I also want my future self to see the learning journey I’ve been through.