0% found this document useful (0 votes)
3 views20 pages

Understanding Python Functions and Modules

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)
3 views20 pages

Understanding Python Functions and Modules

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

Functions

• Function is a sub program which consists of set of instructions used to perform a


specific task. A large program is divided into basic building blocks called function.
• Need For Function:
• When the program is too complex and large they are divided into parts. Each part
is separately coded and combined into single program. Each subprogram is called
as function.
• Debugging, Testing and maintenance becomes easy when the program is divided
into subprograms.
• Functions are used to avoid rewriting same code again and again in a program.
• Function provides code re-usability
• The length of the program is reduced.
Functions
• User defined function
• Built in function
• Built in functions
• Built in functions are the functions that are already created and
stored in python.
• These built in functions are always available for usage and accessed
by a programmer. It cannot be modified.
Functions
Built in function Description
>>>max(3,4) # returns largest element
4
>>>min(3,4) # returns smallest element
3
>>>len("hello") #returns length of an object
5
>>>range(2,8,1) #returns range of given values
[2, 3, 4, 5, 6, 7]
>>>round(7.8) #returns rounded integer of the given number
8.0
>>>chr(5) #returns a character (a string) from an integer
\x05'
>>>float(5) #returns float number from string or integer
5.0
>>>int(5.0) # returns integer from string or float
5
>>>pow(3,5) #returns power of given number
243
>>>type( 5.6) #returns data type of object to which it belongs
<type 'float'>
>>>t=tuple([4,6.0,7]) # to create tuple of items from list
(4, 6.0, 7)
>>>print("good morning") # displays the given object
Good morning
>>>input("enter name: ") # reads and returns the given string
enter name : George
User Defined Functions
• User defined functions are the functions that programmers create for
their requirement and use.
• These functions can then be combined to form module which can be
used in other programs by importing them.
• Advantages of user defined functions:
• Programmers working on large project can divide the workload by making
different functions.
• If repeated code occurs in a program, function can be used to include those
codes and execute when needed by calling that function.
Function definition: (Sub program)
• def keyword is used to define a function.
• Give the function name after def keyword followed by parentheses in
which arguments are given.
• End with colon (:)
• Inside the function add the program statements to be executed
• End with or without return statement
Function definition: (Sub program)
Syntax:
• def fun_name(Parameter1,Parameter2…Parameter n):
• statement1
• statement2…

• statement n
• return[expression]
Example:
• def my_add(a,b):
• c=a+b
• return c
Function Calling: (Main Function)
• Once we have defined a function, we can call it from another function, program
or even the Python prompt.
• To call a function we simply type the function name with appropriate
arguments.
Example:
• x=5
• y=4
• my_add(x,y)
Flow of Execution:
• The order in which statements are executed is called the flow of execution
• Execution always begins at the first statement of the program.
• Statements are executed one at a time, in order, from top to bottom.
• Function definitions do not alter the flow of execution of the program, but
remember that statements inside the function are not executed until the function
is called.
• Function calls are like a bypass in the flow of execution. Instead of going to the
next statement, the flow jumps to the first line of the called function, executes all
the statements there, and then comes back to pick up where it left off.
Note: When you read a program, don’t read from top to bottom. Instead, follow
the flow of execution. This means that you will read the def statements as you are
scanning from top to bottom, but you should skip the statements of the function
definition until you reach a point where that function is called.
Function Prototypes:
i) Function without arguments and without return type
• In this type no argument is passed through the function call and no output is return to main
function
• The sub function will read the input values perform the operation and print the result in the
same block
ii) Function with arguments and without return type
• Arguments are passed through the function call but output is not return to the main function
iii) Function without arguments and with return type
• In this type no argument is passed through the function call but output is return to the main
function.
iv) Function with arguments and with return type
• In this type arguments are passed through the function call and output is return to the main
function
Parameters and Arguments:
Parameters:
• Parameters are the value(s) provided in the parenthesis when we write function header.
• These are the values required by function to work.
• If there is more than one value required, all of them will be listed in parameter list
separated by comma.
• Example: def my_add(a,b):
Arguments:
• Arguments are the value(s) provided in function call/invoke statement.
• List of arguments should be supplied in same way as parameters are listed.
• Bounding of parameters to arguments is done 1:1, and so there should be same number
and type of arguments as mentioned in parameter list.
• Example: my_add(x,y)
RETURN STATEMENT
• The return statement is used to exit a function and go back to the place from where it was called.
• If the return statement has no arguments, then it will not return any values. But exits from
function.
Syntax:
• return[expression]
Example:
• def my_add(a,b):
• c=a+b
• return c
• x=5
• y=4
• print(my_add(x,y))
• Output: 9
ARGUMENTS TYPES
[Link] Arguments
[Link] Arguments
[Link] Arguments
[Link] length Argument
Required Arguments: The number of arguments in the function call should match
exactly with the function definition.
def my_details( name, age ):
print("Name: ", name)
print("Age ", age)
return
my_details("george",56)
Keyword Arguments:
• Python interpreter is able to use the keywords provided to match the
values with parameters even though if they are arranged in out of order.
• def my_details( name, age ):
print("Name: ", name)
print("Age ", age)
return
my_details(age=56,name="george")
Output:
• Name: george
• Age 56
Default Arguments:
Assumes a default value if a value is not provided in the function call for that
argument.
• def my_details( name, age=40 ):
print("Name: ", name)
print("Age ", age)
return
my_details(name="george")
Output:
• Name: george
• Age 40
Variable length Arguments
• If we want to specify more arguments than specified while defining
the function, variable length arguments are used. It is denoted by *
symbol before parameter.
• def my_details(*name ):
print(*name)
my_details("rajan","rahul","micheal",ärjun")
Output:
• rajan rahul micheal ärjun
MODULES
• A module is a file containing Python definitions ,functions, statements and
instructions.
• Standard library of Python is extended as modules.
• To use these modules in a program, programmer needs to import the module.
• Once we import a module, we can reference or use to any of its functions or
variables in our code.
• There is large number of standard modules also available in python.
• Standard modules can be imported the same way as we import our user-defined
modules.
• Every module contains many function.
• To access one of the function , you have to specify the name of the module and
the name of the function separated by dot . This format is called dot notation.
Syntax
• import module_name
• module_name.function_name(variable)
Four Ways to Import Module

You might also like