FUNCTIONS notes
FUNCTIONS notes
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
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))
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