Functions

A function is a reusable block of R code that takes inputs (arguments), does some work, and returns a result — letting you name and repeat logic instead of copying it.

Learn Functions in our free R course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free R course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

By the end of this lesson you'll define your own functions, give arguments default values, return values (automatically or with return()), and understand how local scope keeps functions self-contained.

What You'll Learn in This Lesson

1️⃣ Defining a Function

You create a function with the function keyword, list its arguments in parentheses, and put the body in braces. The value of the last expression is returned automatically — no return needed.

2️⃣ Defaults, Named Arguments, and return()

Give an argument a default with name = value to make it optional. Callers can pass arguments by position or by name. Use return() to exit early — perfect for guard clauses.

3️⃣ Scope: What Stays Inside

Variables created inside a function are local — they vanish when the function returns and don't pollute your workspace. A function can read outer (global) variables, but to get a value out , you must return it. Functions are also first-class values: you can store them in lists and pass them around.

Your turn. Fill in the # TODO blank, run it, and compare with the expected output.

Write it from the outline, run it, and check it against the example output. This combines arguments, defaults, if/else , and return() .

📋 Quick Reference — Functions

Practice quiz

Which keyword defines a function in R?

  • def
  • func
  • function
  • lambda

Answer: function. You create a function with the function keyword: f <- function(x) { ... }.

If you write no return(), what value does an R function return?

  • NULL always
  • The value of the last expression evaluated
  • The first argument
  • An error

Answer: The value of the last expression evaluated. R automatically returns the value of the final expression in the body.

What is return() mainly used for?

  • To define defaults
  • To print output
  • To create a loop
  • To exit the function early

Answer: To exit the function early. return() lets a function exit early, which is handy for guard clauses.

How do you give an argument a default value?

  • function(x, n = 2)
  • function(x, n := 2)
  • function(x, default n 2)
  • function(x, n -> 2)

Answer: function(x, n = 2). Write name = value in the argument list, e.g. function(x, n = 2).

What does power(exponent = 3, base = 2) demonstrate?

  • A syntax error
  • Passing arguments by name, order-independent
  • Returning two values
  • Recursion

Answer: Passing arguments by name, order-independent. Named arguments can be given in any order regardless of definition order.

A variable created inside a function with <- is:

  • Global to the whole session
  • Stored in a file
  • Shared across all functions
  • Local and gone when the function returns

Answer: Local and gone when the function returns. Local variables live in the function's scope and disappear when it returns.

Can a function READ a global variable it does not define locally?

  • Yes, via lexical scoping
  • No, never
  • Only with <<-
  • Only if it is a number

Answer: Yes, via lexical scoping. A function can read outer/global variables; to send a value out, it must return it.

For a one-line body, which definition is valid?

  • function x: x ^ 2
  • square <- function(x) x ^ 2
  • function(x) -> x ^ 2
  • def square(x) x ^ 2

Answer: square <- function(x) x ^ 2. A single-expression body can skip the braces: function(x) x ^ 2.

How can a function return SEVERAL values at once?

  • By returning a list, e.g. list(mean = m, sd = s)
  • It cannot return more than one value
  • By printing them
  • By using two return() lines

Answer: By returning a list, e.g. list(mean = m, sd = s). Return a list (or vector) to hand back multiple named results.

What does "argument 'x' is missing, with no default" mean?

  • x was spelled wrong
  • x must be a number
  • A required argument x was not supplied and has no default
  • The function has too many arguments

Answer: A required argument x was not supplied and has no default. You called the function without a required argument; pass it or give it a default.