Hands-on Data Science
My learning DE

Lesson10 min

Calling functions

Functions are R's tools: they receive arguments, return a result and can be nested inside each other.

Topics:Programming with R

See first: Giving values a name

What you will learn

This lesson shows you how to call functions in R, and afterwards you can:

  • call a function such as sqrt() with arguments in round brackets and use its result further
  • give arguments by name, such as digits in round(), and explain what a default value is
  • read nested calls such as round(sqrt(10), digits = 1) from the inside out
  • open a function's help page with ? and make use of its examples
  • trace the message that R cannot find a function back to a typo or a package that is not loaded

Almost everything you do in R, you do with a function. A function has a name, round brackets follow it, and inside the brackets it receives its values, the arguments. It returns a result.

sqrt(16)

sqrt() takes the square root. You can show the result, store it under a name or use it straight away, like any other value.

Several arguments

Some functions need more than one value. round() rounds a number and learns from the second argument to how many digits:

round(3.14159, 2)

Arguments have names, and you may write them out. Then the order no longer matters, and the code explains itself:

round(3.14159, digits = 2)

If you leave out an argument, the function uses its default. For round() that is zero decimal places, so round(3.14159) gives 3.

Nesting functions

The result of one function can be the argument of the next. R reads such calls from the inside out:

round(sqrt(10), digits = 1)

First sqrt() takes the square root of 10, then round() rounds the result to one digit. More than two or three levels quickly get confusing; that is what the pipe is for later.

Finding help

Every function has a help page with all its arguments and examples. In R on your own computer you open it with a question mark in front of the name:

?round

A web search for "r round" works just as well. Help pages are written tersely, but the examples at the very bottom are almost always worth it.

When R does not know a function

If you mistype the name, R reports that it cannot find the function:

rund(3.14159)

The same message appears when the function comes from a package that is not loaded; the lesson Packages is about that.

Key points

  • A function receives arguments in round brackets and returns a result.
  • Arguments can be given by name; missing ones get a default.
  • R reads nested calls from the inside out.
Back

Builds on this

Details
Level
Beginner
Modules
Empirical Research
In learning paths
Getting started with R
Code
4602
Updated
1 November 2026
Status
published

    A four-digit code takes you straight to the item.