R Programming Series (Part 5): Functions In R
Beginner’s Guide
Functions are really important in a programming language. In this chapter, let’s explore all about functions!
First things first,
What is a Function?
A function is a group of code that, when called or invoked, carries out a certain purpose. R functions are crucial for structuring and modularizing code, which improves readability, reusability, and maintainability.
Numerous built-in functions in R are available for a variety of tasks, and you can also create your own custom functions to meet your specific needs.
The syntax of the function includes a function name followed by parentheses(). Inside the parentheses are the arguments or parameters that the function may accept.
General Structure of Function
Function_name <- function(argument1, argument2,…)
{
# body of the function
# It contains multiple statements
return(result) # optional: return statement to output the result
}
Now, let’s elaborate the function components,
- Function name: The name that recognizes the function. It should be descriptive and follow the rules of variables in R.