What is an Argument in Programming?
In programming, an argument is a value or data item supplied to a function or subroutine when it is called. Arguments provide the actual data on which the function operates, while parameters are the variables defined in the function declaration that receive those values.
For instance, in the definition def add(x, y)
, the variables x
and y
are parameters; when the function is called as add(2, 3)
, the values 2
and 3
are arguments. This distinction helps clarify the roles each plays in function definitions and calls.
Parameters vs. Arguments
Many languages use the terms “parameters” and “arguments” interchangeably, but they refer to different things:
A function call must supply the same number of arguments as the parameters in its definition unless the parameters have default values.
How Arguments in Programming Work
When a function is defined, its parameters are listed between parentheses after the function name. Inside the function, those parameters act as local variables.
When the function is called, the runtime evaluates each argument expression and assigns the resulting values to the corresponding parameters.
If the function has multiple parameters, arguments are assigned in order unless keyword arguments are used. If fewer arguments are supplied than parameters, any parameter with a default value uses that default; otherwise, the call raises an error.
Types of Arguments in Programming
Different languages support various ways to pass arguments. Python provides a good illustration:
Positional Arguments
Positional arguments are passed in the order specified by the function definition. For example:
def greet(name, age):
print(f"Hello, {name}! You are {age} years old.")
greet("Ali", 20) # name='Ali', age=20
Here, "Ali"
and 20
are positional arguments that match the name
and age
parameters respectively. The number of arguments must equal the number of parameters.
Keyword Arguments
Arguments can also be passed using the key=value
syntax. This method specifies which parameter receives each value and allows arguments to be provided in any order:
greet(age=20, name="Ali")
Keyword arguments improve readability and help avoid errors caused by misordered positional arguments.
Default Arguments
A parameter can be assigned a default value. If the caller omits the corresponding argument, the function uses this default:
def greet(name, age=18):
print(f"Hello, {name}! You are {age}.")
greet("Sam") # age defaults to 18
greet("Zara", 25)
Variable-Length Arguments (*args and **kwargs)
Functions sometimes need to accept an arbitrary number of arguments. Python uses *args
to collect extra positional arguments into a tuple and **kwargs
to collect extra keyword arguments into a dictionary.
This allows for flexible function calls:
def print_info(*args, **kwargs):
print("Positional:", args)
print("Keyword:", kwargs)
print_info(1, 2, 3, name="Alex", role="Student")
In this call, (1, 2, 3)
are grouped into args
and {'name': 'Alex', 'role': 'Student'}
is grouped into kwargs
.
Argument Types Summary
Examples and Use Cases of Arguments
Arguments appear in every function call. Examples include:
- Simple function call:
print("Hello")
passes"Hello"
to theprint
function. - Mathematical operations:
max(5, 10)
supplies two numbers as arguments; the function returns the larger number. - Keyword argument:
range(start=1, stop=10, step=2)
uses keyword arguments to specify each parameter. - Variable-length arguments: A logging function might accept any number of messages via
*args
and optional tags via**kwargs
.
Beyond functions, the term argument applies to command-line arguments—values passed to a program at execution time.
These arguments provide input or configuration without hard-coding values. Many languages offer libraries (e.g., Python’s sys.argv
or Java’s main(String[] args)
) to access these values.
Related Concepts
- Parameters: Variables in a function definition that act as placeholders for incoming arguments.
- Functions and Methods: Reusable code blocks that may accept parameters and return values.
- Positional vs. Keyword Parameters: The order and naming of parameters in a function definition determine how arguments are matched.
- Call by Value/Reference: Some languages copy argument values into parameters (call by value), while others pass references (call by reference), which affects mutability and side effects.
Conclusion
In programming, arguments are the concrete values passed to functions during calls, whereas parameters are variables declared to receive those values.
Understanding this distinction clarifies how functions operate and helps avoid errors such as mismatched argument counts.
Languages support various ways to supply arguments—positional, keyword, default, and variable-length—allowing flexibility in how functions are called and defined.
Familiarity with these concepts equips developers to write robust, clear, and adaptable functions across programming languages.
« Back to Glossary Index