Lecture-18 Modularisation 24-25
Topic: Modularisation in Python Functions
Focus: Understanding modularisation, function definitions, and practical examples.
Course Code: COMP101
Lecture Number: 18
Year: 2024-25
Topic: Modularisation (Python functions)
Definition: Modularisation is the process of breaking code into smaller, manageable segments called functions.
Terminology:
Common Terms: Functions, Procedures, Routines, Subroutines, Modules, Methods.
Python's Preferred Terminology: Functions.
Related Concepts: Function Definition, User-defined Function.
Functions:
Serve specific tasks and return values.
Procedures:
Serve specific tasks but do not return values.
Python allows both functionalities in functions.
Modules:
Collections of functions performing related tasks.
Importance:
Functions improve code efficiency, maintenance, and reusability.
Syntax: def funct_name():
def
: Keyword to declare a function.
funct_name
: User-defined function name.
Parentheses ()
and colon :
follow the name.
Indented code: Code to be executed when called.
Function Execution:
Control returns to the statement after the caller post execution.
Non-Modular Code Example:
Directly prints menu and handles options within main body.
Modular Code Example:
Introduces the use of functions to handle repetitive tasks like displaying under-development messages.
Function named stub
can be reused as needed.
Function Definition:
Uses def
followed by function name.
Flow of Control Steps:
Call function using its name and parentheses.
Function executes its indented code.
Upon completion, control returns to the caller.
Starting a program with main()
function:
Use def main():
to define the main function.
Always call main()
at the end to start program execution.
Import statements precede function definitions.
Example Functions:
howToDoIf()
and howToDoIfElse()
demonstrate conditional statements.
Main function displays menu and calls respective functions based on user input.
Program Execution Pattern:
Python scans for the main()
call and executes the structured indented code.
Step 1: Define the main function and call it.
Ensures that the program starts from a defined entry point.
Step 2: Adding a while loop in main()
to ensure continuous menu display until exit.
User can only exit with 'X'.
Step 3: Include input validation for option selection.
Prompt re-entry for invalid inputs.
Step 4: Define specific functions (e.g., partyDrinks()
, findTheCards()
) for each menu option.
Ensure functions are defined before use in the main program.
Step 5: Add loop functionality in specific functions to allow user re-entry.
Example: partyDrinks()
is tailored to run multiple times.
Demonstrated the modular program using entry condition check via if __name__
statement.
Example of merged operational and standalone functions.
__name__
ConceptExplanation:
Python identifies .py files as modules, executing the code starting from main()
.
Importance of if __name__ == '__main__':
:
Distinguishes main program execution vs. when imported elsewhere.
The convention of utilizing double underscores:
Called "dunder" methods, which Python recognizes for specific functionalities.
Topic: Modularisation in Python Functions
Focus: Understanding modularisation, function definitions, and practical examples.
Course Code: COMP101
Lecture Number: 18
Year: 2024-25
Topic: Modularisation (Python functions)
Definition: Modularisation is the process of breaking code into smaller, manageable segments called functions.
Terminology:
Common Terms: Functions, Procedures, Routines, Subroutines, Modules, Methods.
Python's Preferred Terminology: Functions.
Related Concepts: Function Definition, User-defined Function.
Functions:
Serve specific tasks and return values.
Procedures:
Serve specific tasks but do not return values.
Python allows both functionalities in functions.
Modules:
Collections of functions performing related tasks.
Importance:
Functions improve code efficiency, maintenance, and reusability.
Syntax: def funct_name():
def
: Keyword to declare a function.
funct_name
: User-defined function name.
Parentheses ()
and colon :
follow the name.
Indented code: Code to be executed when called.
Function Execution:
Control returns to the statement after the caller post execution.
Non-Modular Code Example:
Directly prints menu and handles options within main body.
Modular Code Example:
Introduces the use of functions to handle repetitive tasks like displaying under-development messages.
Function named stub
can be reused as needed.
Function Definition:
Uses def
followed by function name.
Flow of Control Steps:
Call function using its name and parentheses.
Function executes its indented code.
Upon completion, control returns to the caller.
Starting a program with main()
function:
Use def main():
to define the main function.
Always call main()
at the end to start program execution.
Import statements precede function definitions.
Example Functions:
howToDoIf()
and howToDoIfElse()
demonstrate conditional statements.
Main function displays menu and calls respective functions based on user input.
Program Execution Pattern:
Python scans for the main()
call and executes the structured indented code.
Step 1: Define the main function and call it.
Ensures that the program starts from a defined entry point.
Step 2: Adding a while loop in main()
to ensure continuous menu display until exit.
User can only exit with 'X'.
Step 3: Include input validation for option selection.
Prompt re-entry for invalid inputs.
Step 4: Define specific functions (e.g., partyDrinks()
, findTheCards()
) for each menu option.
Ensure functions are defined before use in the main program.
Step 5: Add loop functionality in specific functions to allow user re-entry.
Example: partyDrinks()
is tailored to run multiple times.
Demonstrated the modular program using entry condition check via if __name__
statement.
Example of merged operational and standalone functions.
__name__
ConceptExplanation:
Python identifies .py files as modules, executing the code starting from main()
.
Importance of if __name__ == '__main__':
:
Distinguishes main program execution vs. when imported elsewhere.
The convention of utilizing double underscores:
Called "dunder" methods, which Python recognizes for specific functionalities.