Reputation:
Once and for all I want to get the R terminology right. However, none of the books I was reading was of big help, and it seems to me the authors choose the names sometimes arbitrarily. So, my question is when exactly are the names "attribute", "parameter", and "argument" used?
From what I read and understood so far, a parameter is what a function can take as input. For example if I have a function that calculates the sum of two values, sum(value1, value2)
, 'value1' and 'value2' are the function's parameters.
If we are calling a function, we call the values passed to the function arguments. For the sum-function example, "23" and "48" would be the function arguments for:
sum(23,48).
So basically we call it parameter when we define a function, and we call it argument when we call the function (so the arguments are passed to the function's parameters)
But what about "attributes"? From what I understand, attributes are the equivalent of parameters in methods (and methods are functions of a class object)?
For example, if I would have something like:
heatmap(myData, Colv=NA, Rowv=NA)
... , would 'myData' be an argument or attribute? And what about Colv=NA
and Rowv=NA
? Isn't heatmap() a function and thus everything in the parentheses should be called arguments?
Upvotes: 4
Views: 3816
Reputation: 3
Attribute : Object's properties, e.g. Person has String fName, lName;
Parameter: appears in function/method definition e.g. public void setName(fName, lName)
Argument: value passed for a method/function's parameter when invoking/calling the method/function e.g. myPerson.setName("Michael", "Jackson")
Upvotes: 0
Reputation:
I am not sure how analogous R is to Python, but I think most of the terms should be consistent across different languages. From what I read and learned in the last couple of days, a parameter is basically what a function takes as its input when you define it:
my_function <- function (param1, param2){
...
}
and it is called argument if you are invoking a function with certain input values (that are passed to the function as parameters):
my_function(arg1, arg2)
Functions that are part of a class are called method. And an attribute can be either a value or method associated with a class object (or so-called instance)
So the question whether we call something argument or attribute depends on what we are calling: a function or a method. But I would say now argument is an appropriate term if we call the heatmap
function, for example:
heatmap(my_data)
Upvotes: 0
Reputation: 269885
Suppose we have:
f <- function(x) x + 1
comment(f) <- "my function"
f(3)
Arguments We distinguish between formal arguments and actual arguments. In the above x
is the formal argument to f
. The names of the formal arguments of f
are given by:
> names(formals(f))
[1] "x"
The actual arguments to a function vary from one call to another and in the above example there is a single actual argument 3
.
The function args
can be used to display the entire function signature of a function including the formal arguments and the default arguments and if you are debugging a function you can enter match.call()
to list the function signature with the actual arguments substituted.
Attributes The attributes of an R object are given by attributes(f)
like this:
> attributes(f)
$srcref
function(x) x + 1
$comment
[1] "my function"
There is one exception and that is that an object's class is also regarded as an attribute but is not given by the above but rather is given by class
:
> class(f)
[1] "function"
Parameters Sometimes function arguments are referred to as parameters or sometimes one refers to those arguments which are fixed as parameters but this tends to be related more to mathematics and statistics than R.
In statistical models the model is typically a function of the data and the model parameters often via the likelihood. For example, here:
> lm(demand ~ Time, BOD)
Call:
lm(formula = demand ~ Time, data = BOD)
Coefficients:
(Intercept) Time
8.521 1.721
the linear regression coefficients of Intercept and Time (viz. 8.521 and 1.721) are often referred to as model parameters.
As Dwin has already pointed out the various values influencing graphics in R are also termed parameters and can be displayed via:
> par()
and the corresponding concepts in other R graphics systems are often also referred to as parameters.
Upvotes: 11
Reputation: 263451
I suppose colloquial use of the term "attribute" might refer to several features of data objects, but there is a very specific meaning in R. An attribute is a value returned by either the functions: attributes
or attr
. These are critical to the language in that classes and names are stored as attributes. There two other assignment functions: attributes<-
and attr<-
that allow additional attributes to be assigned in support of class specific objectives.
?attributes
?attr
There is a par
function which sets graphical "parameters" that control the base graphics behavior. So that would be an R-specific use of parameter than might be slightly different than use of "argument" which is generally applied to the formal arguments to functions.
?par
The is a function args
which applied to a function name or an anonymous function will return its arguments (as a "closure" which gets printed on the console just as a user would type during a function definition) along with their default values. The function formals
will return the same "argument" information in the form of a list.
?args
?formals
I realize I am implicitly arguing with Matthew whose R skills are excellent. Contrary to him, I think that attributes
and arguments
have more specific meanings in the context of R and that careful authors will make an effort to keep their meanings separate. I would not have a problem understanding someone who uses parameter
as a synonym for argument if the context were clearly a discussion of applying a function, since that is the typical parlance in mathematics. I would agree with the conclusion of your last sentence. Those are 'arguments' and most emphatically not attributes. The attributes of an object returned by heatmap are:
> attributes(hv) #from first example in ?heatmap
#$names
# [1] "rowInd" "colInd" "Rowv" "Colv"
But only some of the arguments became attributes and then only after being assigned to the returned value during the function execution.
Upvotes: 5