What are User Defined functions in R?

R provides built-in functions like print(), cat(), etc. but we can also create our own functions. These functions are called user-defined functions. A function is a block of code that performs a specific task. C allows you to define functions according to your need. These functions are known as user-defined functions. For example: Suppose, you need to create a circle and color it depending upon the radius and color.

Example:


# A simple R function to check
# whether x is even or odd
 
evenOdd = function(x){
  if(x %% 2 == 0)
    return("even")
  else
    return("odd")
}
 
print(evenOdd(4))
print(evenOdd(3))

Output:


[1] "even"
[1] "odd"