Week-6-1
Introduction to Functions
Functions are defined using the
def
keyword followed by a function name, with parentheses and a colon.The statements that make up the function's body follow and are intended to group together a number of statements for convenience.
Function Definition and Purpose
Functions enable code organization by grouping statements into a callable unit.
Example: A function can be created to calculate the area of a pizza:
Use Pi, diameter, and radius (calculated as diameter divided by 2).
The area is computed using the formula: Area = Pi * r^2 (where r is the radius).
The function returns the computed area.
Function Call and Execution
When a function is called, the control transfers to the function body, executing statements until a return is reached.
The returned value replaces the function call in the original print statement.
Example Explanation:
The function call outputs a formatted statement about the pizza's area based on calculations.
Example Function: Squaring a Number
A simple function can compute the square of a given number:
Function definition includes an argument (e.g.,
num2square
).When called with a specific number (e.g., 7), it computes and returns the square (7 x 7 = 49).
Result (49) can be stored in a variable and printed.
More Complex Function: Calculating Pizza Volume
Function can also calculate pizza volume:
Requires parameters for pizza diameter and height.
Volume is computed as: Volume = Area * Height (area calculated as before).
Example call with diameter (12) and height (0.3), returning the calculated volume.
Reusability of Functions
Functions can be reused by calling them multiple times with different arguments; different computations can occur each time.
Example: Different diameter and height values for pizza volume calculation.
Void Functions
Functions that perform tasks without returning values are known as void functions.
Example: A function to print summaries of orders (e.g., order ID, items, price) without calculating any results.
Helps organize print statements and reduce redundancy.
Dynamic Typing and Polymorphism
Dynamic typing: Variable types are determined at runtime based on assigned values.
Example: An
add
function can add integers, doubles, or concatenate strings based on the provided arguments.
Polymorphism allows the same operator (e.g.,
+
) to perform different operations based on operand types.
Organizing Code with Functions
Functions improve code readability by grouping related statements, making maintenance easier.
Comparison of function-based code versus non-function-based code highlights the benefits of modularity and maintainability.
Modular Development
Breaking a program into independent modules enhances testing and efficiency during development.
Allows systematic integration of functions as programming progresses.
Mathematical Functions Example
Function to convert measurements (e.g., US sizes to centimeters).
Execution flow demonstrates the calling of functions and the return of computed values.
Example of modular functions where functions can call other functions for calculation and organization.
Incremental Development
Developing programs incrementally by writing, testing, and refining code in small parts.
Function stubs: defining function names without all logic to facilitate incremental testing.
Error Handling for Unimplemented Functions
Employing
raise NotImplementedError
ensures that if a function isn't implemented, the program stops execution and alerts the developer.This minimizes the risk of forgetting to complete function implementations.
Example Usage of Functions in Real Applications
Calculating eBay fees based on sale price using functions for different price brackets.
Functions can include conditional logic and loops to accomplish various tasks, similar to standard programming constructs.