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

FUNCTIONS notes

Chapter 7 discusses functions in Python, emphasizing their role in modular programming to enhance code readability, reduce repetition, and promote reusability. It covers user-defined functions, the importance of scope (local vs global variables), and the use of built-in and user-defined modules. Additionally, it explains docstrings for documentation and provides examples of function usage and module creation.

Uploaded by

ashas.bharath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

FUNCTIONS notes

Chapter 7 discusses functions in Python, emphasizing their role in modular programming to enhance code readability, reduce repetition, and promote reusability. It covers user-defined functions, the importance of scope (local vs global variables), and the use of built-in and user-defined modules. Additionally, it explains docstrings for documentation and provides examples of function usage and module creation.

Uploaded by

ashas.bharath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

FUNCTIONS

CHAPTER 7

Introduction
 Functions is a block of code defined with a name
 Functions in Python help make programs more modular and easier to
manage, particularly for complex problems.
 Example for function

Modular programming
 The process of dividing a computer program into separate
independent blocks of code or separate sub-problems with different
names and specific functionalities is known as modular
programming
The Advantages of Functions
 1. Readability: Functions make code more organized and easier
to understand.
 2. Reduced Code Length: Functions prevent code repetition,
making programs shorter.
 3. Reusability: Functions can be reused in other programs.
 4. Parallel Work: Different team members can work on separate
functions simultaneously.
User Defined Functions
User-defined functions are created by the programmer to perform specific
tasks.
The syntax for defining a function is:
 The items enclosed in "[ ]" are called parameters and they are optional.
 Function header always ends with a colon (:).
 Function name should be unique. Rules for naming identifiers also applies for function
naming.
 The statements outside the function indentation are not considered as part of the function.
Arguments and Parameters

Id()
 The id() function returns a unique id for the specified objects.
 All objects in python has its own unique id.
 The id I assigned to the object when it is created.
 The id() is the object’s memory address and will be different for each
time you run the program.
String as parameter
 If user need to pass string values as an argument
 Python string concatentation

 Default Parameters
 Default arguments in Python are the function arguments that will be
used if no arguments are passed to the function call.
 The parameters should be in the same order as that of the arguments
 Example of Default Arguments
 The following example shows use of Python default arguments.
Here, the second call to the function will not pass value
to "city" argument, hence its default value "Hyderabad" will be used.

Flow of Execution
 Flow of execution can be defined as the order in which the
statements in a program are executed.
 Observe the order in which the statements are executed in the
program when the function call is encountered

Scope of a Variable
 A variable is only available from inside the region it is created. This
is called scope
 Variables in Python can have either local or global scope:
 Global Variables: Defined outside any function and accessible from
anywhere in the code.
 Local Variables: Defined inside a function and accessible only
within that function.
Python Standard Library
 The Python Standard Library is a collection of many built-in
functions that can be used directly in programs.
 Python has a very extensive standard library
Built-in functions
 Built-in functions are pre-defined and readily available for use
 Examples include input(), int(), and print().
 They perform specific operations, such as getting user input,
converting data types, and printing output
Module
 The Python standard library also consists of a number of modules.
 A module in Python is a file containing Python definitions and statements.
 The file name is the module name with the suffix .py added.
Modules help in organizing code logically and can be reused across different
program
Built-in Modules
 Math Module: Provides mathematical functions such as sqrt() and
sin().
 Random Module: Generates random numbers using functions like
random(), randint(), and randrange().
 Statistics Module: Calculates statistical measures with functions like
mean(), median(), and mode().
 Modules are used in a program by importing them using the import
statement.
 Functions of a module are accessed with the syntax: module name
 modulename.functionname()
Module name : random

Module name : statistics


Note
 import statement can be written anywhere in the program • Module must be imported only
once
 In order to get a list of modules available in Python, we can use the following statement: >>>
help("module")
 To view the content of a module say math, type the following: >>> help("math")
 The modules in the standard library can be found in the Lib folder of Python.

From Statement
 Instead of importing all the functions of a module, specific functions
can be imported from a module with syntax:
 from modulename import function1, function2, …… .
 When specific functions from a module is imported using from
keyword, then the function name can be used directly without prefix
its name with its module name.
 Its syntax is >>> from modulename import functionname [,
functionname,...]
 Example
 >>> from math import ceil,sqrt
 >>> value = ceil(624.7)
 >>> sqrt(value)
Additional Examples Composition in function calls, where the output of
one function is used as the input for another:
 The execution of the function sqrt() is dependent on the output of
ceil() function.
 If we want to extract the integer part of 624.7 (we will use trunc()
function from math module),
 we can use the following statements.
 #ceil and sqrt already been imported above
 >>> from math import trunc >>> sqrt(trunc(625.7))

Creating User-Defined Modules


 Besides using built-in modules, you can also create your own
modules.
 A user-defined module is a Python file containing functions and
variables you define
 Steps to Create a Module:
 1. Write the functions you want to include in the module.
 2. Save the file with a .py extension.
 3. Import the module in your script and use its functions

This module contains basic arithmetic operations that can be carried out
on numbers

Using Docstrings
 Docstrings are documentation strings in Python, used to describe
modules, functions, classes, or methods.
 To display the docstring of a module, import the module and use the
__doc__ attribute:
 print(basic_math.__doc__)
 print(.<module name>.__doc__)
 #__ are 2 underscore without space
 This example shows how to use docstrings for module
documentation.
(Important note : For example programs plz refer textbook)

Important question

1. Define modular programming


2. Explain the advantages of using functions in a program.
3. Define user-defined function. Give the syntax to create user-defined functions.
4. Distinguish between Arguments and parameters with a suitable example.
5. What is default parameter? Explain with a suitable example.
6. Define local variable.
7. Define Global variable.
8. Mention the two scopes a variable can have.
9. What are the two important constituents of a Python Standard Library?
10. What is module? Give examples for built-in modules.
11. Explain different types of user-defined functions in python with syntax and examples.
12. Differentiate between Global variable and Local variable

You might also like