Teaching functions in programming R – A function; Is a set of commands to perform a specific task
A function; Is a set of commands to perform a specific task; They are organizing together. The R programming language has a large number of built-in functions and the user can also create his own functions.
In the R programming language, a function is actually an object; In such a way that the interpreter R is able to control with the arguments that the function may take to perform the relevant actions; Be necessary; Transfer to function.
The function, in turn, performs its own tasks and transmits control to the interpreter as well as any results that may be stored in other objects.
Function definition
An R function is created using the function keyword . The syntax of the basic definition of the R function is as follows:
function_name <- function (arg_1, arg_2,…) {
Function body
}
Function components
The different parts of a function are as follows:
- Function Name – This is the real name of the function. It is stored in environment R as an object with this name.
- Arguments – An argument is actually a holder cop. When a function is called; You pass a value to an argument. Arguments are optional; That is, a function may not contain any arguments. Arguments can also have default values.
- Function body – The function body contains a set of expressions that define what the function does.
- Recovery value – the return value of a function; The last expression in the body of the function is to be evaluated.
The R programming language has many internal functions that can be called directly in the program; Without first defining them. We can also create our own functions, which are referred to as user-defined functions.
Internal functions
A simple example of internal functions () seq (), max (), sum and (…) paste and so on. They are written directly by the user; Is called. You can use the functions of R , where R functions that are used most often see.
# Create a sequence of numbers from 32 to 44.
print (seq (32,44))
# Find mean of numbers from 25 to 82.
print (mean (25:82))
# Find sum of numbers frm 41 to 68.
print (sum (41:68))
When we run the above code; The following results are obtain:
[1] 32 33 34 35 36 37 38 39 40 41 41 42 44 44
[1] 53.5
[1] 1526
User-defined functions
We can use user-defined functions in R. These functions depend on the user’s wishes and when they were created; They can be used as internal functions. The following is an example of how to create a function and use it.
# Create a function to print squares of numbers in sequence.
new.function <- function (a) {
for (i in 1: a) {
b <- i ^ 2
print (b)
}
}
Call a function
# Create a function to print squares of numbers in sequence.
new.function <- function (a) {
for (i in 1: a) {
b <- i ^ 2
print (b)
}
}
# Call the function new.function supplying 6 as an argument.
new.function (6)
When we run the above code; The following result is created:
[1] 1
[1] 4
[1] 9
[1] 16
[1] 25
[1] 36
Calling a function without arguments
# Create a function without an argument.
new.function <- function () {
for (i in 1: 5) {
print (i ^ 2)
}
}
# Call the function without supplying an argument.
new.function ()
When we execute the above code; The following result is obtain:
[1] 1
[1] 4
[1] 9
[1] 16
[1] 25
Calling a function with argument values (by position and name)
Arguments for calling a function can be found in the sequence in which the function is define; He expressed. They can also be express in a separate sequence, but must be assign as argument names.
# Create a function with arguments.
new.function <- function (a, b, c) {
result <- a * b + c
print (result)
}
# Call the function by position of arguments.
new.function (5,3,11)
# Call the function by names of the arguments.
new.function (a = 11, b = 5, c = 3)
When we run the above code; The following result is obtain:
[1] 26
[1] 58
Calling a function with a default argument
We can express the value of the arguments in the function definition and call the function without presenting any argument to achieve the pre-parsed results. But we can call some functions by presenting new values of arguments and achieve non-default results.
# Create a function with arguments.
new.function <- function (a = 3, b = 6) {
result <- a * b
print (result)
}
# Call the function without giving any argument.
new.function ()
# Call the function by giving new values of the argument.
new.function (9,5)
When we run the above code; The following result is obtain:
[1] 18
[1] 45
Function lazy evaluation
It is necessary that evaluating arguments for functions is lazy, so that evaluation is done only when requiring by the body of the function.
When we run high fashion; The following results are obtained:
[1] 36
[1] 6
Error in print (b): argument “b” is missing, with no default