Function in R
A function
is a set of codes which executes at a time and used to perform some actions
Function
Syntax
Function
Name
Function
Body
Return
Value
Input
Values
Name Input Body ReturnValue
demo.function
<- function(a) { b <- a^2
return(b) }
Function Code snippet
#Function without input/output
Definition
emptyFunction <- function()
{
print("hello")
}
Calling
emptyFunction()
#Function with input
demo.function <- function(a)
{
for(i
in 1:a)
{
b
<- i^2
print(b)
}
}
demo.function(5)
# Function with return
demoReturn <- function()
{
x
<- 1
z <-
2
return(x)
}
sampleData <- demoReturn()
sampleData
#
Function with input/output
demo.function <- function(a)
{
b
<- a^2
return(b)
}
sampleData <- demo.function(5)
sampleData
Recursive functions
recursive.factorial
<- function(x)
{
if (x == 0) { return (1)}
else
{return (x * recursive.factorial(x-1))}
}
recursive.factorial(5)
Function in R
Reviewed by Rupesh
on
11:47
Rating:
No comments: