FUNCTIONS
R FUNCTIONS Functions are used to logically break our code into simpler parts which become easy to maintain and understand. Syntax for Writing Functions in R
func_name<-function(argument){
statement
}
Here, we can see that the reserved word function is used to declare a function in R.
The statements within the curly braces form the body of the function. These braces are optional if the body contains only a single expression.
Finally, this function object is given a name by assigning it to a variable, func_name.
Example of a Function
pow<-function(x,y){
#function to print x raised to the power y
result<-x^y
print(paste(x,"raised to the power",y,"is",result))
}
HOW TO CALL A FUNCTION?
pow(8,2)
## [1] "8 raised to the power 2 is 64"
pow(2,8)
## [1] "2 raised to the power 8 is 256"
Here, the arguments used in the function declaration (x and y) are called formal arguments and those used while calling the function are called actual arguments.
NAMED ARGUMENTS
In the above function calls, the argument matching of formal argument to the actual arguments takes place in positional order.
This means that, in the call pow(8,2), the formal arguments x and y are assigned 8 and 2 respectively.
We can also call the function using named arguments.
When calling a function in this way, the order of the actual arguments doesn’t matter. For example, all of the function calls given below are equivalent.
When calling a function in this way, the order of the actual arguments doesn’t matter. For example, all of the function calls given below are equivalent.
pow(8,2)
## [1] "8 raised to the power 2 is 64"
pow(x=8,y=2)
## [1] "8 raised to the power 2 is 64"
pow(y=2,x=8)
## [1] "8 raised to the power 2 is 64"
Furthermore, we can use named and unnamed arguments in a single call. In such case, all the named arguments are matched first and then the remaining unnamed arguments are matched in a positional order.
pow(x=8,2)
## [1] "8 raised to the power 2 is 64"
pow(2,x=8)
## [1] "8 raised to the power 2 is 64"
DEFAULT VALUES FOR ARGUMENTS
We can assign default values to arguments in a function in R.
This is done by providing an appropriate value to the formal argument in the function declaration.
Here is the above function with a default value for y.
pow <- function(x,y=2){
#function to print x raised to the power y
result<-x^y
print(paste(x,"raised to the power",y,"is",result))
}
The use of default value to an argument makes it optional when calling the function.
pow(3,2)
## [1] "3 raised to the power 2 is 9"
pow(3,1)
## [1] "3 raised to the power 1 is 3"
R RETURN VALUE FROM FUNCTION
Many a times, we will require our functions to do some processing and return back the result. This is accomplished with the return() function I
Syntax of return()
return(expression)
## function (...) .Primitive("expression")
The value returned from a function can be any valid object.
EXAMPLE: RETURN()
Let us look at an example which will return whether a given number is positive, negative or zero
check<-function(x){
if(x>0){
result<-"Positive"
}
else if(x<0){
result<-"Negative"
}
else{
result<-"Zero"
}
return(result)
}
Example:Taking Input from the User
my.name<-readline(prompt = "Enter name: ")
## Enter name:
my.age<-readline(prompt = "Enter age: ")
## Enter age:
#Convert Character into Integer
my.age<-as.integer(my.age)
Output
Enter name: mary Enter age: 17 “Hi,Mary next year you will be 18 years old.”
Here, we see that with the prompt argument we can choose to display an appropriate message for the user.
In the above example, we convert the Input age,which is a character vector into Integer Using a function as.integer().