Python Functions


function is a block of code that runs only when it is called.
It can take inputs (parameters) and return an output (return value).


🔹 Create a Function

def greet():
    print("Hello!")

Call the function:

greet()

🔹 Function with Parameters

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

greet("John")

🔹 Default Parameter Value

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

greet()        # Hello Guest
greet("Anil")  # Hello Anil

🔹 Return Values

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

print(add(2, 3))  # 5

🔹 Arbitrary Arguments (*args)

def show_names(*names):
    for name in names:
        print(name)

show_names("A", "B", "C")

🔹 Keyword Arguments

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

student(age=18, name="Priya")

🔹 Arbitrary Keyword Arguments (**kwargs)

def details(**info):
    print(info["name"])

details(name="Amit", age=25)

🔹 Pass Statement

Use pass to avoid empty function errors.

def test():
    pass

🔹 Recursion Example

def factorial(n):
    if n == 1:
        return 1
    return n * factorial(n - 1)

print(factorial(5))  # 120

Practice Questions

Q1. Write a Python program to create a function that prints "Welcome to Python".

Q2. Write a Python program to create a function that takes a name as input and prints a greeting like "Hello, CodePractice!".

Q3. Write a Python program to use a default parameter in a function, such as greet(name="Guest").

Q4. Write a Python program to create a function that returns the square of a number.

Q5. Write a Python program to call a function using keyword arguments (e.g., info(name="Sanju", age=27)).

Q6. Write a Python program to use *args to accept multiple values and print their total.

Q7. Write a Python program to use **kwargs to pass and print key-value pairs inside a function.

Q8. Write a Python program to create a function that returns the sum of two numbers.

Q9. Write a Python program to create a function to multiply 3 numbers using default arguments (e.g., multiply(a, b=1, c=1)).

Q10. Write a Python program to write a recursive function to calculate the factorial of a number.



Python Functions Quiz

Q1: What keyword is used to define a function?

A. function
B. define
C. def
D. func

Q2: What does a function return if there’s no return statement?

A. 0
B. Empty string
C. None
D. Error

Q3: What are args used for?

A. Return value
B. Error handling
C. Variable number of arguments
D. Keyword arguments

Q4: How do you call a function named greet?

A. call.greet()
B. greet.call()
C. greet[]
D. greet()

Q5: What does kwargs mean?

A. List of arguments
B. Tuple of arguments
C. Dictionary of keyword arguments
D. Function name

Q6: Which function call is correct?

A. greet = ()
B. greet{}
C. greet()
D. call greet

Q7: What is the purpose of a return statement?

A. Exit function
B. Print value
C. Return value to caller
D. Raise error

Q8: What is recursion in Python?

A. Looping in a function
B. Function calling itself
C. Printing result
D. None of these

Q9: What happens if a function is defined but never called?

A. It runs automatically
B. Gives error
C. Nothing happens
D. Returns zero

Q10: Can a function return multiple values?

A. No
B. Only strings
C. Yes, using tuple
D. Yes, using print

Go Back Top