2024

Introduction to Functions in R

  • 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.

Defining Functions

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.

Example: A Simple Function

greet <- function(name) {
  paste("Hello,", name, "!")
}
  • The greet function takes a single argument, name.

  • It returns a string that greets the user by name.

Calling Functions

greet <- function(name) {
  paste("Hello,", name, "!")
}

message <- greet("Alice")
print(message)  # Outputs: Hello, Alice !
## [1] "Hello, Alice !"
  • After defining a function, call it by using its name followed by parentheses () enclosing any arguments.

Parameters vs Arguments

  • Parameters are the placeholders used in the function’s definition.

  • Arguments are the actual values passed to the function when it is called.

Default Parameters

Functions in R can have default parameter values, making some arguments optional.

greet <- function(name="World") {
  paste("Hello,", name, "!")
}

Named Arguments

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 .

Question 1

How do you define a function in R that takes no arguments and prints “Hello, World!”?

  1. function() {print("Hello, World!")}
  2. myFunc <- print("Hello, World!")
  3. myFunc <- function() {print("Hello, World!")}
  4. function myFunc() {return("Hello, World!")}

Answer 1

Click here for the answer
  • The correct answer is 3. myFunc <- function() {print("Hello, World!")} defines a function that takes no arguments and prints “Hello, World!”.

Question 2

Given a function addNumbers that takes two arguments, x and y, and returns their sum, which of the following calls is correct?

  1. addNumbers(x, y)
  2. addNumbers <- (x + y)
  3. result <- addNumbers(2, 3)
  4. result = addNumbers(x: 2, y: 3)

Answer 2

Click here for the answer
  • The correct answer is 3. result <- addNumbers(2, 3) correctly calls the function addNumbers with 2 and 3 as arguments and stores the sum in result.

Question 3

What is the output of the following code?

add <- function(x, y) {
  return(x + y)
}

print(add(5, 3))
## [1] 8
  1. 8
  2. 53
  3. “x + y”
  4. Error

Answer 3

Click here for the answer
  • The correct answer is 1. 8 because 5 + 3 equals 8.

Question 4

How do you define a function with one compulsory parameter name and one optional parameter age with a default value of 30 in R?

  1. person <- function(name, age=30) {# do stuff}
  2. person <- function(name=30, age) {# do stuff}
  3. person <- function(name, age) {# do stuff}
  4. person <- function(name, optional age=30) {# do stuff}}

Answer 4

Click here for the answer
  • The correct answer is 1. 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.

Question 5

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")
}
  1. describe_pet("dog", "Rover")
  2. describe_pet(pet_name="Daisy", animal_type="cat")
  3. describe_pet(animal_type="hamster")
  4. describe_pet("Daisy", animal_type="dog")

Answer 5

Click here for the answer
  • The correct answer is 2. describe_pet(pet_name="Daisy", animal_type="cat"). It correctly provides both required arguments, using named arguments to specify them in any order.