0

If we have something simple like

x <- c(9, 4)

mean(x)

Is the variable x itself considered the argument to the function mean()? What is the parameter in this context?

  • the parameter is the variable defined in the method/function signature, while the argument is the value/variable passed into the method/function when calling/running it. – Onyambu Feb 09 '22 at 20:07
  • In R everything is an object that you can see in your RStudio Global Environment pane. So functions act on objects passed as parameters. In your case x is a vector object. The Type will be numeric and the length will be 2. If you pass the wrong type to a function, it will return an error message. e.g. mean(9) returns 9, mean("9") returns an error. Here is a link that explains data types in R: https://r-coder.com/data-types-r/ Note that atomic types cannot be mixed in a atomic object. R will "coerce" them into a single type. – SteveM Feb 09 '22 at 20:17
  • @SteveM the underlyng question was parameter vs argument and not about data types. – Onyambu Feb 09 '22 at 20:22
  • @Onyambu so we would say that the argument is x because that is the variable being passed into the function mean() when we ran it? – Laura Edwards Feb 09 '22 at 20:34
  • indeed. The argument is `x` ie whatever you are passing into the function. Unfortunately in this example, the parameter is also `x`. NOT THE `x` defined above, but rather if you look at the signature of the mean function, you would see that it has `x`, or simply if you run `formals(mean)` the first parameter is `x` and then there is `...` which indicates that more parameters can be passed into the function – Onyambu Feb 09 '22 at 20:38

0 Answers0