In this article, we’ll learn what functions are and all the parts inside a function in R.
A function is a set of statements organized together to perform a specific task. R has a large number of in-built functions and the user can create their own functions.
Functions are used to logically break our code into simpler parts which become easy to maintain and understand. Functions allow you to automate common tasks in a more powerful and general way than copy-and-pasting
Good coding style is like a good punctuation. Functions are an asset to the whole program.
When you should use the function?
If you have a use case where you have to use a certain set of code again and again in the same program, then we use functions instead of writing the same repetitive block of code.
Now we will see the different parts of functions:
Usage
> func_name <- function (argument) {
+ statement
+ }
Func_name : This generally contains the name of the specific function. The name generally defines what the function is all about. If we have to write a function, say about the sum of two numbers, then we can give the function name to “sum”.
Function (argument): This function keyword tells us that the particular object is a function. Inside it we can give the arguments to the specific function, such as the name of the variable that is to be returned by this function.
Curly Braces: The curly braces define the starting of the main code for the specified function.
Statement: Statement is like the body of a function. Here we define what a function will perform after being called in the program. If it is a function that can sum up two numbers, then this statement contains the code required to do the sum “var1+var2”.
User Defined Functions (UDF)
Whether you need to accomplish a particular task and are not aware that a dedicated function or library exists already or because by the time you spend googling for some existing solution, you can have already come out with your own, you will find yourself at some time typing something like.
function.name <- function(arguments)
{
computations on the arguments
some other code
}
So, in most cases, a function has a name, some arguments used as input to the function, within the ()
following the keyword ‘function’; a body, which is the code within the curly braces {}
, where you carry out the computation; and can have one or more return values (the output). You define the function similarly to variables, by “assigning” the directive function(arguments) to the “variable” function.name
, followed by the rest.
Remember that there are also functions that don’t carry names; These are called “anonymous functions”.
Make sure that the name that you choose for the function is not an R reserved word. This means that you, for example, don’t want to pick the name of an existing function for your own UDF, because it can cause you a lot of headaches since R will not know whether you mean your recently defined UDF or the existing function that was loaded with one of the libraries.
One of the ways to avoid this is by using the help system: if you get some information back by entering {r eval=FALSE} ? OurFunctionName
, you know it is better not to use that name because it has already been taken.
> square <- function(n){
+ n*n
+ }
> square(10)
[1] 100
How Can You See Your R Function in RStudio?
When you develop your function, and you can see it in the RStudio environment. An easy way to visualize its code is to type its name without the parentheses ()
.
When you exit Rstudio without closing the function script file, and you saved your environment upon exit, you’ll find it again in your workspace among the script files that may have been there once you exited.
However, during the development of a slightly larger project, it is very likely that you wrote your function as an R script and saved it somewhere.
Nested Function Calls in R
The return
statement is not required in a function, but it is advisable to use it when the function performs several computations or when you want the value (and not the object that contains it!) to be accessible outside of the function body. As you have seen, the latter is not the default behavior.
Note that as the name says, it has the effect of ending the function execution and return control to the code which called it.
Now consider the arguments: these can be of any type and can have default values inside the function. The latter provides an output even when explicit values are not passed to it. Finally, you can call another function within a function.
Let’s see these points in detail through the following examples.
First you define a vector v
that you will use in the following:
# Create numeric vector `v` of 4 elements
> v <- c(1, 3, 0.2, 1.5, 1.7)
# Create a matrix `M`
> M <- cbind( c(0.2, 0.9, 1), c(1.0, 5.1, 1), c(6, 0.2, 1), c(2.0, 9, 1))
Then you show an example of a function calling the first function that you made above. Note that you can pass one argument only in the call, even if the function was defined with two arguments. This time you also use return()
> mysquare <- function(v){
+ u = c(0,0,0)
+ for(i in 1:length(v)){
+ u[i] = square(v[i]);
+ }
+ return(u)
+ }
> v <- c(1.00, 9.00, 0.04, 2.25, 2.89)
> mysquare(v)
[1] 1.00 9.00 0.04 2.25 2.89
Conclusion
Hence, you have seen that functions constitute the most essential programming construct in R, which is in fact, a functional language. You can develop functions on your own, which are called “User Defined Functions (UDF)”. The first example introduced us to the notion of functions and variable visibility or “scoping” across environments.
This brings the end of this Blog. We really appreciate your time.
Hope you liked it.
Do visit our page www.zigya.com/blog for more informative blogs on Data Science.
Keep Reading! Cheers!