Grade 12 Exam  >  Grade 12 Notes  >  Computer Science for Grade 12  >  Detailed Notes: Functions

Detailed Notes: Functions | Computer Science for Grade 12 PDF Download

Download, print and study this document offline
Please wait while the PDF view is loading
 Page 1


Computer Science
Class XII ( As per  CBSE Board)
Chapter 2
Functions
Page 2


Computer Science
Class XII ( As per  CBSE Board)
Chapter 2
Functions
Function Introduction
A function is a programming block of codes which is
used to perform a single, related task. It only runs
when it is called. We can pass data, known as
parameters, into a function. A function can return data
as a result.
We have already used some python built in functions
like print(),etc.But we can also create our own
functions. These functions are called user-defined
functions.
Page 3


Computer Science
Class XII ( As per  CBSE Board)
Chapter 2
Functions
Function Introduction
A function is a programming block of codes which is
used to perform a single, related task. It only runs
when it is called. We can pass data, known as
parameters, into a function. A function can return data
as a result.
We have already used some python built in functions
like print(),etc.But we can also create our own
functions. These functions are called user-defined
functions.
Advantages of Using functions:
1.Program development made easy and fast : Work can be divided among project
membersthusimplementationcanbecompletedfast.
2.Program testing becomes easy : Easy to locate and isolate a faulty function for
furtherinvestigation
3.Code sharing becomes possible : A function may be used later by many other
programs this means that a python programmer can use function written by
others,insteadofstartingoverfromscratch.
4.Codere-usabilityincreases:Afunctioncanbeusedtokeepawayfromrewriting
the same block of codes which we are going use two or more locations in a
program.Thisisespeciallyusefulifthecodeinvolved islongorcomplicated.
5.Increases program readability : The length of the source program can be
reduced by using/calling functions at appropriate places so program become
morereadable.
6.Function facilitates procedural abstraction : Once a function is written,
programmerwouldhavetoknowtoinvokeafunctiononly,notitscoding.
7.Functions facilitate the factoring of code : A function can be called in other
functionandsoon …
Page 4


Computer Science
Class XII ( As per  CBSE Board)
Chapter 2
Functions
Function Introduction
A function is a programming block of codes which is
used to perform a single, related task. It only runs
when it is called. We can pass data, known as
parameters, into a function. A function can return data
as a result.
We have already used some python built in functions
like print(),etc.But we can also create our own
functions. These functions are called user-defined
functions.
Advantages of Using functions:
1.Program development made easy and fast : Work can be divided among project
membersthusimplementationcanbecompletedfast.
2.Program testing becomes easy : Easy to locate and isolate a faulty function for
furtherinvestigation
3.Code sharing becomes possible : A function may be used later by many other
programs this means that a python programmer can use function written by
others,insteadofstartingoverfromscratch.
4.Codere-usabilityincreases:Afunctioncanbeusedtokeepawayfromrewriting
the same block of codes which we are going use two or more locations in a
program.Thisisespeciallyusefulifthecodeinvolved islongorcomplicated.
5.Increases program readability : The length of the source program can be
reduced by using/calling functions at appropriate places so program become
morereadable.
6.Function facilitates procedural abstraction : Once a function is written,
programmerwouldhavetoknowtoinvokeafunctiononly,notitscoding.
7.Functions facilitate the factoring of code : A function can be called in other
functionandsoon …
Creating & calling a Function
(user defined)/Flow of execution
A function is defined using the def keyword in python.E.g.
program is given below.
def my_own_function():
print("Hello from a function")
#program start here.program code  
print("hello before calling a function")
my_own_function() #function calling.now function codes will be executed
print("hello after calling a function")
Save the above source code in python file and execute it
#Function block/
definition/creation
Page 5


Computer Science
Class XII ( As per  CBSE Board)
Chapter 2
Functions
Function Introduction
A function is a programming block of codes which is
used to perform a single, related task. It only runs
when it is called. We can pass data, known as
parameters, into a function. A function can return data
as a result.
We have already used some python built in functions
like print(),etc.But we can also create our own
functions. These functions are called user-defined
functions.
Advantages of Using functions:
1.Program development made easy and fast : Work can be divided among project
membersthusimplementationcanbecompletedfast.
2.Program testing becomes easy : Easy to locate and isolate a faulty function for
furtherinvestigation
3.Code sharing becomes possible : A function may be used later by many other
programs this means that a python programmer can use function written by
others,insteadofstartingoverfromscratch.
4.Codere-usabilityincreases:Afunctioncanbeusedtokeepawayfromrewriting
the same block of codes which we are going use two or more locations in a
program.Thisisespeciallyusefulifthecodeinvolved islongorcomplicated.
5.Increases program readability : The length of the source program can be
reduced by using/calling functions at appropriate places so program become
morereadable.
6.Function facilitates procedural abstraction : Once a function is written,
programmerwouldhavetoknowtoinvokeafunctiononly,notitscoding.
7.Functions facilitate the factoring of code : A function can be called in other
functionandsoon …
Creating & calling a Function
(user defined)/Flow of execution
A function is defined using the def keyword in python.E.g.
program is given below.
def my_own_function():
print("Hello from a function")
#program start here.program code  
print("hello before calling a function")
my_own_function() #function calling.now function codes will be executed
print("hello after calling a function")
Save the above source code in python file and execute it
#Function block/
definition/creation
Variable’s Scope in function
There are three types of variables with the view of scope.
1. Local variable – accessible only inside the functional block where it is declared.
2. Global variable – variable which is accessible among whole program using global 
keyword.
3. Non local variable – accessible in nesting of functions,using nonlocal keyword.
Local variable program:
def fun(): 
s = "I love India!" #local variable
print(s)
s = "I love World!" 
fun()
print(s)
Output:
I love India!
I love World!
Global variable program:
def fun():
global s #accessing/making global variable for fun() 
print(s)
s = "I love India!“ #changing global variable’s value
print(s)
s = "I love world!" 
fun()
print(s)
Output:
I love world!
I love India!
I love India!
Read More
1 videos|25 docs|18 tests

Top Courses for Grade 12

1 videos|25 docs|18 tests
Download as PDF
Explore Courses for Grade 12 exam

Top Courses for Grade 12

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Extra Questions

,

Free

,

study material

,

Important questions

,

MCQs

,

ppt

,

practice quizzes

,

pdf

,

past year papers

,

mock tests for examination

,

Summary

,

Exam

,

Sample Paper

,

Previous Year Questions with Solutions

,

Detailed Notes: Functions | Computer Science for Grade 12

,

Detailed Notes: Functions | Computer Science for Grade 12

,

video lectures

,

Objective type Questions

,

shortcuts and tricks

,

Semester Notes

,

Viva Questions

,

Detailed Notes: Functions | Computer Science for Grade 12

;