Back to CS Curriculum

Functions in Python

CBSE Class 12 - Computer Science

Functions in Python

Functions are a fundamental concept in Python programming that help organize code, improve readability, and enable code reuse.

What are Functions?

A function is a block of reusable code designed to perform a specific task. It helps in organizing the code, making it more readable and easier to maintain. Functions can take inputs, process them, and return an output.

Functions can be broadly classified into two categories:

1. Built-in Functions

Functions that are already defined in Python, such as print(), len(), sum(), etc.

2. User-Defined Functions

Functions created by the user to perform specific tasks.

User-Defined vs. Built-in Functions

Built-in Functions

These are pre-defined in Python and can be used directly without any need for definition. Example: print(), max(), type().

User-Defined Functions

These are functions defined by the user to encapsulate a specific task or set of tasks. Example:

Modules in Python

A module is a file containing Python definitions and statements. Modules help in organizing and reusing code across different projects.

Commonly used modules:

  • random: For generating random numbers.
  • statistics: For performing statistical calculations.
  • math: For mathematical functions like square root, factorial, etc.

Example of using modules:

Types of User-Defined Functions

1. Functions with no arguments and no return value:

2. Functions with arguments but no return value:

3. Functions with arguments and a return value:

4. Functions with no arguments but a return value:

Formal Arguments vs. Actual Arguments

Formal ArgumentsActual Arguments
Values received by the function are called parameters (or formal arguments).Values passed to the function are called arguments (or actual arguments).
Appear in the function header or function definition.Appear in the function call statement.
Also known as Formal Parameters or Formal Arguments.Also known as Actual Parameters or Actual Arguments.

Types of Function Arguments

1. Positional Arguments

The most straightforward way of passing arguments to a function. The arguments passed are assigned to the parameters in the order they appear.

2. Default Arguments

You can define default values for parameters. If no value is provided during the function call, the default value is used.

3. *args (Variable-Length Arguments)

Allows a function to accept any number of positional arguments. The arguments are passed as a tuple.

4. **kwargs (Keyword Arguments)

Allows a function to accept any number of keyword arguments. The arguments are passed as a dictionary.

Scope of Variables

1. Local Scope

Variables defined within a function have a local scope, meaning they can only be accessed within that function.

2. Global Scope

Variables defined outside any function have a global scope, meaning they can be accessed anywhere in the code.

3. Nonlocal Scope

The nonlocal keyword is used to indicate that a variable is not local to the current function but is in the nearest enclosing scope.

Nested Functions

A nested function is a function defined inside another function. It can access variables from the outer function's scope.

Example of a nested function:

Diagram showing functions and return values in Python
Get in Touch