2.1 Functions

A central part of any programming language are functions. Functions take some input to generate output. In mathematics, f(x) denotes a function f that takes an input argument x, does something with it, and returns the result to us. Examples for well known functions are:

  • the logarithmic function: log(x)

  • the exponential function: ex

  • the square root: x

  • the sinus function: sin(x)

R has a large number of built-in functions that we can readily use. Just pick a number, say 5, and type into the console:

> log(5)
[1] 1.609438
> exp(5)
[1] 148.4132
> sqrt(5)
[1] 2.236068
> sin(5)
[1] -0.9589243

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:

> log(5)
[1] 1.609438

The natural logarithm uses base e=2.718282.... 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:

> log(5, exp(1))
[1] 1.609438

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:

> log(5, 10)
[1] 0.69897

In R, every argument has a name. We can either pass the arguments with or without name to the function:

> log(5, 10)
[1] 0.69897
> log(5, base = 10)
[1] 0.69897
> log(x = 5, base = 10)
[1] 0.69897

When providing a name, we can even shuffle the arguments around:

> log(base = 10, x = 5)
[1] 0.69897

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,

> log(10, 5)
[1] 1.430677

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:

> help(log)

or the shortcut

> ?log

Sometimes, you might not remember the exact name of the function. You can then browse through the help files using

> help.search("logarithm")

or the shortcut

> ??logarithm

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.

  1. 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?

  2. Round ´1.23456789´ to zero, three and seven decimal places. Try calling the function with and without providing argument names.

  3. Try to guess the result of round(5.678, digits=1), round(x=5.678, digits=1), round(digits=1, x=5.678) and round(1, 5.678)!

  4. Find the function to calculate the binomial coefficient (53).