R - Functions
A function is a block of statements which executes only when it is called somewhere in the program. Function provides re-usability of same code for different inputs, hence saves time and resources. R has many built-in functions and one of the common function is print(). A user can create their own function which is also termed as user-defined functions.
Create Function
In R, a function is defined using function's name followed by left assignment operator, function keyword, and parenthesis containing function's parameter(s) if it has any. At last, it contains block of statements which is also called body of the function.
Call Function
After defining the function, it can be called anywhere in the program with it's name followed by parenthesis containing function's parameter(s), if it has any.
Syntax
#Defining function function_name <- function(parameters) { statements } #Calling function function_name(parameters)
Example: A function with no parameter
In the example below, a function HelloWorld is created to print Hello World!. It requires no parameters to execute.
HelloWorld <- function() { print("Hello World!") } HelloWorld()
The output of the above code will be:
[1] "Hello World!"
Parameter
A parameter (or also known as argument) is a variable which is used to pass information inside a function. In above example, the function does not has any parameter. But a user can create a function with single or multiple parameters. Value of a parameter can be further used by the function to achieve desired result.
Example: A function with a parameter
In the example below, the function called MyPrint is created which takes one parameter to execute.
MyPrint <- function(name) { str = paste("Hello ", name) print(str) } MyPrint("John") MyPrint("Marry") MyPrint("Sam")
The output of the above code will be:
[1] "Hello John" [1] "Hello Marry" [1] "Hello Sam"
Default Parameter Value
Default value can be assigned to a parameter at the time of creating function. When the function is called without parameter then it uses the default value.
Example: A function with a default value parameter
In the example below, the function MyVector is created which takes one parameter to execute. If the parameter is not passed it takes the default value of the parameter.
MyVector <- function(a=7) { print(c(1:a)) } MyVector() MyVector(5) MyVector(3)
The output of the above code will be:
[1] 1 2 3 4 5 6 7 [1] 1 2 3 4 5 [1] 1 2 3
Function to Return Values
A function can be used to return values. See the example below:
Example: A function with a default value parameter
In the example below, the function called MyFunction is created which takes two parameter to execute: num and multiplier. The default value of multiplier parameter is 2. If the multiplier parameter is not passed, function will take its default value. The function returns the product of these two arguments.
MyFunction <- function(num, multiplier = 2) { x = num * multiplier return <- x } print(MyFunction(5, 3)) print(MyFunction(10)) print(MyFunction(25, 4))
The output of the above code will be:
[1] 15 [1] 20 [1] 100
Calling function by passing argument by name
Argument values can be passed in function with its name. In this way, the order of argument does not matter.
Example:
The example below describes how to call a function by passing argument with its name.
MyFunction <- function(x, y) { return <- (x/y) } print(MyFunction(2, 10)) print(MyFunction(y = 2, x = 10))
The output of the above code will be:
[1] 0.2 [1] 5