Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

Python Functions (Complete Guide for Beginners)

 In Python, functions are one of the most important building blocks of programming. A function is a reusable block of code that performs a specific task.

Instead of writing the same code again and again, you can put it inside a function and call it whenever needed.


🔹 What is a Function in Python?

A function is a group of statements that:

  • Performs a specific task
  • Can be reused multiple times
  • Makes code clean and organized

👉 Simple definition:

A function is a reusable block of code designed to perform a task.


🔹 Types of Functions in Python

Python has two main types:

1. Built-in Functions

These are already available in Python:

  • print()
  • len()
  • type()
  • range()

2. User-defined Functions

These are created by programmers using def.


🔹 Syntax of a Function

def function_name(parameters):
# code block
return value

🔹 Creating a Simple Function

def greet():
print("Hello, welcome to Python!")

greet()

🔸 Output:

Hello, welcome to Python!

🔍 Explanation:

  • def → keyword to define function
  • greet() → function name
  • Function is called using greet()

🔹 Function with Parameters

def add(a, b):
print("Sum:", a + b)

add(5, 3)

🔸 Output:

Sum: 8

🔍 Explanation:

  • a and b are parameters
  • Values passed during function call are arguments

🔹 Function with Return Value

def multiply(a, b):
return a * b

result = multiply(4, 6)
print(result)

🔸 Output:

24

🔍 Explanation:

  • return sends result back to caller
  • Value stored in result

🔹 Function with Default Parameter

def greet(name="Guest"):
print("Hello", name)

greet()
greet("Coco")

🔸 Output:

Hello Guest
Hello Coco

🔹 Keyword Arguments

def student(name, age):
print(name, age)

student(age=20, name="Alex")

🔸 Output:

Alex 20

🔹 Variable-Length Arguments (*args)

def total(*numbers):
print(sum(numbers))

total(10, 20, 30)

🔸 Output:

60

🔍 Explanation:

  • *numbers allows multiple values

🔹 Keyword Variable-Length Arguments (**kwargs)

def info(**data):
print(data)

info(name="John", age=25, city="Phnom Penh")

🔸 Output:

{'name': 'John', 'age': 25, 'city': 'Phnom Penh'}

🔹 Real-Life Example of Function

👉 Example: ATM System

def withdraw(balance, amount):
if amount <= balance:
return balance - amount
else:
return "Insufficient balance"

print(withdraw(1000, 300))

🔸 Output:

700

🔹 Why Use Functions?

✔ Reusability
✔ Code organization
✔ Easier debugging
✔ Less repetition
✔ Cleaner structure


🔹 Function vs Without Function

❌ Without function:

print(2 + 3)
print(5 + 7)
print(10 + 20)

✔ With function:

def add(a, b):
print(a + b)

add(2, 3)
add(5, 7)
add(10, 20)

🔹 Scope of Variables in Functions

Local Variable

Inside function only:

def test():
x = 10
print(x)

test()

Global Variable

Outside function:

x = 20

def show():
print(x)

show()

🔹 Key Points to Remember

  • Function is defined using def
  • Can take parameters
  • Can return values using return
  • Helps reuse code
  • Makes programs cleaner and structured

🚀 Conclusion

Python functions are essential for writing efficient and organized code. Once you understand functions, you can build:

  • Real applications
  • APIs
  • Games
  • Automation tools

Functions are the foundation of professional Python programming.




Post a Comment

0 Comments