Class XII - Computer Science - Chapter 3 - Working With Functions
Class XII - Computer Science - Chapter 3 - Working With Functions
Chapter 3
FUNCTIONS
FUNCTIONS
Large programs are difficult to manage without functions.
A large program is broken down into smaller units known as functions.
A function is a subprogram that acts on data and often returns a value.
Function is a collection of statements which is made to perform a specific task.
TYPES OF FUNCTIONS
Three types of functions are used in Python:
1) Built-in functions – These functions are predefined in Python. A built-in function is a
ready-to-use function. Functions make any programming language efficient and provide a
structure to language. Python has many built-in functions which makes programming easy,
fast and efficient. They always reside in standard library and we need not to import any
module to use them.
For Ex – int ( ), str ( ), float ( ), input ( ), eval ( ), min ( ), max ( ), type ( ), range ( ), etc.
2) Module functions – Module is a simple Python file (.py file) which contains the definitions
of functions and variables. When we divide a program into modules then each module
contains functions and variables and each function is made for a special task. Once a code
is written in modules, it can also be used in other programs. To use the module functions,
respective modules should be import.
For Ex – math module, random module, string module, statistics module, etc.
Syntax –
where,
def means a function definition is starting
func_name states the name of the function, name can be built-in or user-defined
arguments/parameters are listed inside parentheses as variables/identifiers
colon is added to at the end of def line, means it requires a block
def, func_name, arguments/parameters, colon together makes a function header
body of function contains the block of statements or the functionality of function
TYPES OF ARGUMENTS
Three types of functions are used in Python:
1) Positional Arguments – Also known as required arguments or mandatory arguments.
These are the arguments which are passed in correct positional order in function. No value
can be skipped from the function call. Number of arguments and number of parameters
should be equal. If we change the position of the arguments, then the answer will be changed.
For Ex –
def subtract (a, b) :
100
print (a – b)
-100
subtract (200, 100)
subtract (100, 200)
2) Default Arguments – These are the arguments through which default values can be
provided to the variables. If we don’t pass any value to the function, then it will take a pre-
defined value. Default values are specified in the function header. Non-default arguments
cannot follow default arguments.
For Ex –
def wish (name = “Elon”) :
print (“Hello”, name, “! Happy Birthday”)
4) Variable Length Arguments – Any number of arguments can be passed as per the
requirement of the user in variable length arguments. (*) asterisk symbol is used to give
Variable length argument.
For Ex –
def sum (*x) :
Sum = 0
s = 0
for i in x: Sum = 10
s = s + i Sum = 30
print (“Sum = ”, s)
Sum = 60
sum ( )
sum (10)
sum (10, 20)
sum (10, 20, 30)
FUNCTION RETURN
The return statement is used to return the final computed result or value. The return statement
should be the last statement because the statements written after return statement will not be
executed by Python interpreter as it ends the function execution.
2) Non-void functions – Functions that perform a block of statements and returns any final
value or result to the caller function are known as non-void functions. Non-void function
always contains a return statement. Returned value can be a literal, variable or an expression.
For Ex –
def add (a, b) :
c = a + b
return c The sum is 100
Sum = 50 Sum = 50
Output : Output :
Sum = 50 Sum = 50
Output : Output :
SCOPE OF VARIABLES
Scope of variable means the part of program where the variable will be visible. It means the space
or area in which a variable can be used or can execute its powers. Scope is the collection of variables
and their values.
Scope can be classified into two types –
1) Global Scope – All those names/variables/identifiers which are assigned at top level in
module or directly assigned in interpreter. A global variable can be used in whole program.
Variables which are defined outside all the functions are always global variables.
2) Local Scope – Those variables which are assigned inside a function and it can be used only
in that function only.
For Ex –
def add (a, b): // Here variables a and b have local scope within add ( )
c = a + b
print (“Sum = ”, c)
MAIN ( )
Main function is an optional statement in Python.
It is also a top-level statement.
Every Python program starts the execution of program from the main ( ), if it is written.
For the convenience of Non-python programmers, we can use it as follows –
def hello ( ) :
print (“Hello World.”)
This is the main function.
def greet ( ) :
Hello World.
print (“Good Morning.”)
Good Morning.
def main ( ) :
print (“This is the main function.”)
hello ( )
greet ( )
main ( )
6. a = power (x, 2)
7. return a
8.
9. n = 5
10. result = calcSquare (n) + power (3, 3)
11. print (result)
1 → 5 → 9 → 10 → 5 → 6 →1 → 2 → 3 → 6 → 7 → 10 → 1 → 2 → 3 → 10 → 11
_____________________
KRISHNA CLASSES
A Place Where Concept Communicates.
Mob : 6396912366, 8474970970
Add. : Krishna Public School, Vidhyapati Nagar, Behind C to C Mall, Hathras, UP - 204101