MCS-201 · Unit 11

Functions and File Handling in Python

Section 1 of 8

Block 3 · Unit 11Functions and File Handling in Python8 min · Section 1 of 8

11.1Defining and Calling Functions

Covers IGNOU MCS-201 §11.2, 11.2.1

A function is a named block of code that performs one task. You have already used many: print(), len(), input(). This section shows how to write your own.

Why functions

  • Decomposition: a large problem is split into smaller, manageable parts.
  • Reuse: write the code once, call it many times, even from other programs (Unit 12).
  • Less duplication: a fix in one place fixes every use.
  • Readability and testing: a well-named function explains itself and can be tested on its own.

Kinds of functions

KindMeaningExamples
Built-inAlways available in Pythonprint(), max(), len(), input()
User-definedWritten by you with deffactorial(), calculate_tax()
Anonymous (lambda)A small unnamed function written as one expressionlambda x: x * x

Defining a function

Syntax
def function_name(parameter_1, parameter_2, ...):
    """Optional docstring describing the function."""
    statement(s)
  • def starts the function definition; the name follows the identifier rules.
  • The parentheses hold zero or more parameters: names that receive the input values.
  • The header ends with a colon, and the indented lines form the function body.
  • A string on the first line of the body is the function’s docstring, shown by help(function_name).

Defining a function only creates it; nothing in the body runs until the function is called: function_name(argument_1, argument_2, ...). The values given in the call are arguments; each one is bound to the matching parameter.

ParameterArgument
WhereIn the function definitionIn the function call
WhatA name waiting for a valueThe actual value passed
Examplen in def factorial(n):5 in factorial(5)

Key takeaways

  • Functions break programs into reusable, testable pieces.
  • Python has built-in, user-defined and anonymous (lambda) functions.
  • def defines; a call with parentheses runs the body.
  • Parameters receive arguments.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

What does def do?

Show answer

Answer: B. Defines a function

def creates a function object and binds it to the name; the body runs only when the function is called.

Question 2 · multiple choice

In def area(length, width):, what are length and width?

Show answer

Answer: A. Parameters

Names in the definition are parameters; the values passed in a call such as area(4, 5) are arguments.

Question 3 · multiple choice

What does this program print?

Python · quiz_def.py
def show():
    print("inside")

print("start")
show
print("end")
Show answer

Answer: B. start, end

show without parentheses only refers to the function; it is never called, so “inside” is not printed.

Question 4 · multiple choice

Which is a built-in function?

Show answer

Answer: C. max()

max() is always available; the others would have to be written by you, and def is a keyword.

Question 5 · multiple choice

What does this program print?

Python · quiz_square.py
def square(n):
    return n * n

print(square(3) + square(4))
Show answer

Answer: D. 25

9 + 16 = 25; each call returns a value that can be used in an expression.

Question 6 · short answer

Explain the difference between a function definition and a function call, with an example.

Show worked answer

A function definition creates the function: it gives the name, the parameter list and the body, as in def greet(name): print("Hello", name). Nothing is printed when this runs. A function call executes the body with actual values for the parameters, as in greet("Nav"), which prints Hello Nav. A function is defined once but can be called any number of times with different arguments.

↑ ↓ move↵ openTab switch type