Argument
May 20, 2023
An argument is an input value that is passed into a function or method. In programming, functions and methods are used to group together a set of instructions that perform a specific task. These instructions can include calculations, comparisons, or any other type of operation that manipulates data. When a function or method is called, it can take one or more arguments as input, which can be used to customize the behavior of the function or method.
Arguments are an essential part of programming, as they allow functions and methods to be reused in different contexts. For example, a function that calculates the area of a rectangle could take the length and width of the rectangle as arguments, allowing it to be used for rectangles of different sizes. Similarly, a method that sorts a list of numbers could take the list as an argument, allowing it to be used for different lists.
Syntax
In most programming languages, arguments are passed to a function or method using parentheses. The arguments are separated by commas and enclosed in parentheses, like this:
def add_numbers(a, b):
return a + b
result = add_numbers(2, 3)
In this example, the add_numbers
function takes two arguments, a
and b
, which are added together and returned. The function is called on the last line with the arguments 2
and 3
, and the result is assigned to the result
variable.
Types of Arguments
There are several types of arguments that can be passed to a function or method, each with its own purpose and syntax.
Positional Arguments
The most common type of argument is the positional argument. Positional arguments are passed to a function or method based on their position in the argument list. In other words, the first argument is passed as the first parameter, the second argument is passed as the second parameter, and so on.
def add_numbers(a, b):
return a + b
result = add_numbers(2, 3)
In this example, 2
is passed as the first positional argument, which is assigned to the a
parameter, and 3
is passed as the second positional argument, which is assigned to the b
parameter.
Keyword Arguments
Keyword arguments are passed to a function or method using the name of the parameter as a keyword. This allows the arguments to be passed in any order, as long as the parameter names are used correctly.
def speak(name, message):
print(name + " says: " + message)
speak(name="Alice", message="Hello, World!")
In this example, the speak
function takes two keyword arguments, name
and message
. The arguments are passed using the parameter names as keywords, so the order of the arguments doesn’t matter.
Default Arguments
Default arguments are arguments that have a default value assigned to them. If no value is passed for a default argument, the default value is used instead.
def greet(name="World"):
print("Hello, " + name + "!")
greet()
greet("Alice")
In this example, the greet
function takes one default argument, name
, which defaults to "World"
. If no argument is passed, the function uses the default value of "World"
. If an argument is passed, the function uses the passed value instead.
Variable-Length Arguments
Variable-length arguments allow a function or method to take an arbitrary number of arguments, which can be accessed as a tuple or a list.
def sum_numbers(*numbers):
total = 0
for number in numbers:
total += number
return total
result = sum_numbers(1, 2, 3)
In this example, the sum_numbers
function takes a variable-length argument list, which is accessed using the *numbers
syntax. The function then iterates over the numbers
tuple and adds up all the values.
Keyword-Only Arguments
Keyword-only arguments are arguments that can only be passed using the keyword syntax. This allows a function or method to have required parameters that must be passed as keywords, while also allowing optional parameters to be passed positionally.
def speak(message, *, name="World"):
print(name + " says: " + message)
speak("Hello, World!") # Raises TypeError
speak("Hello, World!", name="Alice")
In this example, the speak
function takes one positional argument, message
, and one keyword-only argument, name
. The name
argument can only be passed using the name=
syntax, so calling the function without the keyword argument raises a TypeError
.