(Sections 3.3 - 3.4)
Check to see that it works:
pow()
Returns a ValueIn R, a function returns the value of the final expression that it evaluates.
So let’s say it again:
In R,
a function returns
the value of the final expression
that it evaluates.
Observe that:
return()
The function return()
forces R to stop executing code in the body of the function:
return()
The following two functions do the same thing:
People with experience in other programming languages often like to end their functions with return()
, but in R it’s not necessary.
This function announces the result, but also returns the result.
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.
Recall g()
:
Rewrite g()
so that it cats()
s “Hello” to the Console but also returns val
.
The grumpy message won’t show.
cat()
the Message …print()
the Message[1] "OK, OK, I'm getting to it ... "
[1] 16
This is another way to force the side-effect.
print()
R will evaluate the following expression, and then print the result:
It’s as if we had written:
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!)
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?
So cat()
returns NULL
, which is R’s way of saying “nothing”. (But it is still a value.)
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:
\[\pi = \frac{4}{1} - \frac{4}{3} + \frac{4}{5} - \frac{4}{7} +\frac{4}{9} - \cdots\]
In mathematics courses we learn to write the sum like this:
\[\pi = \sum_{k=1}^{\infty} (-1)^{k+1}\frac{4}{2k-1}.\]
\[\pi = \sum_{k=1}^{\infty} (-1)^{k+1}\frac{4}{2k-1}.\]
\[\pi = \sum_{k=1}^{\infty} (-1)^{k+1}\frac{4}{2k-1}.\]
\[\pi = \sum_{k=1}^{\infty} (-1)^{k+1}\frac{4}{2k-1}.\]
The more terms you add up before stopping, the closer to \(\pi\) you will get.
\[\pi \approx \sum_{k=1}^{4} (-1)^{k+1}\frac{4}{2k-1}.\]
Not so great …
\[\pi \approx \sum_{k=1}^{8} (-1)^{k+1}\frac{4}{2k-1}.\]
Better …
\[\pi \approx \sum_{k=1}^{100} (-1)^{k+1}\frac{4}{2k-1}.\]
No way: too much typing!
To watch a function do its work step-by-step, run it in “debug”mode:
To set the function to run normally again, run:
n
We must provide an argument for the parameter n
, or there will be an error:
We can get around this by providing a default value for the parameter:
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.
Recall manyCat()
:
Rewrite it so that:
word
is “Kansas”, andn
is 3