Topic 1 / 3📁 Unit Index
BP101T • Unit 4Topic 1 of 3⏱ 5 Min Read

1. Function Architecture, Parameter Passing & Scope in Lab Scripts

💡 Core Concept & Clinical Context:

A function is an encapsulated, reusable block of organized instructions that takes input arguments, performs a specific computational task, and returns an output. Functions follow the DRY (Don't Repeat Yourself) principle. In pharmaceutical laboratory systems, functions prevent error-prone code duplication across dilution, posology, and assay calculations. Variables created inside a function exist only within its local scope and cannot accidentally overwrite global variables.

Pharmaceutical Code & Calculation Examples:
1Comprehensive Stock Solution Dilution Calculator Function

Implementing $C_1V_1 = C_2V_2$ with positional, keyword, and default parameters.

example_1.py
def calculate_dilution_volume(target_conc, target_vol, stock_conc=100.0):
    """
    Calculates required stock volume using C1 * V1 = C2 * V2.
    Parameters:
      target_conc (float): Desired final concentration (% w/v or mg/mL)
      target_vol (float): Desired final volume (mL)
      stock_conc (float): Available stock concentration (default = 100.0% pure)
    Returns:
      float: Volume of stock solution required in mL
    """
    if stock_conc <= target_conc:
        raise ValueError("Stock concentration must be strictly greater than target concentration.")
    
    # V1 = (C2 * V2) / C1
    stock_vol_required = (target_conc * target_vol) / stock_conc
    water_to_add = target_vol - stock_vol_required
    return stock_vol_required, water_to_add

# Call using positional and keyword arguments
v_stock, v_water = calculate_dilution_volume(target_conc=2.5, target_vol=500.0, stock_conc=20.0)
print(f"Measure {v_stock:.2f} mL of stock and add {v_water:.2f} mL of purified water.")
📋Key Takeaways & Exam Highlights
  • `def` keyword declares a function; `return` sends values back to caller.
  • Docstrings (`"""..."""`) document function purpose, parameters, and return types.
  • Default arguments (`stock_conc=100.0`) provide fallback values if omitted by user.
  • Local variables: Defined inside function body, destroyed when function finishes execution.
🇮🇳 हिन्दी व्याख्या (1. Function Architecture, Parameter Passing & Scope in Lab Scripts)

फ़ंक्शन (Function) कोड का ऐसा रियूजेबल ब्लॉक है जिसे एक बार बनाकर बार-बार इस्तेमाल किया जाता है, जैसे C1V1 = C2V2 का डाइल्यूशन फॉर्मूला।

Need to see the complete unit overview, solved numericals, or exam checklist?Open Unit 4 Overview Hub →