Julius Schmid

Functions Are Objects

R functions are first-class objects (of the class “function”, of course), meaning that they can be used for the most part just like other objects. This is seen in the syntax of function creation:

g <- function(x) {
 return(x+1)
 }

Here, function() is a built-in R function whose job is to create functions! On the right-hand side, there are really two arguments to function(): The first is the formal argument list for the function we’re creating — here, just x — and the second is the body of that function — here, just the single statement return(x+1). That second argument must be of class “expression”. So, the point is that the right-hand side creates a function object, which is then assigned to g.

Note that for the first argument x, we could either have a single number (which g increases by 1) or a vector of numbers (where g increases each component separately by 1).

The two arguments to function() can later be accessed via the R functions formals() and body(), as follows:

Let us start with the function formals(). This function returns the first argument, i. e. all variables that are used in the definition of the function:

formals(g)
$x

For the function g this is just the one variable x.

Now, let us apply the body() function. It returns the concrete definition of the function, depending on all variables that are put in the first argument (which are just the variables that the formals() function returns).

body(g)
{
    return(x + 1)
}

The body() function prints everything within the brackets for the definition of the function. The important part here is “return(x + 1)” which exactly says that for an input x, we return x + 1.

Recall that when using R in interactive mode, simply typing the name of an object results in printing that object to the screen. Functions are no exception, since they are objects just like anything else. Hence, we just need to call g in order to see how the function is defined:

g
function(x) {
 return(x+1)
 }

Now, we create a function h and simulate a similar case scenario to the one shown above.

#Enter Answer Here

h <- function(y,z) {
  return(y^2 + 2 * y * z - z)
}

The function h now has two variables as the first input argument. Note that these two variables could be single numbers, as well as vectors.

Just as we did for g, we call the function formals() for h. This function returns the first argument, i. e. all variables that are used in the definition of the function:

formals(h)
$y


$z

We see that this time we do not only have one variable, but two! h is defined in dependence of y and z, which is exactly our output.

Let us again apply the body() function. Recall that it returns the concrete definition of the function, depending on all variables that are put in the first argument.

body(h)
{
    return(y^2 + 2 * y * z - z)
}

We obtain the concrete function definition which uses the variables y and z, which is just the result of y^2 + 2 * y * z - z.

Let us evaluate the function for the concrete values y = 1, z = 5.

h(1,5)
[1] 6

So h(1,5) = 6.

If you’re using a function that you wrote but which you’ve forgotten the details of. Printing out a function is also useful if you are not quite sure what an R library function does. By looking at the code, you may understand it better. For example, if you are not sure as to the exact behavior of the graphics function abline(), you could browse through its code to better understand how to use it.

LS0tCnRpdGxlOiAiRnVuY3Rpb25zIGluIFIiCm91dHB1dDogaHRtbF9ub3RlYm9vawotLS0KCkp1bGl1cyBTY2htaWQKCioqRnVuY3Rpb25zIEFyZSBPYmplY3RzKioKClIgZnVuY3Rpb25zIGFyZSBmaXJzdC1jbGFzcyBvYmplY3RzIChvZiB0aGUgY2xhc3MgImZ1bmN0aW9uIiwgb2YgY291cnNlKSwgbWVhbmluZyB0aGF0IHRoZXkgY2FuIGJlIHVzZWQgZm9yIHRoZSBtb3N0IHBhcnQganVzdCBsaWtlIG90aGVyIG9iamVjdHMuIFRoaXMgaXMgc2VlbiBpbiB0aGUgc3ludGF4IG9mIGZ1bmN0aW9uIGNyZWF0aW9uOgpgYGB7cn0KZyA8LSBmdW5jdGlvbih4KSB7CiByZXR1cm4oeCsxKQogfQpgYGAKCkhlcmUsIGZ1bmN0aW9uKCkgaXMgYSBidWlsdC1pbiBSIGZ1bmN0aW9uIHdob3NlIGpvYiBpcyB0byBjcmVhdGUgZnVuY3Rpb25zISBPbiB0aGUgcmlnaHQtaGFuZCBzaWRlLCB0aGVyZSBhcmUgcmVhbGx5IHR3byBhcmd1bWVudHMgdG8gZnVuY3Rpb24oKTogVGhlIGZpcnN0IGlzIHRoZSBmb3JtYWwgYXJndW1lbnQgbGlzdCBmb3IgdGhlIGZ1bmN0aW9uIHdl4oCZcmUgY3JlYXRpbmcg4oCUIGhlcmUsIGp1c3QgeCDigJQgYW5kIHRoZSBzZWNvbmQgaXMgdGhlIGJvZHkgb2YgdGhhdCBmdW5jdGlvbiDigJQgaGVyZSwganVzdCB0aGUgc2luZ2xlIHN0YXRlbWVudCByZXR1cm4oeCsxKS4gVGhhdCBzZWNvbmQgYXJndW1lbnQgbXVzdCBiZSBvZiBjbGFzcyAiZXhwcmVzc2lvbiIuIFNvLCB0aGUgcG9pbnQgaXMgdGhhdCB0aGUgcmlnaHQtaGFuZCBzaWRlIGNy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