Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
25 views

Class XII - Computer Science - Chapter 3 - Working With Functions

The document provides an overview of functions in Python, explaining their importance in managing large programs by breaking them into smaller units. It details the types of functions including built-in, module, and user-defined functions, as well as the concepts of arguments, parameters, and the scope of variables. Additionally, it covers the structure of a Python program, the main function, and the flow of execution in function calls.

Uploaded by

vayuvarshney43
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Class XII - Computer Science - Chapter 3 - Working With Functions

The document provides an overview of functions in Python, explaining their importance in managing large programs by breaking them into smaller units. It details the types of functions including built-in, module, and user-defined functions, as well as the concepts of arguments, parameters, and the scope of variables. Additionally, it covers the structure of a Python program, the main function, and the flow of execution in function calls.

Uploaded by

vayuvarshney43
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

CLASS XII – COMPUTER SCIENCE (PYTHON)

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.

3) User-defined functions – Functions which are created/developed by user as per the


requirement of the user are known as user-defined functions. Function is a kind of collection
of statements which are written for a specific task. def keyword is used to make user defined
functions. To use the user-defined function, functions should be called/invoked.

Syntax –

Prepared by – Er. Animesh Gupta Page | 1


KRISHNA CLASSES

def func_name (arguments/parameters):


# block of statements
# body of function

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

 ARGUMENTS & PARAMETERS


 These are the values/variables/identifiers that are listed inside the parentheses of a function
while writing a function header.
 They are used by the function for a specific task.
 Values/variables are passed from a function, when a function is called/invoked. It is known as
arguments.
 Values/variables are received into a function, when a function is created/defined. It is known
as parameters.
 Arguments/parameters can be classified into two categories:
 Actual arguments or Actual parameters i.e., values that are passed from a function.
 Formal arguments or Formal parameters i.e., values that are received into a function.
For Ex –
def add (a, b): // Here a, b are formal arguments or formal parameters
c = a + b
print (“Sum = ”, c)

x = int (input (“Enter first number =”))


y = int (input (“Enter second number =”))

add (x, y) // Here x, y are actual arguments or actual parameters


add (15, 10) // Here 15, 10 are actual arguments or actual parameters

Prepared by – Er. Animesh Gupta Page | 2


CLASS XII – COMPUTER SCIENCE (PYTHON)

 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”)

Hello Elon ! Happy Birthday


wish (“Musk”)
wish ( ) Hello Musk ! Happy Birthday

3) Keyword Arguments – Also known as named arguments. Keyword arguments can be


used to change the order/sequence of arguments. No need to remember the position of the
argument, just pass the values with the argument name.
For Ex – Hello Shiv ! Good morning
def wish (name, msg) :
Hello Vishnu ! Good afternoon
print (“Hello”, name, “!”, msg)
Hello Pushkar ! Good evening
wish (“Shiv”, “Good morning”)
wish (name=“Vishnu”, msg=“Good afternoon”)
wish (msg=“Good evening”, name=“Pushkar”)

Prepared by – Er. Animesh Gupta Page | 3


KRISHNA CLASSES

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.

Two types of function returns are used in Python:


1) Void functions – Functions that perform a block of statements and does not return any final
value or result to the caller function are known as void functions. Void function may or may
not have a return statement.
For Ex –
def add (a, b) :
c = a + b
print (“The sum is ” c) The sum is 30

add (10, 20)

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) :

Prepared by – Er. Animesh Gupta Page | 4


CLASS XII – COMPUTER SCIENCE (PYTHON)

c = a + b
return c The sum is 100

sum = add (15, 85)


print (“The sum is ”, sum)
Actually, there are 4 types of function return. Following are the examples of each.
Void function with argument Void function without argument
def add (a, b) : def add ( ) :
c = a + b a, b = 30, 20
print (“Sum = ”, c) c = a + b
print (“Sum = ”, c)
add (30, 20)

Sum = 50 Sum = 50
Output : Output :

Non-Void function with argument Non-Void function without argument


def add (a, b) : def add ( ) :
c = a + b a, b = 30, 20
return c c = a + b
return c
sum = add (30, 20)
print (“Sum = ”, sum) sum = add ( )
print (“Sum = ”, sum)

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.

Prepared by – Er. Animesh Gupta Page | 5


KRISHNA CLASSES

For Ex –
def add (a, b): // Here variables a and b have local scope within add ( )
c = a + b
print (“Sum = ”, c)

x = int (input (“Enter first number =”))


y = int (input (“Enter second number =”))

add (x, y) // Here variables x and y have global scope

Whenever a variable is accessed within a program or a function, Python follows Name


Resolution rule, also known as LEGB rule.
 If the variable is found in Local environment (or local namespace), it uses its name
and its value. If not, then moves to next step.
 If the variable is found in Enclosing environment (or
enclosing namespace), it uses its name and its value. If
not, then moves to next step.
 If the variable is found in Global environment (or
global namespace), it uses its name and its value. If
not, then moves to next step.
 If the variable is found in Built-in environment (or built-in namespace), it uses its
name and its value. If not, then Python will report an error.

 STRUCTURE OF A PYTHON PROGRAM


A Python program generally starts the execution from the statement __main__, also known as top-
level statements (without indentation). These statements are not a part of any function.
def function1 ( ) :
:
def function2 ( ) :
:
def function3 ( ) :
:
:
#top-level statements
statement1
statement2
:
Python stores this name in a built-in variable called __name__, it will give output “__main__”

Prepared by – Er. Animesh Gupta Page | 6


CLASS XII – COMPUTER SCIENCE (PYTHON)

 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 ( )

 FLOW OF EXECUTION IN A FUNCTION CALL


 The execution of any program starts from the very first line and this execution goes line by
line.
 One statement is executed at a time.
 Function doesn’t change the flow of any program.
 Statements of function doesn’t start executing until it is called.
 When function is called then the control is jumped into the body of function.
 Then all the statements of the function get executed from top to bottom one by one.
 And again the control returns to the place where it was called.
 And in this sequence the python program gets executed.

# Trace the flow of execution for the given Python program.


1. def power (b, p) :
2. y = b ** p
3. return y
4.
5. def calcSquare (x) :

Prepared by – Er. Animesh Gupta Page | 7


KRISHNA CLASSES

6. a = power (x, 2)
7. return a
8.
9. n = 5
10. result = calcSquare (n) + power (3, 3)
11. print (result)

Flow of execution will be:

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

Prepared by – Er. Animesh Gupta Page | 8

You might also like