Back to: Python for Medical Imaging
Functions become much more useful when we can provide them with information to work with. We use parameters and arguments to provide information to the functions.
In medical physics and medical imaging, researchers perform the same calculation using different measurements, patients, images, or acquisition parameters. Instead of creating a new function for every case, we can pass different values to the same function.
What Is a Parameter?
When a function is created, it defines a parameter as a variable inside its parentheses.
For example:
def display_modality(modality):
print("Imaging modality:", modality)
Here:
modality
is a parameter.
During the function call, a value fills the parameter’s placeholder.
What Is an Argument?
The function receives the actual value you supply when you call it.
display_modality("CT")
Here:
"CT"
is the argument.
The output is:
Imaging modality: CT
We can summarize the distinction as:
| Term | Meaning | Example |
|---|---|---|
| Parameter | Variable defined by the function | modality |
| Argument | Value passed to the function | "CT" |
A simple way to remember this is:
Function definition → Parameter
Function call → Argument
Functions with Multiple Parameters
A function can accept more than one parameter.
Suppose we want to calculate the approximate length represented by a CT image series.
def calculate_scan_length(slice_thickness, number_of_slices):
scan_length = slice_thickness * number_of_slices
return scan_length
The function has two parameters:
slice_thickness
number_of_slices
We can call the function with:
length = calculate_scan_length(1.25, 300)
print("Scan length:", length, "mm")
Output:
Scan length: 375.0 mm
In this function call:
1.25 → argument for slice_thickness
300 → argument for number_of_slices
Additionally, this is a simplified programming example. We determine geometric coverage from the appropriate DICOM spatial information rather than assuming that slice thickness × number of images always equals scan length.
Positional Arguments
In the previous example, Python assigns arguments according to their position.
calculate_scan_length(1.25, 300)
Python interprets this as:
slice_thickness = 1.25
number_of_slices = 300
These are called positional arguments.
Consider another medical physics example:
def calculate_total_dose(dose_per_fraction, number_of_fractions):
return dose_per_fraction * number_of_fractions
Call the function:
total_dose = calculate_total_dose(2.0, 25)
print("Total dose:", total_dose, "Gy")
Output:
Total dose: 50.0 Gy
The order matters when positional arguments are used.
Keyword Arguments
Python also allows us to specify the parameter name explicitly.
total_dose = calculate_total_dose(
dose_per_fraction=2.0,
number_of_fractions=25
)
These are called keyword arguments.
One advantage is that the function call becomes easier to understand.
For example:
calculate_scan_length(
slice_thickness=1.25,
number_of_slices=300
)
is immediately clear to someone reading the program.
Keyword arguments are particularly useful when a function has several parameters.
Default Parameters
Sometimes a parameter commonly uses the same value. Python allows us to specify a default value.
For example:
def display_scan_info(modality, contrast_used=False):
print("Modality:", modality)
print("Contrast used:", contrast_used)
If we call:
display_scan_info("CT")
Python uses the default:
Modality: CT
Contrast used: False
But we can override it:
display_scan_info("CT", contrast_used=True)
Output:
Modality: CT
Contrast used: True
Passing Variables as Arguments
Arguments do not have to be written directly as numbers or strings.
Variables can also be passed to functions.
slice_thickness = 0.625
number_of_slices = 400
length = calculate_scan_length(
slice_thickness,
number_of_slices
)
print("Scan length:", length, "mm")
This is particularly important in real programs because values will often come from:
- files,
- measurements,
- DICOM metadata,
- calculations,
- user input,
- or other functions.
Passing a List to a Function
A list can also be passed as an argument.
For example, suppose we have several HU measurements:
hu_values = [-102, -98, -105, -100, -95]
We can create:
def calculate_mean(values):
return sum(values) / len(values)
and pass the complete list:
mean_hu = calculate_mean(hu_values)
print("Mean HU:", mean_hu)
Output:
Mean HU: -100.0
Here:
values
is the parameter, while:
hu_values
is the argument.
This concept becomes especially important later when working with NumPy arrays and medical images.
Practical PyMedLab Example — Image Statistics
We can combine several parameters in one function:
def display_image_info(modality, rows, columns, slice_thickness):
print("Modality:", modality)
print("Image matrix:", rows, "x", columns)
print("Slice thickness:", slice_thickness, "mm")
Call the function:
display_image_info(
modality="CT",
rows=512,
columns=512,
slice_thickness=1.0
)
Output:
Modality: CT
Image matrix: 512 x 512
Slice thickness: 1.0 mm
Later in PyMedLab, these values could be obtained from a DICOM dataset rather than entered manually.
Common Beginner Mistakes
Forgetting an Argument
If a function requires two arguments:
def calculate_total_dose(dose, fractions):
return dose * fractions
this will cause an error:
calculate_total_dose(2.0)
Python is missing the value for fractions.
Correct:
calculate_total_dose(2.0, 25)
Supplying Arguments in the Wrong Order
Suppose we define:
Suppose we define:
def patient_info(patient_id, modality):
print(patient_id, modality)
This:
patient_info("CT", "P001")
does not represent what we intended.
Use:
patient_info("P001", "CT")
or, even more clearly:
patient_info(
patient_id="P001",
modality="CT"
)
Exercises
Exercise 1 — Basic
Create a function called display_patient() with two parameters:
patient_id
age
Call the function using "P001" and 54.
Exercise 2 — Medical Imaging
Create:
def calculate_scan_length(slice_thickness, number_of_slices):
Use the function to calculate the approximate length for:
Slice thickness: 0.625 mm
Number of slices: 400
Exercise 3 — Medical Physics
Create:
def calculate_total_dose(dose_per_fraction, fractions):
Calculate the total dose for:
Dose per fraction: 2 Gy
Fractions: 30
Exercise 4 — Keyword Arguments
Create:
def scan_information(modality, body_region, contrast_used):
Call it using keyword arguments for:
Modality: CT
Body region: Chest
Contrast used: True
Exercise 5 — Challenge
Create a function:
def calculate_image_statistics(values):
that receives a list of measurements and returns:
Mean
Minimum
Maximum
Test it with:
hu_values = [-105, -98, -101, -95, -103]
Key Takeaways
- Parameters are variables defined in a function.
- Arguments are the actual values passed to the function.
- Functions can accept one or multiple parameters.
- Positional arguments depend on their order.
- Keyword arguments explicitly identify the corresponding parameter.
- Default parameters provide values that can be used when an argument is not supplied.
- Lists and other Python objects can also be passed to functions.
- Parameters and arguments make functions reusable for different datasets and calculations.

Leave a Reply