In R, a function returns the value of the final expression that it evaluates.
This Is Important!!
So let’s say it again:
In R,
a function returns
the value of the final expression
that it evaluates.
Example
f <-function(x) {2*x +345"hello" x^2}
Try it:
Observe that:
2*4 + 3, 45 and “hello” vanish
only \(4^2 = 16\) is returned
Forcing Return with return()
The function return() forces R to stop executing code in the body of the function:
g <-function(x) { val <-3*x +7return(val)cat("Hello!")}g(1)
[1] 10
Optional Use of return()
The following two functions do the same thing:
f1 <-function(x) x^2f2 <-function(x) return(x^2)
People with experience in other programming languages often like to end their functions with return(), but in R it’s not necessary.
A Talky Function
talkySquare <-function(x) { result <- x^2cat("The square of ", x, " is: ", result, ".\n", sep ="") result # returns the square!}
This function announces the result, but also returns the result.
Side-Effects
The announcement of the square was a side-effect.
Side-Effect
Any result produced outside of the run-time environment of a function, other than the value that the function returns.
We’ll learn about run-time environments soon.
Practice
Recall g():
g <-function(x) { val <-3*x +7return(val)cat("Hello!")}
Rewrite g() so that it cats()s “Hello” to the Console but also returns val.
Printing
Grumpy Square
grumpySquare <-function(x) {"OK, OK, I'm getting to it ... " x^2}
The grumpy message won’t show.
Could cat() the Message …
grumpySquare <-function(x) {cat("OK, OK, I'm getting to it ... ") x^2}grumpySquare(4)
OK, OK, I'm getting to it ...
[1] 16
Could also print() the Message
grumpySquare <-function(x) {print("OK, OK, I'm getting to it ... ") x^2}grumpySquare(4)
[1] "OK, OK, I'm getting to it ... "
[1] 16
This is another way to force the side-effect.
Implicit Calls to print()
R will evaluate the following expression, and then print the result:
2+2
[1] 4
It’s as if we had written:
print(2+2)
[1] 4
In the body of a function, you need print() or cat() to make the result show in the Console. (Unless it’s the value of the final expression evaluated!)
What Does cat() Return?
cat() is used mainly for its side-effect: putting stuff out to the console.
But all functions return a value.
So what does cat() return?
Let’s Investigate!
Try this:
So cat() returns NULL, which is R’s way of saying “nothing”. (But it is still a value.)
Default Values
Madhava Series for \(\pi\)
Sometime around the year 1400CE, the South Indian mathematician Madhava discovered the following infinite-series formula for \(\pi\), the ratio of the circumference to the diameter of a circle:
# sum the first n terms of the Madhava seriesmadhavaPI <-function(n) {# make a vector of all of the k's we need: k <-1:n# make a vector of the first n terms of the sum: terms <- (-1)^(k+1)*4/(2*k-1)# return the sum of the terms:sum(terms)}
Try it Out
madhavaPI(n =1000)
[1] 3.140593
Study How the Function Works
To watch a function do its work step-by-step, run it in “debug”mode:
debug(madhavaPI)madhavaPI(n =5)
To set the function to run normally again, run:
undebug(madhavaPI)
Inconvenience: We Must Provide n
We must provide an argument for the parameter n, or there will be an error:
madhavaPI()
Error in madhavaPI(): argument "n" is missing, with no default
Default Values
We can get around this by providing a default value for the parameter:
madhavaPI <-function(n =10000) { k <-1:n terms <- (-1)^(k+1)*4/(2*k-1)sum(terms)}
Try it:
Definition
Default Value
A value for a parameter of a function that is provided when the function is defined.
This value will become the value assigned to the parameter when the function is called, unless the user explicitly assigns some other value as the argument.