Hereβs a simple explanation of functions in Python in easy international English, perfect for students and beginners:
π§ What is a Function?
- A function is a block of code that does a specific job.
- You can use it again and again.
- It helps make your program clean and easy.
π§ Why Use Functions?
- Avoid writing the same code again.
- Break big problems into small parts.
- Makes your program organized.
βοΈ How to Create a Function
β Syntax:
def function_name():
# your code here
β Example:
def say_hello():
print("Hello, students!")
βΆοΈ How to Call a Function
say_hello()
β Function with Parameters
- You can send values into a function using parameters.
def greet(name):
print("Hello", name)
Call it like:
greet("Alice")
greet("Ravi")
β Function with Return Value
- A function can give back (return) a value.
def add(a, b):
return a + b
result = add(5, 3)
print("Sum is:", result)
π§ Types of Functions in Python
Type | Example Code |
---|---|
No parameters, no return | def greet(): print("Hi") |
With parameters | def greet(name): print(name) |
Return something | def add(a, b): return a + b |
Built-in functions | print() , len() , type() |
π Activity β Create and Use a Function
def multiply(x, y):
result = x * y
return result
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
print("Multiplication is:", multiply(num1, num2))
β Summary Table
Term | Meaning |
---|---|
def | Keyword to define a function |
Parameter | Input to function (inside brackets) |
Return | Output from the function |
Call | Run the function using its name |
Here are more examples, practice questions, and answers to help students understand Functions in Python better
β More Function Examples
πΉ Example 1: Function with No Parameters, No Return
def welcome():
print("Welcome to Python class!")
welcome()
πΉ Example 2: Function with Parameters
def greet(name):
print("Hello", name)
greet("Asha")
greet("John")
πΉ Example 3: Function with Return
def square(n):
return n * n
result = square(4)
print("Square is:", result)
πΉ Example 4: Find Maximum of Two Numbers
def find_max(a, b):
if a > b:
return a
else:
return b
print("Max is:", find_max(10, 20))
πΉ Example 5: Even or Odd
def check_even_odd(n):
if n % 2 == 0:
return "Even"
else:
return "Odd"
print(check_even_odd(7)) # Output: Odd
πΉ Example 6: Factorial of a Number
def factorial(n):
result = 1
for i in range(1, n+1):
result *= i
return result
print("Factorial of 5 is:", factorial(5)) # 120
πΉ Example 7: Sum of a List
def sum_list(numbers):
total = 0
for n in numbers:
total += n
return total
print(sum_list([1, 2, 3, 4])) # Output: 10
π Practice Questions + Answers
β Q1: Write a function that multiplies two numbers and returns the result.
β Answer:
def multiply(a, b):
return a * b
print(multiply(3, 4)) # Output: 12
β Q2: Create a function that checks if a number is positive or negative.
β Answer:
def check_number(n):
if n >= 0:
return "Positive"
else:
return "Negative"
print(check_number(-5)) # Output: Negative
β Q3: Write a function that returns the length of a word.
β Answer:
def word_length(word):
return len(word)
print(word_length("Python")) # Output: 6
β Q4: Write a function that returns the reverse of a string.
β Answer:
def reverse_string(text):
return text[::-1]
print(reverse_string("hello")) # Output: olleh
β Q5: Write a function that returns the average of 3 numbers.
β Answer:
def average(a, b, c):
return (a + b + c) / 3
print(average(10, 20, 30)) # Output: 20.0
π Challenge Practice
β Q6: Create a function that takes a list of numbers and returns only even numbers.
β Answer:
def get_even_numbers(numbers):
evens = []
for n in numbers:
if n % 2 == 0:
evens.append(n)
return evens
print(get_even_numbers([1, 2, 3, 4, 5, 6])) # Output: [2, 4, 6]
β Q7: Write a function that counts vowels in a string.
β Answer:
def count_vowels(text):
vowels = 'aeiouAEIOU'
count = 0
for char in text:
if char in vowels:
count += 1
return count
print(count_vowels("Python is easy")) # Output: 4
β Summary
Type | Example |
---|---|
No parameters, no return | def greet(): print("Hi") |
With parameters | def add(a, b): print(a + b) |
Return value | def square(x): return x * x |
Real-world use | Checking even/odd, sum, max, etc. |