← Back to R Course | Chapter 5: Functions | Lesson 1 of 7

Function Basics

In this page:

  1. Function Basics

Function Basics

A function in R is created with function(args) { body } and assigned to a name like any other value, since functions are first-class objects in R. Calling it looks just like calling any built-in function. Breaking logic into functions is the primary way to organize and reuse R code.

Note: Functions are ordinary values in R -- you can pass them as arguments to other functions, just like numbers or strings.

Example: Function Basics

markup
greet <- function() {
  cat("Hello from a function!\n")
}

greet()
greet()
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.