0Pricing
R Academy · Lesson

Writing Functions in R

Understand how to create reusable code blocks using functions for cleaner and more modular scripts.

Writing Functions in R is a free R Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the R Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

Introduction to Writing Functions in R

Functions in R allow you to write reusable blocks of code, making your programs more efficient and organized.

Writing Functions in R — illustration 1

2

Defining a Function

Use the function keyword to define a function.

my_function <- function() {
  print('Hello, R!')
}
my_function()

3

Function Arguments

Functions can accept arguments to make them more flexible.

add_numbers <- function(a, b) {
  return(a + b)
}
add_numbers(3, 5)

4

Default Arguments

You can assign default values to function arguments.

multiply <- function(a, b = 2) {
  return(a * b)
}
multiply(5)

5

Returning Values

Functions return values using the return() statement.

square <- function(x) {
  return(x^2)
}
square(4)

6

Using Functions Inside Functions

Functions can call other functions for better modularity.

power <- function(x, y) {
  return(x^y)
}
calculate <- function(a) {
  return(power(a, 3))
}
calculate(2)

7

Anonymous Functions

Anonymous functions can be created using function() without a name.

(function(x) x + 2)(5)

8

9

Applying Functions to Vectors

You can apply functions to vectors using sapply() or lapply().

numbers <- c(1, 2, 3, 4)
sapply(numbers, square)

10

Summary

In this lesson, you learned:

  • How to create and use functions in R.
  • How to pass arguments and return values.
  • How to use functions inside other functions.
Writing Functions in R — illustration 10

Frequently asked questions

Is the “Writing Functions in R” lesson free?

Yes — the full text of “Writing Functions in R” is free to read here on the web, and the R Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the R Academy course, upgrade to CoddyKit PRO.

What will I learn in “Writing Functions in R”?

Understand how to create reusable code blocks using functions for cleaner and more modular scripts. You practise R Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start R Academy?

No prior experience is required. R Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Writing Functions in R” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this R Academy lesson?

Yes. Every R Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Writing Functions in R
  2. Control Flow Statements
  3. Debugging Techniques
← Back to R Academy