Bird
Raised Fist0
R Programmingprogramming~5 mins

Function definition in R Programming - Cheat Sheet & Quick Revision

Choose your learning style10 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:
add <- function(x, y) {
  return(x + y)
}

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:
hello <- function() {
  print("Hello, world!")
}

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:
multiply(3, 4)

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

      Practice

      (1/5)
      1. What is the correct way to start defining a function named greet in R?
      easy
      A. greet <- function() { }
      B. function greet() { }
      C. def greet() { }
      D. function <- greet() { }

      Solution

      1. Step 1: Recognize R function syntax

        In R, functions are defined by assigning a function() to a name using <-.
      2. Step 2: Match the correct syntax

        greet <- function() { } uses the correct syntax: name <- function() { }.
      3. Final Answer:

        greet <- function() { } -> Option A
      4. Quick Check:

        Function definition = name <- function() [OK]
      Hint: Use name <- function() to define functions in R [OK]
      Common Mistakes:
      • Using def instead of function
      • Writing function name() without <-
      • Placing function keyword after name
      2. Which of the following is the correct syntax to define a function add that takes two arguments x and y in R?
      easy
      A. function add(x, y) { x + y }
      B. add(x, y) <- function { return x + y }
      C. add <- function(x, y) { return(x + y) }
      D. add <- function x, y { return(x + y) }

      Solution

      1. Step 1: Check function definition format

        In R, arguments go inside parentheses after function keyword, and the body is inside braces.
      2. Step 2: Verify return statement and assignment

        add <- function(x, y) { return(x + y) } correctly assigns function with arguments and uses return() properly.
      3. Final Answer:

        add <- function(x, y) { return(x + y) } -> Option C
      4. Quick Check:

        Arguments inside function() and return() used [OK]
      Hint: Put arguments inside function() parentheses [OK]
      Common Mistakes:
      • Placing arguments outside function()
      • Missing braces {} around function body
      • Incorrect assignment syntax
      3. What will be the output of the following R code?
      multiply <- function(a, b) {
        result <- a * b
        return(result)
      }
      multiply(3, 4)
      medium
      A. 12
      B. 7
      C. 34
      D. Error

      Solution

      1. Step 1: Understand function behavior

        The function multiply takes two numbers and returns their product.
      2. Step 2: Calculate the product of 3 and 4

        3 * 4 equals 12, so the function returns 12.
      3. Final Answer:

        12 -> Option A
      4. Quick Check:

        3 * 4 = 12 [OK]
      Hint: Multiply inputs and return result to get output [OK]
      Common Mistakes:
      • Adding instead of multiplying
      • Concatenating numbers as strings
      • Forgetting to return the result
      4. Identify the error in this R function definition:
      sum_values <- function(x, y) 
        total <- x + y
        return(total)
      }
      medium
      A. Function name is invalid
      B. Missing opening brace { after function()
      C. Incorrect use of return()
      D. Arguments should be inside square brackets []

      Solution

      1. Step 1: Check function body braces

        The function body must be enclosed in braces { } immediately after function() declaration.
      2. Step 2: Locate missing brace

        The code lacks an opening brace { after function(x, y), causing syntax error.
      3. Final Answer:

        Missing opening brace { after function() -> Option B
      4. Quick Check:

        Function body needs { } [OK]
      Hint: Always put { after function() to start body [OK]
      Common Mistakes:
      • Omitting braces around function body
      • Placing return outside function
      • Using wrong brackets for arguments
      5. You want to write a function filter_positive that takes a numeric vector and returns only the positive numbers. Which function definition correctly does this in R?
      hard
      A. filter_positive <- function(vec) { return(vec[vec == 0]) }
      B. filter_positive <- function(vec) { return(vec[vec < 0]) }
      C. filter_positive <- function(vec) { vec > 0 }
      D. filter_positive <- function(vec) { return(vec[vec > 0]) }

      Solution

      1. Step 1: Understand filtering in R

        To get positive numbers, select elements where vec > 0.
      2. Step 2: Check return statement

        filter_positive <- function(vec) { return(vec[vec > 0]) } returns vec elements where condition vec > 0 is TRUE, correctly filtering positives.
      3. Final Answer:

        filter_positive <- function(vec) { return(vec[vec > 0]) } -> Option D
      4. Quick Check:

        Filter with vec[vec > 0] returns positives [OK]
      Hint: Use vec[vec > 0] to filter positive numbers [OK]
      Common Mistakes:
      • Using wrong comparison operator
      • Not returning filtered vector
      • Returning logical vector instead of values
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard