Unit 4: Programming in Python – Class 10 SEE Computer Science Notes
Importantedunotes.com
Back to Computer Notes

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.

Built-in Functions: Python’s standard library comes pre-loaded with a massive number of functions ready for you to use. You are already familiar with many of them, such as 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.
User-defined Functions: In addition to the built-in tools, Python allows you to create your very own functions to perform highly specific tasks for your unique program. By defining your own functions, you can package complex logic into a single, clean command that you can reuse anywhere in your code.

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:

The items enclosed in the parentheses are called parameters, and they are completely optional. A function may or may not need parameters to do its job.
The function header must always end with a colon :.
Your function’s name should be unique and follow the standard rules for naming Python identifiers.
Finally, all code belonging to the function must be indented. Any statements placed outside of this indentation are not considered part of the function.

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.

Local Scope: Variables created inside a function are local to that specific function. They only exist while the function is executing. Once the function finishes its job, those local variables are wiped from memory. You cannot access a local variable from outside its parent function.
Global Scope: If a variable is declared at the top level of your script—outside of any functions—it has a global scope. This means it can be seen, accessed, and read from absolutely anywhere in your entire program, including from inside your user-defined functions.

4.10 Parameters vs. Arguments

While they sound similar, there is a distinct difference between parameters and arguments in Python.

Parameters are the placeholder variables written inside the parentheses when you define the function header. They simply tell the function what kind of data it should expect to receive.
Arguments are the actual, concrete data values that you pass into the function when you call it in your program. An argument can be a constant number, a variable, or even a mathematical expression. For example, if you call 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:

1. Positional (Required) Arguments: The arguments must be passed in the exact physical order as the parameters were defined. If the function expects three values, you must provide exactly three values in the correct sequence. For example, calling check(2, 5, 7) passes 2 to the first parameter, 5 to the second, and 7 to the third.
2. Default Arguments: You can assign a default fallback value to a parameter directly in the function header. If the user calls the function without providing that specific argument, Python will automatically use the default value. This is highly useful for values that rarely change, like setting an interest rate to 0.10. Note that default parameters must always be placed after required parameters in the header.
def interest(prin, time, rate=0.10):
    return prin * time * rate
3. Keyword (Named) Arguments: You can completely bypass the strict positional order by explicitly naming the parameters when you call the function. This gives you the flexibility to pass values in any order you like. For example, calling 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.

1. Functions Returning a Value (Non-Void Functions): These functions perform calculations and explicitly hand a computed result back to the caller using the 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.
2. Functions Not Returning a Value (Void Functions): These functions perform an action—like printing a greeting to the screen or saving a file—but do not return any computed data back to the program. They either lack a return statement entirely or use an empty return keyword. Behind the scenes, Python quietly returns a special empty value called 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.

1. Which keyword is used to define a user-defined function in Python?
a) function
b) define
c) def
d) func
Correct Answer: c) def
Justification: In Python, the reserved keyword def (short for define) is strictly used to declare the start of a user-defined function header.
2. What are the values passed to a function when it is called known as?
a) Parameters
b) Arguments
c) Return values
d) Scope
Correct Answer: b) Arguments
Justification: Parameters are the placeholders in the definition, while arguments are the actual, concrete values passed into the function when it is executed.
3. The part of the program where a function can be accessed is called its:
a) Parameter
b) Argument
c) Return type
d) Scope
Correct Answer: d) Scope
Justification: “Scope” dictates the specific region or context in a program where a variable, function, or object is visible and accessible to the computer.
4. Variables declared inside a function have:
a) Global scope
b) Local scope
c) Unlimited scope
d) No scope
Correct Answer: b) Local scope
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.
5. What does the return statement do in a Python function?
a) Prints output to the console.
b) Takes input from the user.
c) Sends a value back to the caller of the function.
d) Defines the function.
Correct Answer: c) Sends a value back to the caller of the function.
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.
6. What are the values specified in the function header within the parentheses called?
a) Arguments
b) Parameters
c) Return values
d) Local variables
Correct Answer: b) Parameters
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.
7. What type of argument allows you to call a function by specifying the parameter names?
a) Positional arguments
b) Default arguments
c) Keyword arguments
d) Required arguments
Correct Answer: c) Keyword arguments
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.
8. A function that performs an action but does not explicitly return a value is sometimes referred to as a:
a) Non-void function
b) Void function
c) Recursive function
d) Anonymous function
Correct Answer: b) Void 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.
9. Can a Python function return multiple values?
a) No
b) Yes, as a list
c) Yes, as a tuple
d) Yes, directly separated by commas
Correct Answer: d) Yes, directly separated by commas
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.

a) What is the main benefit of using user-defined functions in programming? 2 Marks
The main benefits are modularity and code reusability. Functions allow you to break complex problems down into smaller, manageable blocks of code that can be written once and reused multiple times throughout your program without having to rewrite the logic.

b) Name the two types of Python functions. 2 Marks
The two types are Built-in functions (pre-loaded functions provided by Python’s standard library, like print()) and User-defined functions (custom functions created by the programmer using the def keyword).

c) Explain the difference between a parameter and an argument in the context of Python functions. 2 Marks
A parameter is the placeholder variable defined in the function header that waits for data. An argument is the actual, concrete data value that is passed into the function when it is invoked or called by the program.

d) What is local scope in Python functions? Provide a brief example. 2 Marks
Local scope refers to variables created inside a function. They only exist while the function is actively running and cannot be accessed from outside. For example, if you declare x = 10 inside a function, trying to print(x) outside of that function will cause an error because x is local to the function.

e) What is global scope in Python? How does it differ from local scope? 2 Marks
Global scope refers to variables declared at the top level of a script, entirely outside of any functions. Unlike local variables which are restricted to their specific function blocks, global variables remain in memory for the duration of the program and can be accessed from absolutely anywhere in the code.

f) What is the purpose of the return keyword in a Python function? 2 Marks
The return keyword is used to immediately exit a function and hand a specific computed value or result back to the line of code that originally called the function, allowing the program to save or use that result.

g) Explain what positional arguments are and how they are passed to a function. 2 Marks
Positional arguments are passed to a function strictly based on their physical order. The first argument passed corresponds to the first parameter, the second to the second, and so on.

h) Describe the use case for default arguments in Python functions. 2 Marks
Default arguments are highly useful when a parameter almost always takes the same value (like setting a default tax rate). It allows the user to safely skip passing that argument during the function call, making the code cleaner while still providing flexibility to change it if needed.

i) What are keyword arguments, and what advantage do they offer when calling a function? 2 Marks
Keyword arguments involve explicitly naming the target parameter when passing data (e.g., 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.

j) What happens if a void function has a return statement without any value? 2 Marks
If a void function uses an empty return keyword, it simply halts execution and exits the function immediately. Python will quietly return a special empty data type called None to the caller.

Exercise 3: Write long answers to these questions.

a) Explain the concept of function scope in Python, differentiating between local and global scope. 4 Marks

Scope determines the visibility and lifespan of a variable in Python.

Local Scope: Any variable created inside a user-defined function is considered “local.” It is born when the function starts and is completely wiped from memory the moment the function finishes. It is impossible to read or alter a local variable from outside its parent function. This keeps functions isolated and prevents them from accidentally interfering with other parts of your program.
Global Scope: Any variable declared in the main body of your script, outside of all functions, is “global.” Global variables remain alive in memory for the entire duration the program is running and can be freely read by any function in your code.

b) Describe the concept of return values in Python functions. Differentiate between functions that return a value (non-void) and those that do not (void). Provide examples of both types and explain how the return value can be used in the calling part of the program. 4 Marks

Return values are the mechanism a function uses to send computed data back to the main program.

Non-Void Functions: These perform a mathematical task and explicitly hand back data using the 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)
Void Functions: These perform a background action (like saving a file or printing a message to the screen) but do not give any data back to the main program. They have no return statement (or just an empty return).
def say_hello():
    print("Welcome to the program!")
# The function executes the print command but gives nothing back to save.
say_hello()

c) Python allows functions to return multiple values. Explain how this is achieved and provide an example demonstrating a function that returns multiple values and how these values can be accessed by the caller. 4 Marks

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)

d) Design a Python program that includes at least two user-defined functions: One function that takes two numbers as arguments and returns their product. Another function that takes a list of numbers as an argument and prints each number. 4 Marks
# 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

Scroll to Top