Search⌘ K
AI Features

Solution Review: Creating Functions

Explore how to define functions in R by using arguments and conditional blocks. Understand the syntax of creating functions, practice making functions that perform tasks based on input, and improve your problem-solving abilities in R programming.

We'll cover the following...

Solution: Creating a Function

R
evenOdd <- function(testVariable)
{
if(testVariable %% 2 == 0)
{
cat("even")
} else
{
cat("odd")
}
}
# Driver Code
evenOdd(78)
cat("\n")
evenOdd(67)
cat("\n")
evenOdd(1)

Explanation

Create a function using the following syntax:

evenOdd <- function(testVariable)
{
   <Conditional Block>
}

The function takes an argument testVariable and accordingly performs some tasks on it.