Unit II · Basics of Python Programming for Pharmaceutical Sciences (BP101T) · As per PCI B.Pharmacy Syllabus, NEP 2020
As a pharmacy program grows — from checking one patient’s dosage to calculating totals across several drugs, printing labels, and validating prescriptions — repeating the same block of code by copy-pasting it everywhere quickly becomes unmanageable. Functions solve this by letting a programmer package a piece of logic once, under a name, and then reuse it as many times as needed. This post covers how to define a function, how to call it, how to pass data into it using arguments, and how to get results back out of it using return — the building blocks that make larger, well-organised pharmacy programs possible.

What a Function Is and Why It Matters
A function allows a program to be broken into small, self-contained parts, so the same block of code can be executed again and again without ever having to rewrite it. Functions matter for several concrete reasons that go beyond convenience:
- Reusability: the same piece of code can be reused many times instead of being retyped.
- Easy to understand: breaking a program into functions keeps it short, clean, and organised.
- Saves time: there is no need to type the same statements again for repeated tasks.
- Easy to fix errors: if a mistake is discovered, only the relevant function needs to be corrected, not the entire program.
Defining a Function
Defining a function means creating it. At this stage, the programmer decides three things: what the function should be named, what task or logic it should perform, and what input values it may need to accept. Crucially, a function does not execute anything at the time it is defined — it is only being set up for later use, much like the blueprint of a machine describes what the machine will do without actually running it.
def function_name():
# code block
The def keyword tells Python that a function is being defined. Here is a function created to display a greeting for pharmacy students:
def message():
print("Hello Pharmacy Students")
At this point the function has merely been created; nothing has been printed to the screen yet, because the function has not actually been executed.
Calling a Function
Calling a function means actually using or running a function that has already been defined. When a function call is made, Python jumps into the body of that function and executes every statement written inside it.
def message():
print("Hello Pharmacy Students")
message()
Output:
Hello Pharmacy Students
Calling message() causes Python to enter the function and run its print statement, producing the output on screen. Note the parentheses () after the function name — without them, Python would refer to the function itself as an object rather than actually running it.
Functions With Input: Parameters and Arguments
Parameters are values supplied to a function so it can work with different pieces of data each time it is called, instead of being restricted to one fixed value hardcoded inside it.
def dosage(mg):
print("Dose:", mg, "mg")
dosage(500)
dosage(250)
Calling dosage(500) and then dosage(250) prints Dose: 500 mg followed by Dose: 250 mg, because the value passed in on each call replaces the parameter mg inside the function body. This is the essence of what makes functions genuinely reusable: the same function body handles any dose value it is given.
Parameter vs. Argument — A Precise Distinction
These two terms are often used loosely, but BP101T expects a precise understanding of the difference:
- Parameter: a placeholder variable named in the function definition, such as
nameindef greet(name):. - Argument: the real value supplied when the function is actually called, such as
"Asha"ingreet("Asha").
def greet(name):
print("Hello", name)
greet("Asha")
In this call, "Asha" is the argument being passed in, name is the parameter that receives it inside the function, and the function then uses this received value to print Hello Asha.
Positional, Keyword, and Default Arguments
Python supports three distinct styles of passing arguments into a function, and each behaves differently.
Positional arguments — the order in which values are passed matters, because each value is matched to a parameter purely based on its position:
def add(a, b):
print(a + b)
add(2, 3)
Keyword arguments — values are passed by explicitly naming the parameter they belong to, so the order in which they are written no longer matters:
add(b=3, a=2)
Default arguments — a default argument is a value that a parameter automatically takes if the caller does not supply one explicitly:
def greet(name="Guest"):
print("Hello", name)
greet() # Hello Guest
greet("Ravi") # Hello Ravi
Calling greet() with no argument at all falls back on the default value "Guest" and prints Hello Guest, while calling greet("Ravi") overrides that default and prints Hello Ravi. Default arguments are especially useful in pharmacy programs where a parameter usually takes one common value (such as a standard dose or unit) but occasionally needs to be overridden.
Functions That Return a Value
A function can also send a result back to the part of the program that called it, using the return statement. This behaves like a machine that produces an output after it finishes processing its input, rather than merely displaying something on screen and then discarding it.
def add(a, b):
return a + b
result = add(5, 10)
print("Result:", result)
Output:
Result: 15
Here, add(5, 10) computes the sum and sends it back using return; that returned value is stored in the variable result, so printing result displays Result: 15. This is a critical difference from a function that only prints its output: a print statement inside a function displays a value once and it is gone, whereas a returned value can be stored, reused in further calculations, or passed into another function entirely.
print() vs. return: A Common Point of Confusion
def f1():
print(10)
def f2():
return 10
Calling f1() only displays the value 10 on the screen using print, without making it available for any later use in the program. Calling f2(), on the other hand, gives the value 10 back to the caller, so it can be stored in a variable, used in a calculation, or passed on to another function. As soon as a return statement executes inside a function, that function stops running immediately — no statement written after return inside the same function will ever run. If a function contains no return statement at all, Python automatically makes it return the special value None.
Complete Programming Examples
Example 1: Function Without a Parameter
This example displays a standard drug dosage using a function that takes no input at all.
# Function without parameter
def drug_info():
print("Drug: Paracetamol")
print("Dose: 500 mg")
drug_info()
Output:
Drug: Paracetamol
Dose: 500 mg
Example 2: Function With Parameters
This example displays different drug names along with their respective dosages by passing values into a function, so the same function serves any drug and dose combination.
# Function with parameters
def drug_info(drug, dose):
print("Drug Name:", drug)
print("Dosage:", dose, "mg")
drug_info("Amoxicillin", 250)
drug_info("Ibuprofen", 400)
Output:
Drug Name: Amoxicillin
Dosage: 250 mg
---------------------------
Drug Name: Ibuprofen
Dosage: 400 mg
---------------------------
Example 3: Function With a Return Value
This example calculates the total dosage of two drugs by returning the sum from a function, rather than just printing it directly.
# Function with return value
def total_dosage(d1, d2):
return d1 + d2
result = total_dosage(500, 250)
print("Total Dosage:", result, "mg")
Output:
Total Dosage: 750 mg
Example 4: Squaring a Number
A short combined example ties parameters and return values together in a single, self-contained function.
def square(x):
return x * x
num = square(4)
print(num)
Output:
16
The function multiplies its input by itself; calling square(4) returns 16, which is stored in num and then printed. Because total_dosage and square both return their results instead of printing directly, either one could be used inside a larger calculation — for instance, feeding the returned total dosage into a further function that checks it against a maximum safe limit.
Argument Passing Styles at a Glance
| Style | How Values Are Matched | Example Call |
|---|---|---|
| Positional | By order of appearance | add(2, 3) |
| Keyword | By explicit parameter name | add(b=3, a=2) |
| Default | Falls back to a preset value if omitted | greet() uses name="Guest" |
Frequently Asked Questions
Can a Python function return more than one value?
Yes. A single return statement can return multiple values separated by commas, such as return name, dose. Python packages these into a tuple automatically, and the caller can unpack them into separate variables in one line, for example drug_name, drug_dose = get_prescription(). This is commonly used when a function needs to hand back several related pieces of pharmacy data at once, such as a drug name together with its calculated dose.
What is the difference between a parameter with a default value and one without?
A parameter without a default value must be supplied by the caller every time the function is called, or Python raises a TypeError for a missing required argument. A parameter with a default value, such as name="Guest", becomes optional — the caller may omit it, in which case the default is used automatically. In a function’s parameter list, all parameters with default values must come after those without defaults.
Do variables created inside a function exist outside it?
No, not by default. A variable created inside a function body has what is called local scope — it exists only while that function is running and disappears once the function finishes, and it cannot be accessed directly from outside the function. This is precisely why the return statement matters: it is the standard, correct way to hand a value computed inside a function back out to the rest of the program.
Summary
Functions let a Python program organise repeated logic into named, reusable blocks instead of duplicating code everywhere it is needed. Defining a function with def only sets it up; nothing runs until the function is actually called. Parameters let a function accept different input values on each call, and Python supports passing those values positionally, by keyword, or by relying on a default when none is supplied. The return statement is what allows a function to hand a computed result back to the calling code so it can be stored and reused, in clear contrast to a function that merely print()s its output and discards it. Together, these ideas — definition, calling, parameters, arguments, and return values — form the foundation for writing organised, maintainable pharmaceutical calculation programs, such as dosage calculators and BMI tools, which build directly on these concepts.
References
- Pharmacy Council of India (PCI), B.Pharm Regulations, NEP 2020 — BP101T: Basics of Python Programming for Pharmaceutical Sciences Syllabus
- Python Software Foundation, official documentation — “Defining Functions”, The Python Tutorial
- Python Software Foundation, official documentation — “More on Defining Functions”, The Python Tutorial
