Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
def Keyword & Call Stack ArchitectureAs software projects grow from quick scripts to large-scale systems, writing hundreds of lines of linear, repetitive code becomes unmaintainable. Duplicated code introduces bugs: fixing an algorithm in one place requires tracking down every duplicate copy across your project.
To solve this, computer science relies on modular programming powered by functions. A function is a self-contained, reusable block of code designed to perform a specific task. In Python, functions embody the DRY (Don't Repeat Yourself) principle: you write logic once and execute it anywhere, as often as required.
How data and code flow step-by-step
def KeywordA Python function is declared using the def (short for define) keyword:
snake_case: lowercase words separated by underscores (e.g., calculate_gst_invoice, verify_aadhaar)._, never a digit.def, class, for, while).send_alert(), format_report().Defining a function merely registers its instructions in Python's memory; it does not execute them. To execute the function, you must call (or invoke) it using parentheses ():
Output:
greet_student without () references the function object itself rather than invoking it.
```python
print(greet_student) # Output: <function greet_student at 0x7f88a0> (NOT EXECUTED!)
greet_student("Ram") # Calls and executes the function!
```Professional Python code includes a docstring (documentation string) as the very first line inside the function body, enclosed in triple quotes """:
Output:
In Python, functions are first-class citizens: they can be assigned to variables, passed as arguments to other functions, and stored in data structures (like lists or dictionaries):
Output:
| Best Practice (DO) | Dangerous Anti-Pattern (DON'T) |
|---|---|
| DO adhere to single-responsibility principle: each function should do one thing well. | DON'T write "God functions" that do 10 unrelated things (e.g. read file, compute tax, format HTML, send email). |
DO use expressive snake_case function names (e.g. verify_pan_card). | DON'T name functions ambiguously like do_it(), temp(), or func1(). |
| DO include triple-quoted docstrings explaining parameters and expected outputs. | DON'T call a function before it is defined (Python reads top-to-bottom; calling before def raises NameError). |
DO use parentheses () to invoke functions. | DON'T write func when you mean func(). |
def keyword followed by the function name, parentheses (), and a colon :.name().snake_case conventions."""...""") placed on the first line inside the function documents its API contract and is accessible via help(fn) or fn.__doc__.return StatementIf a function computes a value but doesn't return it, Python returns None by default:
global KeywordReading a global variable is permitted, but assigning to it creates a local variable unless explicitly declared:
A. function B. fun C. def D. method
my_function instead of my_function())?A. The function executes with default parameters B. Python raises an ExecutionError C. It evaluates to a reference to the function object itself without executing it D. Python executes it in background daemon mode
A. Python uses hoisting to resolve it like JavaScript B. Python raises a NameError: name 'my_func' is not defined C. Python skips the call and continues D. Python issues an IndentationWarning
A. C-style comments /* ... */ B. Triple-quoted docstrings """...""" placed as the first statement inside the function body C. Semicolons followed by text D. Inline hashtags # doc:
A. Queue (FIFO) B. Call Stack (LIFO) C. Binary Search Tree D. Circular Buffer
Build a modular banking transaction management terminal for the State Bank of India (SBI).
Instead of writing monolithic code, break the ATM interface down into clean, modular, single-responsibility functions:
display_welcome_banner(branch_name): Renders an ASCII welcome header.check_balance(account_number, current_balance): Displays formatted account balance in INR.process_withdrawal(account_number, current_balance, withdrawal_amount): Checks if funds are sufficient. If valid, deducts amount and prints receipt; otherwise displays deficit advisory.process_deposit(account_number, current_balance, deposit_amount): Credits amount and prints updated ledger.In the next lesson, we will continue your Python learning journey with Arguments: Positional, Keyword (14: Functions).
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Project: Multiplication Table Generator | Arguments: Positional, Keyword |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.