Programming in Python
Chapter Two: User-defined Functions
Comprehensive study guide exploring function definitions, parameters vs arguments, scopes, return values, and fully solved textbook exercises for SEE preparation.
Welcome to Chapter Two! As your Python programs grow larger, writing the same code over and over again becomes exhausting and messy. This is where functions come to the rescue.
In this guide, you will learn about user-defined functions, parameters, arguments, return values, scopes, and you will gain access to complete textbook exercise solutions for your Class 10 SEE preparation.
1. Comprehensive Theory of Python Functions
User-defined Functions
A Python function is an organized, reusable block of code designed to perform a single, specific action. Think of a function like a cooking recipe: once you write the recipe down, you don’t have to reinvent how to bake a cake every time—you simply pull out the recipe and follow it. Functions provide your application with better modularity and a high degree of code reusability.
4.7 Types of Python Functions
Python divides functions into two primary categories: built-in functions and user-defined functions.
print(), int(), len(), and sum(). These functions are always available to you right out of the box because they are loaded into the computer’s memory the moment you start the Python interpreter.4.8 Creating a User-Defined Function
Creating your own function is straightforward. A function definition always begins with the keyword def (which is short for define).
The Syntax:
def function_name(parameter1, parameter2):
# Set of instructions to be executed
return value
Important Rules to Remember:
:.Example of Creating and Calling a Function:
# Defining the function
def add_numbers(x, y):
sum_value = x + y
return sum_value
# Calling the function in our main program
num1 = 5
num2 = 6
print("The sum is", add_numbers(num1, num2))
4.9 Scope of Variables
The “scope” of a function or variable refers to the specific part of your program where it can be accessed, read, and called.
4.10 Parameters vs. Arguments
While they sound similar, there is a distinct difference between parameters and arguments in Python.
area(5), the number 5 is the argument.Passing Parameters in Python
When you call a function, Python allows you to pass arguments in three different ways:
check(2, 5, 7) passes 2 to the first parameter, 5 to the second, and 7 to the third.def interest(prin, time, rate=0.10):
return prin * time * rate
interest(time=2, rate=0.12, prin=2000) is perfectly valid because you specifically told Python where each value belongs.4.11 Returning Values from Functions
Functions in Python can be categorized based on whether or not they hand data back to the main program when they finish running.
return keyword. The returned value can be a literal number, a variable, or the result of an expression. The calling program can then save this returned value into a new variable.None for these functions.
def greet(name):
print("Hello", name)
return # Exits the function without handing back any data
Returning Multiple Values: Unlike many other programming languages, Python is clever enough to let you return multiple distinct values from a single function at the exact same time. You achieve this by simply separating the variables with commas in your return statement.
def squared(x, y, z):
return x*x, y*y, z*z
v1, v2, v3 = squared(2, 3, 4)
print("The returned values are:", v1, v2, v3) # Outputs: 4 9 16
Exercise 1: Choose the correct answer.
Select an option to view the correct answer and justification.
Justification: In Python, the reserved keyword def (short for define) is strictly used to declare the start of a user-defined function header.
Justification: Parameters are the placeholders in the definition, while arguments are the actual, concrete values passed into the function when it is executed.
Justification: “Scope” dictates the specific region or context in a program where a variable, function, or object is visible and accessible to the computer.
Justification: Any variable created directly inside a function block is strictly local to that function and is wiped from memory as soon as the function finishes executing.
Justification: The return keyword halts the function’s execution and explicitly passes the computed data back to the line of code that originally called the function.
Justification: The variables listed inside the parentheses during the function’s initial creation are known as parameters. They act as empty variables waiting for data.
Justification: Keyword (or named) arguments allow you to ignore the positional order by explicitly typing the parameter name followed by an equals sign when calling the function.
Justification: A void function executes its internal block of code (such as printing to the screen) but does not pass any calculated data back to the main program.
Justification: Python uniquely allows you to return multiple distinct values by simply separating them with commas in the return statement (e.g., return x, y, z).
Exercise 2: Write short answers to these questions.
print()) and User-defined functions (custom functions created by the programmer using the def keyword).
x = 10 inside a function, trying to print(x) outside of that function will cause an error because x is local to the function.
calculate(time=5, rate=2)). The major advantage is extreme flexibility; you do not have to memorize or follow the exact positional order defined in the function header.
None to the caller.
Exercise 3: Write long answers to these questions.
Scope determines the visibility and lifespan of a variable in Python.
Return values are the mechanism a function uses to send computed data back to the main program.
return keyword. The caller can save this data into a variable and use it later in the program.def add(a, b):
return a + b
# The function hands back the sum, which we save into the 'result' variable.
result = add(5, 5)
def say_hello():
print("Welcome to the program!")
# The function executes the print command but gives nothing back to save.
say_hello()
While many programming languages restrict functions to returning only a single variable, Python allows you to return multiple distinct values simultaneously by simply separating them with commas in the return statement. Behind the scenes, Python safely packs these values together into a single “Tuple” data structure. The caller can then “unpack” them directly into separate variables in a single line of code.
def calculate_both(x, y):
sum_val = x + y
prod_val = x * y
return sum_val, prod_val # Returning two values separated by a comma
# The caller unpacks the returned data directly into two new variables
addition, multiplication = calculate_both(4, 5)
print("Sum is:", addition)
print("Product is:", multiplication)
# Function 1: Takes two arguments, calculates the product, and returns the result
def multiply_numbers(num1, num2):
product = num1 * num2
return product
# Function 2: Takes a list of numbers as an argument and prints each one using a loop
def print_list_items(number_list):
print("Printing the items in the list:")
for number in number_list:
print(number)
# --- Main Program Execution ---
# Testing the first function (Returning a value)
result = multiply_numbers(5, 10)
print("The product of the two numbers is:", result)
print("-------------------")
# Testing the second function (Void function performing an action)
my_numbers = [15, 42, 7, 99, 100]
print_list_items(my_numbers)
📚 Also Read: Class 10 SEE Notes
Computer Science Units
Other Subjects
