0% found this document useful (0 votes)
4 views

1.Chapter 2 -Functions

Chapter 2 discusses functions in programming, defining them as blocks of code that perform specific tasks and can be reused to enhance program development, testing, and readability. It categorizes functions into built-in, module-defined, and user-defined types, and explains the concepts of arguments and variable scope. Additionally, it differentiates between global and local variables based on their lifetime and visibility within a program.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

1.Chapter 2 -Functions

Chapter 2 discusses functions in programming, defining them as blocks of code that perform specific tasks and can be reused to enhance program development, testing, and readability. It categorizes functions into built-in, module-defined, and user-defined types, and explains the concepts of arguments and variable scope. Additionally, it differentiates between global and local variables based on their lifetime and visibility within a program.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Chapter -2

Functions
Introduction
• A group of statements within a program that perform a specific task.
• Usually one task of a large program.
• Functions can be executed in order to perform overall program task known as
divide and conquer approach.
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/arguments into a function. A function can return data as a result.
Advantages of Using functions
1. Program development made easy and fast : Work can be divided among
project members thus implementation can be completed fast.
2. Program testing becomes easy : Easy to locate and isolate a faulty function
for further investigation
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, instead of starting over from scratch.
4. Code re-usability increases : A function can be used to keep away from
rewriting the same block of codes which we are going use two or more
locations in a program. This is especially useful if the code involved is long or
complicated.
5. Increases program readability : It makes possible top down modular
programming. The length of the source program can be reduced by using
functions at appropriate places.
TYPES OF FUNCTIONS
Functions can be categorized into three types
1)Built in Functions
2)Functions defined in Modules
3)User defined functions

1)Built in Functions
These are predefined function in python and are used as and when there is need by
simply calling them.
Example:
int(),float(),str(),tuple(),list(),dict()
min(),max() ,input(),eval(),len(),type(),round(),range(),abs()
sum(),chr(),ord(),print()

1
round (): The function rounds a given floating point number to the specified
number of digits and returns the result. round(12.78914,2) results in 12.79.
range (): The function is used to define a series of numbers.

2)Functions defined in modules


These functions are predefined in particular modules and can only be used
when the corresponding module is imported.
Eg:if we want to use a predefined function sqrt(),we need to first import the module
math in our program and call for the function math.sqrt().

3)User defined functions

2
3
4
Arguments which are mentioned in the function call is known as the actual argument.
Arguments which are mentioned in the definition of the function is called formal
arguments. Formal arguments are very similar to local variables inside the function.

5
6
Example

7
8
Variable-length arguments
Variable-length arguments are arguments that can take an unspecified amount of
input.
• pass multiple values with single argument name.

9
10
Scope of Variables
• It refers to the part of the program where it is visible.
• Lifetime: Life time of a variable is the time for which a variable lives in
memory.
• Two types of scope of variables –
o Global scope
o Local scope
Global Variable:
• They live in memory as long as the program is running
(lifetime is entireprogram).
• Names assigned at the top level of a module (outside the function)
Local Variable:
• They live in memory as long as their function is being executed
(lifetime is their function’s run).
• Names assigned inside a function definition or loop.
Example:

11
Predict the output

12
13
14
15
16

You might also like