Functions in Python

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

TypeExample Code
No parameters, no returndef greet(): print("Hi")
With parametersdef greet(name): print(name)
Return somethingdef add(a, b): return a + b
Built-in functionsprint(), 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

TermMeaning
defKeyword to define a function
ParameterInput to function (inside brackets)
ReturnOutput from the function
CallRun 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

TypeExample
No parameters, no returndef greet(): print("Hi")
With parametersdef add(a, b): print(a + b)
Return valuedef square(x): return x * x
Real-world useChecking even/odd, sum, max, etc.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *