1. Function Architecture, Parameter Passing & Scope in Lab Scripts
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.
Implementing $C_1V_1 = C_2V_2$ with positional, keyword, and default parameters.
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.")- `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.
फ़ंक्शन (Function) कोड का ऐसा रियूजेबल ब्लॉक है जिसे एक बार बनाकर बार-बार इस्तेमाल किया जाता है, जैसे C1V1 = C2V2 का डाइल्यूशन फॉर्मूला।