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. This is lesson 1 of 3. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the R Academy learning path, and your progress syncs across the web and the CoddyKit app. The R Academy course includes 3 lessons in total.
Introduction to Writing Functions in R
Functions in R allow you to write reusable blocks of code, making your programs more efficient and organized.

Defining a Function
Use the function keyword to define a function.
my_function <- function() {
print('Hello, R!')
}
my_function()Function Arguments
Functions can accept arguments to make them more flexible.
add_numbers <- function(a, b) {
return(a + b)
}
add_numbers(3, 5)Default Arguments
You can assign default values to function arguments.
multiply <- function(a, b = 2) {
return(a * b)
}
multiply(5)Returning Values
Functions return values using the return() statement.
square <- function(x) {
return(x^2)
}
square(4)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)Anonymous Functions
Anonymous functions can be created using function() without a name.
(function(x) x + 2)(5)Applying Functions to Vectors
You can apply functions to vectors using sapply() or lapply().
numbers <- c(1, 2, 3, 4)
sapply(numbers, square)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.

Learn R with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 43
- Lessons
- 159
Frequently Asked Questions
Is the “Writing Functions in R” lesson free?
Yes — any 3 lessons of the R Academy learning path, including “Writing Functions in R”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The R Academy course includes 3 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 3.
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
- Writing Functions in R
- Control Flow Statements
- Debugging Techniques