lecture 9
lecture 9
• 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.
TYPES OF FUNCTION
• 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 my_add(a,b):
• c=a+b
• return c
FUNCTION CALLING: (MAIN FUNCTION)
•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: