Recall & Review
beginner
What is a function in R?
A function in R is a block of code designed to perform a specific task. It can take inputs, called arguments, and return an output.
Click to reveal answer
beginner
How do you define a simple function in R that adds two numbers?
You use the syntax: <br>
add <- function(x, y) {
return(x + y)
}<br>This creates a function named 'add' that returns the sum of x and y.Click to reveal answer
beginner
What does the 'return()' statement do inside an R function?
The 'return()' statement sends the result back to where the function was called. It ends the function and outputs the value inside return().
Click to reveal answer
beginner
Can an R function have no arguments? Give an example.
Yes, an R function can have no arguments. Example:<br>
hello <- function() {
print("Hello, world!")
}<br>This function prints a greeting when called.Click to reveal answer
beginner
How do you call a function named 'multiply' with arguments 3 and 4 in R?
You call it by writing:<br>
multiply(3, 4)<br>This runs the function with 3 and 4 as inputs.
Click to reveal answer
What keyword is used to define a function in R?
✗ Incorrect
In R, the keyword 'function' is used to define a function.
What does the following R code do?<br>
f <- function(x) { x * 2 }✗ Incorrect
The code defines a function 'f' that takes input x and returns x multiplied by 2.
How do you return a value from a function in R?
✗ Incorrect
The 'return()' function sends a value back from the function.
Which of these is a valid function call in R?
✗ Incorrect
Functions in R are called by writing the function name followed by parentheses with arguments inside.
Can an R function have no arguments?
✗ Incorrect
Functions in R can be defined without any arguments.
Explain how to define and call a function in R that takes two numbers and returns their sum.
Think about the syntax: name <- function(args) { ... } and how to use return()
You got /5 concepts.
Describe what happens when you call a function in R and how the return value is used.
Imagine the function as a machine that takes input and gives output.
You got /5 concepts.