2.1 Functions
A central part of any programming language are functions. Functions take some input to generate output. In mathematics, denotes a function that takes an input argument , does something with it, and returns the result to us. Examples for well known functions are:
the logarithmic function:
the exponential function:
the square root:
the sinus function:
R has a large number of built-in functions that we can readily use. Just pick a number, say , and type into the console:
As you can see, functions in R consists of a name, e.g. log
, and parenthesis (i.e. round brackets), ()
. The arguments for the function are specified inside the parenthesis, e.g. log(5)
, where 5
is the argument to the function log
.
Some functions can take multiple arguments. An example is the logarithmic function log()
. If we only specify one argument, e.g. 5, it will compute the natural logarithm:
The natural logarithm uses base . The base to use can be provided to the function log
as a second argument. If we provide the base exp(1)
as a second argument, we get exactly the same result:
This second argument is said to have a default value that is used if we don’t specify anything else. However, we can also pick another logarithmic base, for example 10, and override the default argument:
In R, every argument has a name. We can either pass the arguments with or without name to the function:
When providing a name, we can even shuffle the arguments around:
which still gives the same result, because R knows which value belongs to base
and which one belongs to x
. If we don’t specify the argument names, R would interprets x = 10
and base = 5
,
which is obviously not the same.
2.1.1 Help pages
But how do you even know which function to use, what a function exactly does or which arguments it takes? Fortunately, there are many ways to get help!
For starters, there is an internal help-page for every function. For getting help on the log()
, just type into the console:
or the shortcut
Sometimes, you might not remember the exact name of the function. You can then browse through the help files using
or the shortcut
These built-in help functions are useful if you already know quite specifically what you’re looking for. However, you often have only a vague idea on how to solve your problem. In this case, internet search engines are your best friend. Knowing how to “google” is a very important skill in programming, and can be trained as every other skill. Try to describe your problem in a short, abstract way, and add the name of your programming language (in our case R) to the search. Or, if you have an error you don’t understand, paste the entire error message into the search field. On websites like stackoverflow.com, you practically always find someone who solved the problem you encountered.
2.1.2 Exercises: Functions
See Section 18.0.2 for solutions.
Search for the function
round()
by using the R help page. What is the purpose of this function? Which argument(s) does it take? Which argument(s) have a default value?Round ´1.23456789´ to zero, three and seven decimal places. Try calling the function with and without providing argument names.
Try to guess the result of
round(5.678, digits=1)
,round(x=5.678, digits=1)
,round(digits=1, x=5.678)
andround(1, 5.678)
!Find the function to calculate the binomial coefficient .