Python functions class12 – 4 important insights of functions

In this article, Python functions class12 you will get detailed notes about function types, return values, and the scope of the function variables.

Python functions class12- function types

Python functions class12 - Python function types

Python supports three types of functions:

  • Built-in Functions: Pre-defined functions of Python such as input(), int(), max(), len() etc.
  • Functions defined in modules: Functions defined in particular modules, can be used when the module is imported. A module is a container of functions, variables, constants, and classes in a separate file that can be reused. The module can be imported in two ways
  1. import statement: Used to import the entire module. EX. import math
  2. from statement: import all functions or the selected one. EX. from random import randint
  • User-Defined Functions: Function created by the programmer

User-Defined Functions – Python functions class12

Python UDF example
Python UDF example

In the above program, two functions were created

i) simple_interest()

ii) main()

The simple_interest() function receives three parameters p,r and n. main() function prompts to input these values and is accepted as an argument in calling simple_interest() function. 

Parameters and Arguments in Function

Arguments and Parameters Python functions class 12

Parameters are the values provided at the time of function definition. For Ex. p,r and n.

Arguments are the values passed while calling a function. For Ex. princ_amt, r, n in main().

Types of Arguments:

Python supports four argument types:

  1. Positional Arguments: Arguments passed to a function in correct positional order, no. of arguments must match with no. of parameters required.
  2. Default Arguments: Assign default to value to a certain parameter, it is used when the user knows the value of the parameter, default values are specified in the function header. It is optional in the function call statement. If not provided in the function call statement then the default value is considered. Default arguments must be provided from right to left.
  3. Key Word Arguments: Keyword arguments are the named arguments with assigned values being passed in function call statement, the user can combine any type of argument.
  4. Variable Length Arguments: It allows the user to pass as many arguments as required in the program. Variable-length arguments are defined with * symbol.
Positional arguments in Python Functions
Positional arguments in Python Functions
default arguments default parameters in python functions
default arguments default parameters in python functions
def sum(*n):
                  for i in n:
                  print(“Sum = “, total)
 # Calling function
sum()  # o/p  sum=0
sum(10) #o/p  sum=10
sum(10,20,30,40) #o/p sum=100

Rules for combining three types of arguments

1. An argument list must contain positional arguments followed by any keyword argument.

2. Keyword arguments should be taken from the required arguments preferably.

3. Value of argument can’t be specified more than once.


cal_discount(amt=500,rate=10) or cal_discount(rate=10,amt=500)

Python functions class12-Returning values from function

A function in Python may or may not return a value. A function that does not return values is called a void function. A result will be printed in the same function.

A function that returns a value is a function that displays the calculated result. The value returned to a specific variable. The result will be printed in some other function.

Python functions class12 – Scope of Variable

A variable scope refers to access location of a variable.

There are two kinds of scopes:

Global Variable: A variable that is declared in top-level statements is called a global variable. To access the value of a global variable user needs to write a global keyword in front of the variable in a function. 

Local Variable: A name declared in a specific function body is called a local variable. 

Python global and local scopes
Python global and local scopes

Mutable/immutable of arguments/parameters & function calls

All variables in Python store the memory address of the actual object, not the value itself. Although mutable and immutable objects and variables are handled differently during working with function arguments. 

Python variable memory location
Python variable memory location

Immutable objects are those objects whose values cannot be changed. They allocate new memory whenever the value is changed. 

memory allocation in python
memory allocation in python

New memory allocated for x when value assigned x=45. Every time when value is changed memory address also gets changed. When new object is created old object will be discarded and memory cells make free for use. 

Passing immutable type value

immutable object passing in functions
immutable object passing in functions

Passing mutable type value (List)

As you know list, dictionary etc are mutable data types in python. 

mutable objects passing in functions - Python functions class12
mutable objects passing in functions

Passing Strings:

Consider following program:

Python functions class12 - 4 important insights of functions
passing string into python function

Thank you fore reading the article. Share your valuable feedback in comment section.

Checkout More

Check out python functions documentations.

If you are looking for questions from function follow this link:

Questions and Answers Working with functions class 12

Share this article with your friends and ask your doubt in comment section.

That all from python functions class12.

Thank you for reading this article.

2 thoughts on “Python functions class12 – 4 important insights of functions”

Leave a Reply