Basic Functions in R
In this lesson, you will learn what functions are and how they are used in R. Functions help you organize code and perform tasks more efficiently.
Almost everything you do in R is based on functions, from simple calculations to advanced data analysis.
What Is a Function?
A function is a reusable block of code that performs a specific task.
Instead of writing the same logic repeatedly, you can use a function to execute that logic whenever needed.
Built-in Functions in R
R comes with many built-in functions that are ready to use.
These functions help with calculations, data manipulation, and analysis.
sum(c(10, 20, 30))
mean(c(5, 10, 15))
max(c(2, 8, 4))
These functions return results immediately without any extra setup.
Using Function Arguments
Functions often accept inputs called arguments.
Arguments allow you to customize how a function behaves.
round(3.14159, digits = 2)
Here, the argument controls how many decimal places are shown.
Understanding Return Values
Most functions return a value after execution.
You can store the returned value in a variable for later use.
result <- sqrt(16)
print(result)
The function calculates the value and assigns it to a variable.
Creating Your Own Function
R allows you to create custom functions using the function() keyword.
Custom functions make your code cleaner and easier to maintain.
add_numbers <- function(a, b) {
sum <- a + b
return(sum)
}
This function adds two numbers and returns the result.
Calling a Custom Function
Once a function is defined, you can call it by its name.
This helps reuse logic multiple times without rewriting code.
add_numbers(10, 5)
The function executes and produces the output.
Functions Without Arguments
Some functions do not require any input values.
They simply perform a task and return a result.
greet <- function() {
print("Welcome to Dataplexa R Course")
}
greet()
This function prints a message when called.
Default Argument Values
Functions can have default values for arguments.
If no value is provided, the default is used automatically.
power <- function(x, y = 2) {
x ^ y
}
power(4)
power(4, 3)
This makes functions flexible and easier to use.
Why Functions Are Important
Functions help break complex problems into smaller, manageable parts.
They improve readability, reduce errors, and support reusable code.
📝 Practice Exercises
Exercise 1
Use a built-in function to find the minimum value in a vector.
Exercise 2
Create a function that multiplies two numbers.
Exercise 3
Write a function that calculates the square of a number.
Exercise 4
Create a function with a default argument value.
✅ Practice Answers
Answer 1
min(c(3, 7, 2, 9))
Answer 2
multiply <- function(a, b) {
a * b
}
multiply(4, 6)
Answer 3
square <- function(x) {
x ^ 2
}
square(5)
Answer 4
greet_user <- function(name = "Guest") {
paste("Hello", name)
}
greet_user()
greet_user("Alex")
What’s Next?
In the next lesson, you will practice basic programs using functions.
This will strengthen your understanding and prepare you for intermediate concepts.