Function
A function is a piece of code that takes input, performs a set of operations, and then returns the output. Functions in R are used to perform repetitive tasks, making it easier to write and manage your code.
Types of function in R Language
Built-in Function: R has a number of built-in functions that can be used to perform common tasks such as computing summary statistics, creating plots, and transforming data. Some of the most commonly used built-in functions are
mean
,sd
,sum
,plot
,summary
, etc.User-defined Function: R language allow us to write our own function.
Creating a Function
To create a function, use the function()
keyword:
Arguments
Information can be passed into functions as arguments.
Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.
The following example has a function with one argument (firstname
). When the function is called, we pass along a first name, which is used inside the function to print the full name:
Parameters or Arguments?
The words "parameter" and "argument" refer to the same concept: information that is input into a function.
From the function's point of view:
A parameter is denoted by a variable that is listed within the parentheses of a function's definition.
An argument refers to the specific value that is supplied to the function when it is executed.
Number of Arguments
By default, it is mandatory to invoke a function with a precise number of arguments. This means that if a function requires 2 arguments, it must be called with exactly 2 arguments, not more and not fewer.
Arguments
Information can be passed into functions as arguments.
Arguments are specified after the function name, inside the parentheses.
You can add as many arguments as you want, just separate them with a comma.
This is a simple R function called name_generator
that takes one argument, firstname
, and returns a string created by concatenating the firstname
argument with the string "Rahman".
Let's write a function that expects 3 arguments:
This R code defines a function called name_formatter
that takes three arguments: firstname
, lastname
, and nickname
. The function concatenates the three arguments to form a full name, with the nickname in parentheses.
The function uses the paste
function to concatenate strings. First, the function creates a new string formatted_nickname
by concatenating the string "(", nickname, ")", sep = ""
using the paste
function. This creates a string with the value of nickname
enclosed in parentheses.
Next, the function returns the result of another call to paste
which concatenates firstname
, formatted_nickname
, and lastname
with a space character as the separator.
Finally, the function is called with the arguments "Anisur", "Rahman", "Riyaz"
and the result is stored in the variable result
. The final output of the function will be:
If you try to call this function with more or less than 3 arguments, you will get an error.
Default Parameter Value
The below example demonstrates how to implement a default value for a parameter. In the event that the function is invoked without specifying a value, it will utilize the default argument.
Return Values
To let a function return a result, use the return()
function:
Lazy evaluations of Functions in R Programming Language
Lazy evaluation is a feature in R where a function's argument is only evaluated when it is needed to be used within the function. This feature is particularly useful for writing functions that accept large or potentially infinite arguments without actually evaluating them, thus reducing memory usage and speeding up execution.
To learn more about the lazy evaluation of function go to this link.
Source
Sources of contents of this page:
Last updated