Python #3 Recursive, Lambda Functions

CS25C02 · COMPUTER PROGRAMMING: PYTHON

Unit 3: Functions

Write reusable programs by hiding redundancy and complexity, passing arguments, returning results, and exploring recursion and lambda expressions.

09Core topics
04Practical programs
12Quiz questions
0 / 9Topics completed
YOUR LEARNING PATH

Choose a topic ๐ŸŒŸ

Read the notes, trace the example, and try each mini task before moving on.

๐Ÿงน
TOPIC 01

Hiding Redundancy

Reuse a function instead of repeating the same code.

๐Ÿ“˜ Understand the idea

A function groups statements under a name. When the same task appears in several places, define it once and call it whenever needed. This reduces duplication and makes updates easier.

Remember

Keep one source of truth: if the logic changes, update the function in one place.

Worked example

def show_welcome():
    print("Welcome to Python!")

show_welcome()
show_welcome()

๐Ÿง  Exam quick notes

  • Define a function using def, followed by a name and parentheses.
  • The function body must be consistently indented.
  • Call the function by writing its name followed by parentheses.
  • Test typical inputs and boundary cases.

✍️ Mini task

Why use a function when the same statements are needed several times?

๐Ÿงฉ
TOPIC 02

Hiding Complexity

Give a meaningful name to a multi-step operation.

๐Ÿ“˜ Understand the idea

A function can hide implementation details behind a simple name. A caller needs to know what the function does and what inputs it expects, not every internal step.

Remember

Choose descriptive names and keep each function focused on one responsibility.

Worked example

def rectangle_area(length, width):
    return length * width

print(rectangle_area(5, 3))

๐Ÿง  Exam quick notes

  • Define a function using def, followed by a name and parentheses.
  • The function body must be consistently indented.
  • Call the function by writing its name followed by parentheses.
  • Test typical inputs and boundary cases.

✍️ Mini task

How does a function help hide complexity?

๐Ÿ“ฅ
TOPIC 03

Parameters

Parameters are names in a function definition.

๐Ÿ“˜ Understand the idea

Parameters act as local names that receive values when a function is called. They make a function reusable with different inputs.

Remember

Parameters appear in the function definition, inside the parentheses.

Worked example

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

greet("Asha")

๐Ÿง  Exam quick notes

  • Define a function using def, followed by a name and parentheses.
  • The function body must be consistently indented.
  • Call the function by writing its name followed by parentheses.
  • Test typical inputs and boundary cases.

✍️ Mini task

In def greet(name), what is name?

๐Ÿ“จ
TOPIC 04

Arguments

Arguments are the values passed to a function call.

๐Ÿ“˜ Understand the idea

When calling a function, supply arguments for its parameters. The values can be literals, variables, or expressions.

Remember

Remember: parameter = definition; argument = call.

Worked example

def square(number):
    return number * number

result = square(6)
print(result)

๐Ÿง  Exam quick notes

  • Define a function using def, followed by a name and parentheses.
  • The function body must be consistently indented.
  • Call the function by writing its name followed by parentheses.
  • Test typical inputs and boundary cases.

✍️ Mini task

In square(6), what is 6?

↩️
TOPIC 05

Return Values

Use return to send a result back to the caller.

๐Ÿ“˜ Understand the idea

A return statement ends the current function call and provides a result. The caller can store that result or use it in an expression. A function without an explicit return returns None.

Remember

print() displays a value; return gives a value back to the caller.

Worked example

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

total = add(8, 4)
print(total)

๐Ÿง  Exam quick notes

  • Define a function using def, followed by a name and parentheses.
  • The function body must be consistently indented.
  • Call the function by writing its name followed by parentheses.
  • Test typical inputs and boundary cases.

✍️ Mini task

What does return do?

๐Ÿ”
TOPIC 06

Formal vs Actual Arguments

Distinguish parameter names from supplied values.

๐Ÿ“˜ Understand the idea

Formal parameters are the names written in a function definition. Actual arguments are the values or expressions supplied in a function call.

Remember

Use the exact terminology requested by your course: formal parameter in definition; actual argument in call.

Worked example

def multiply(x, y):  # x and y are formal parameters
    return x * y

print(multiply(3, 7))  # 3 and 7 are actual arguments

๐Ÿง  Exam quick notes

  • Define a function using def, followed by a name and parentheses.
  • The function body must be consistently indented.
  • Call the function by writing its name followed by parentheses.
  • Test typical inputs and boundary cases.

✍️ Mini task

Which are the formal parameters in multiply(x, y)?

๐Ÿท️
TOPIC 07

Named Arguments

Pass arguments using parameter names.

๐Ÿ“˜ Understand the idea

Keyword (named) arguments identify which parameter receives each value, making calls easier to read. In Python, positional arguments must come before keyword arguments in a call.

Remember

Example: describe(item="book", quantity=2). Names must match the function parameters.

Worked example

def describe(item, quantity):
    print(quantity, item)

describe(item="notebook", quantity=2)

๐Ÿง  Exam quick notes

  • Define a function using def, followed by a name and parentheses.
  • The function body must be consistently indented.
  • Call the function by writing its name followed by parentheses.
  • Test typical inputs and boundary cases.

✍️ Mini task

Why can named arguments improve readability?

๐ŸŒ€
TOPIC 08

Recursive Functions

A recursive function calls itself.

๐Ÿ“˜ Understand the idea

Recursion can express problems that break naturally into smaller versions of the same problem. Every correct recursive solution needs a base case to stop further calls.

Remember

Without a reachable base case, recursion may continue until Python raises RecursionError.

Worked example

def factorial(n):
    if n <= 1:  # base case
        return 1
    return n * factorial(n - 1)

print(factorial(5))

๐Ÿง  Exam quick notes

  • Define a function using def, followed by a name and parentheses.
  • The function body must be consistently indented.
  • Call the function by writing its name followed by parentheses.
  • Test typical inputs and boundary cases.

✍️ Mini task

What is the purpose of a base case?

⚡
TOPIC 09

Lambda Functions

Create a small anonymous function with one expression.

๐Ÿ“˜ Understand the idea

A lambda expression creates a function object, often for a short operation used briefly as an argument. A lambda body is a single expression, not a block of statements.

Remember

Use def for named, multi-step functions; use lambda for concise expressions.

Worked example

double = lambda n: n * 2
print(double(9))

๐Ÿง  Exam quick notes

  • Define a function using def, followed by a name and parentheses.
  • The function body must be consistently indented.
  • Call the function by writing its name followed by parentheses.
  • Test typical inputs and boundary cases.

✍️ Mini task

How many expressions can a lambda body contain?

HANDS-ON LEARNING

Practical Python Lab ๐Ÿงช

Minimum three practicals are required; four runnable exercises are provided. Edit the code and run it directly in your browser.

PRACTICAL 01

Practical 1 · Calculator Functions

Define and call separate functions for addition, subtraction, multiplication, and safe division.

๐Ÿ Python · Pyodide
Ready to run
OUTPUT
Click “Run Python” to execute this example.

The Python runtime loads on first use and needs an internet connection. Modify the code to experiment.

PRACTICAL 02

Practical 2 · Student Marks and Grade

Use parameters, a return value, and if/elif logic to classify a mark.

๐Ÿ Python · Pyodide
Ready to run
OUTPUT
Click “Run Python” to execute this example.

The Python runtime loads on first use and needs an internet connection. Modify the code to experiment.

PRACTICAL 03

Practical 3 · Recursive Factorial

Calculate factorial using a base case and a recursive call.

๐Ÿ Python · Pyodide
Ready to run
OUTPUT
Click “Run Python” to execute this example.

The Python runtime loads on first use and needs an internet connection. Modify the code to experiment.

PRACTICAL 04

Practical 4 · Lambda and Sorting

Use a lambda expression as a key to sort records by score.

๐Ÿ Python · Pyodide
Ready to run
OUTPUT
Click “Run Python” to execute this example.

The Python runtime loads on first use and needs an internet connection. Modify the code to experiment.

CHECK YOUR UNDERSTANDING

Unit 3 quiz ๐Ÿง 

Answer all 12 questions, then check your score and read the explanations.

1. Which keyword defines a function?

2. What is the main benefit of hiding redundancy?

3. In def greet(name):, name is a…

4. In square(5), 5 is an…

5. Which keyword sends a result back to the caller?

6. What does a function return if it has no return statement?

7. Which is a keyword argument call?

8. What are formal parameters?

9. What must a recursive function normally have?

10. What does factorial(4) return for a correct factorial function?

11. A lambda function body contains…

12. What does sorted(items, key=lambda x: x[1]) do?

Submit to reveal the correct answers and explanations.

YOUR LEARNING PROGRESS

Celebrate each step ๐Ÿ†

Tick each topic after reading the notes and attempting its mini task.

Unit III progress0%

๐ŸŽฏ Learning outcomes

By the end of Unit III, you should be able to create reusable functions, distinguish parameters from arguments, return values, use named arguments, explain recursion, and write simple lambda expressions.

CONTINUE LEARNING

References & learning resources ๐Ÿ“š

CS25C02 · Unit III: Functions · Interactive Python Study Hub

Comments

Popular posts from this blog

index.html

Python #1 Fundamentals

#Python YT Videos for Jamal Engineering Students