Skip to content

Language

Sign in

Learn › Class 12 › Computer Science / Informatics Practices › Python revision, functions, exceptions

Functions in Python

You will learn to

  • Define and call functions with parameters and return values
  • Use positional, default and keyword arguments correctly
  • Tell local and global scope apart

Helpful to know first: Python basics: variables, operators, if and loops; Lists, tuples and dictionaries.

A function is a named block of code that does one job. Python has built-in functions such as len(), functions in modules such as math.sqrt(), and functions you define yourself.

You define a function with def, a name, parameters in brackets and a colon. The return statement sends a value back. A function without return gives back None.

Kinds of arguments

Positional arguments match parameters by order. Default parameters, as in def area(l, b=1), may be left out. Keyword arguments name the parameter, as in area(b=4, l=3). Defaults must come after non-default parameters.

A variable created inside a function is local and disappears when the function ends. A variable created outside all functions is global. To change a global variable inside a function, declare it with the global keyword.

Python passes references to objects. A function can change a list it receives, because lists are mutable. Reassigning an int or a string inside the function does not affect the caller.

Common mix-ups

  • Not quite: print() inside a function and return do the same thing.

    Instead: print() only shows a value on screen. return hands the value back so the caller can store or use it.

  • Not quite: Assigning to a variable inside a function changes the global variable of the same name.

    Instead: It creates a new local variable, unless you first write global name inside the function.

Reviewed by EduFlint team on 1 Dec 2026 · version 1

Written by us for this site. Independent; not affiliated with NCERT or CBSE.

Report a mistake

Looking for another topic?