Functions in R are objects that allow you to package a sequence of statements to perform a specific task, improving code reusability and organization.
They can take inputs, perform an operation, and return an output.
2024
Functions in R are objects that allow you to package a sequence of statements to perform a specific task, improving code reusability and organization.
They can take inputs, perform an operation, and return an output.
function_name <- function(parameters) { # Block of code return(result) }
A function is defined using the function
keyword.
The inputs are specified within the parentheses. They represent the inputs the function can accept.
The body of the function is enclosed in {}
.
The return()
statement is used to return the result of the function.
If return()
is not explicitly called, the last line is returned automatically.
greet <- function(name) { paste("Hello,", name, "!") }
The greet
function takes a single argument, name
.
It returns a string that greets the user by name.
greet <- function(name) { paste("Hello,", name, "!") } message <- greet("Alice") print(message) # Outputs: Hello, Alice !
## [1] "Hello, Alice !"
()
enclosing any arguments.Parameters are the placeholders used in the function’s definition.
Arguments are the actual values passed to the function when it is called.
Functions in R can have default parameter values, making some arguments optional.
greet <- function(name="World") { paste("Hello,", name, "!") }
Pass arguments to a function with or without specifying their names.
describe_pet <- function(animal_type, pet_name) { cat("I have a", animal_type, "named", pet_name, ".\n") } describe_pet(pet_name="Whiskers", animal_type="cat")
## I have a cat named Whiskers .
How do you define a function in R that takes no arguments and prints “Hello, World!”?
function() {print("Hello, World!")}
myFunc <- print("Hello, World!")
myFunc <- function() {print("Hello, World!")}
function myFunc() {return("Hello, World!")}
myFunc <- function() {print("Hello, World!")}
defines a function that takes no arguments and prints “Hello, World!”.Given a function addNumbers
that takes two arguments, x
and y
, and returns their sum, which of the following calls is correct?
addNumbers(x, y)
addNumbers <- (x + y)
result <- addNumbers(2, 3)
result = addNumbers(x: 2, y: 3)
result <- addNumbers(2, 3)
correctly calls the function addNumbers
with 2 and 3 as arguments and stores the sum in result
.What is the output of the following code?
add <- function(x, y) { return(x + y) } print(add(5, 3))
## [1] 8
How do you define a function with one compulsory parameter name
and one optional parameter age
with a default value of 30 in R?
person <- function(name, age=30) {# do stuff}
person <- function(name=30, age) {# do stuff}
person <- function(name, age) {# do stuff}
person <- function(name, optional age=30) {# do stuff}}
person <- function(name, age=30)
. This syntax correctly defines a function with a required name
parameter and an optional age
parameter that defaults to 30 if not provided.Given the describe_pet
function below, which of the following calls to describe_pet
is correct in R?
describe_pet <- function(animal_type, pet_name) { cat("I have a", animal_type, "named", pet_name, ".\n") }
describe_pet("dog", "Rover")
describe_pet(pet_name="Daisy", animal_type="cat")
describe_pet(animal_type="hamster")
describe_pet("Daisy", animal_type="dog")
describe_pet(pet_name="Daisy", animal_type="cat")
. It correctly provides both required arguments, using named arguments to specify them in any order.