Like any other object, functions must be defined before use. A simple example of this is the definition of the polynomial function .
In the 'console' of R studio you define the function as follows:
> f <- function(x) { x^3 + x - 1 }
> f(-2:2) # apply the function f to -2, -1, 0, 1, 2
[1] -11 -3 -1 1 9
In a script we usually divide the code over several lines (for readability) and then the definition above looks like as follows:
f <- function(x) {
value <- x^3 + x -1
return(value)
}
But we add one more example: look at the example and then read the explanation.
The function has the following power series expansion:
Now suppose that we give polynomial functions
) as function statement the polynomial that you would get when you cut off the power series after
terms. So:
In this way we get a series of functions
Since we do not want to define each function separately, we construct an R function
F
with two arguments, namely
and
, in the follwing script:
F <- function(x, n) {
sum_of_numbers <- 0
for (k in 0:n) {
sum_of_numbers <- sum_of_numbers + x^k/factorial(k)
}
return(sum_of_numbers)
}
print(F(1, 3)) # rough approximation of the number e
print(F(1, 5)) # good approximation of the number e
print(F(1, 10)) # better approximation of the number e
You get three number of output, viz. 2.66667, 2.716667, and 2.718282, when you run the script. The last instruction shows that the function for and returns a numerical result that approximates the exact value of , the base of the natural logarithm, to an accuracy of 6 decimal places. The deviation of the series approximation from the exact value is called the truncation error of the approximation.
On purpose, we have not defined the function shorter via
F <- function(n, x) { sum( x^(0:n) / factorial(0:n) ) }
because it allows us to discuss more aspects of a function definition and does not require the use of vectorisation.
The standard form in R of the definition of a function that returns a value is
name <- function(arguments) {
indented instructions
return(value)
}
The concrete example of the function F
illustrates that
- a function can have more than one argument: here
n
plays the role of a parameter and x
the 'common' variable of a mathematical function ;
- the function statement is delimited by indentation of instructions;
- variables can be defined within a function statement, which only have meaning within the function itself and not outside of it. We call the types of variables local variables (besides
sum_of_numbers
, k
is also a local variable in the iteration loop).
- the
return
function is used to return a value.
If you forget the return
statement, the function by default returns the value of the last statement, which can be the empty object NULL
. This can have its uses because an R function can also be intended solely for a side effect, such as printing text. However, this is often not the case, and forgetting the return
function is considered a mistake or a style error.
An alternative is to use an expression as the final instruction (as in our first example of a cubic polynomial function) where the calculated value of the expression is returned as the result. For simple mathematical functions, this way of defining functions is not wrong, but using the return
function is often clearer