0
0
R Programmingprogramming~5 mins

Function definition in R Programming - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Afunction
Bdef
Cfunc
Dfun
What does the following R code do?<br>
f <- function(x) { x * 2 }
APrints x times 2
BCalls a function named f
CCreates a variable x with value 2
DDefines a function that doubles x
How do you return a value from a function in R?
AUsing return()
BUsing output()
CUsing print()
DUsing send()
Which of these is a valid function call in R?
Amyfunc 5, 10
Bmyfunc(5, 10)
Ccall myfunc(5, 10)
Dmyfunc->(5,10)
Can an R function have no arguments?
AOnly if it returns NULL
BNo
CYes
DOnly if it is anonymous
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.