In this tutorial, we will learn about functions in R. We will cover the basics of creating, using, and understanding functions, including defining functions, passing arguments, and returning values.
A function in R is a block of code that performs a specific task. Functions help to break our program into smaller and modular chunks, making the code more organized and reusable.
Functions in R are defined using the function
keyword:
my_function <- function(arg1, arg2) {
# function body
result <- arg1 + arg2
return(result)
}
The above code defines a function named my_function
that takes two arguments and returns their sum.
In R, defining a function involves using the function keyword, and calling a function is done by writing the function name followed by parentheses containing any arguments.
For example,
greet
that takes one argument name
.paste
function to create a greeting message.greet
with a string argument and print the result.greet <- function(name) {
message <- paste('Hello,', name)
return(message)
}
print(greet('Arjun'))
[1] "Hello, Arjun"
In R, named arguments in function definitions allow you to specify default values, improving code readability and flexibility when calling the function.
For example,
multiply
that takes two arguments a
and b
.a
and b
.multiply
with named arguments and print the result.multiply <- function(a, b) {
result <- a * b
return(result)
}
print(multiply(a = 4, b = 5))
[1] 20
In R, default parameter values in function definitions let you set initial values for arguments, making them optional when calling the function.
For example,
power
that takes two arguments base
and exponent
, with exponent
having a default value of 2.^
operator.power
with and without the second argument and print the results.power <- function(base, exponent = 2) {
result <- base ^ exponent
return(result)
}
print(power(3))
print(power(3, 3))
[1] 9 [1] 27
In R, the return value from a function is the last evaluated expression, or you can explicitly specify it using the return() function.
For example,
sum_values
that takes two arguments x
and y
.x
and y
.sum_values
with two numbers and print the result.sum_values <- function(x, y) {
return(x + y)
}
print(sum_values(10, 15))
[1] 25
In R, nested functions are functions defined within other functions, allowing for encapsulation and the creation of helper functions that are only accessible within the enclosing function.
For example,
outer_function
that takes one argument outer_arg
.outer_function
, we define another function named inner_function
that takes one argument inner_arg
.inner_function
returns the product of outer_arg
and inner_arg
.outer_function
calls inner_function
with an argument and returns its result.outer_function
with a number and print the result.outer_function <- function(outer_arg) {
inner_function <- function(inner_arg) {
return(outer_arg * inner_arg)
}
return(inner_function(5))
}
print(outer_function(4))
[1] 20
is_even
that takes one argument num
.if
statement to check if the number is even.TRUE
; otherwise, it returns FALSE
.is_even
with a few different numbers and print the results.is_even <- function(num) {
if (num %% 2 == 0) {
return(TRUE)
} else {
return(FALSE)
}
}
print(is_even(4))
print(is_even(7))
[1] TRUE [1] FALSE
starts_with
that takes two arguments: str
and prefix
.substr
function to extract the beginning part of the string.if
statement to check if this extracted part matches the prefix
.TRUE
; otherwise, it returns FALSE
.starts_with
with different strings and prefixes and print the results.starts_with <- function(str, prefix) {
if (substr(str, 1, nchar(prefix)) == prefix) {
return(TRUE)
} else {
return(FALSE)
}
}
print(starts_with('Hello, world!', 'Hello'))
print(starts_with('Hello, world!', 'world'))
[1] TRUE [1] FALSE
factorial
that takes one argument n
.if
statement to check if n
is 0 or 1, in which case the factorial is 1.n
is greater than 1, we recursively call the factorial
function to compute the factorial.factorial
with a few different numbers and print the results.factorial <- function(n) {
if (n == 0 || n == 1) {
return(1)
} else {
return(n * factorial(n - 1))
}
}
print(factorial(5))
print(factorial(0))
[1] 120 [1] 1