A user-defined function is designed according the needs of the user. In R, a “function” keyword is used to create a user-defined function.The basic structure for declaring a function is
# function_name<-function(argument1,argument2)
# {
# function body
# return
# }
Now, Let’s create a function to add two numbers
f<-function(a,b)
{
d=a+b
}
Next we will call this function f.
res<-f(2,3)
res
## [1] 5
While writing the code, one can define a value for one or more arguments of a function. So, while invoking the function if user does not provide the value for those arguments, the default values may be applied.
g<-function(x,y=5)
{
z=x+y
}
In the above code, There are two arguments “x” and “y”. And, we have passed default value for “y” as 5.
Now,Make a function call with 2 arguments.
res1<-g(3,4)
res1
## [1] 7
Now, make a function call with only 1 argument
res1<-g(3) # here we have passed only argument. "y" value is not given by user
res1
## [1] 8
Unlike other programming languages where return() function has to be called explicitly, R follows a different rule.If there is no explicit return value in a function, then it would simply return the last evaluated expression.
h<-function(x,y)
{
a<-x+y
b<-x-y
c<-x*y
}
Now let’s make a function call now
d<-h(5,6)
d
## [1] 30
In the above code, only last expression is evaluated.
let’s consider above example.
h<-function(x,y)
{
a<-x+y
b<-x-y
c<-x*y
}
r<-h(2,3)
r
## [1] 6
Now, consider the below code.
h<-function(x,y)
{
a<-x+y
b<-x-y
c<-x*y
return(a)
}
r1<-h(2,3)
r1
## [1] 5
There are situations where we need to return complex objects(not simple number or strings) . These can be added to a list and can be returned back to calling function.
In the below example, we are going to return both matrix and a vector through return.
res2<-function()
{
v<-c(1,2,5,3,8)
m<-matrix(1:8,ncol=4)
v1<-mean(v)
m1<-min(m)
L<-list(vec=v1,mat=m1)
return(L)
}
res2()
## $vec
## [1] 3.8
##
## $mat
## [1] 1
Let’s create a vector
v<-c(1,3,7,2,6)
At any point of time, if we want to see the content of v, we simply type “v”and the contents are displayed.
v
## [1] 1 3 7 2 6
In the same way, R treats functions as objects. So, if we type the function name, the code inside the function is displayed.
res2
## function()
## {
## v<-c(1,2,5,3,8)
## m<-matrix(1:8,ncol=4)
## v1<-mean(v)
## m1<-min(m)
## L<-list(vec=v1,mat=m1)
## return(L)
## }
And functions can be assigned to another functions too. As like assigning a value to a variable objects.
hi<-res2
hi
## function()
## {
## v<-c(1,2,5,3,8)
## m<-matrix(1:8,ncol=4)
## v1<-mean(v)
## m1<-min(m)
## L<-list(vec=v1,mat=m1)
## return(L)
## }
Here, The right-handside creates a function object,which is then assignes to a variable.
R does not allow usage of pointers as a result of which sometimes you may face restrictions in changing the values directly.
flower<-c("rose","lily","Tulips","lotus")
sort(flower)
## [1] "lily" "lotus" "rose" "Tulips"
flower[1]
## [1] "rose"
Here, the function to sort could not change the flower object. If we want to change x, then we should reassign it.
flower<-sort(flower)
flower
## [1] "lily" "lotus" "rose" "Tulips"
In case of python, the actual object is modified.
#flower=["rose","lily","Tulips","lotus"]
#flower.sort()
#flower
[‘Tulips’, ‘lily’, ‘lotus’, ‘rose’]
#flower[0]
‘Tulips’