R Custom Functions

Custom Functions

A function is a set of statements organized together to perform a specific task. R has a large number of built-in functions and also the user can create their own functions

R also has the ability to create custom functions. In order to create a custom function, we need to use the ‘function()’ command

In R, a function is an object, so the R interpreter is able to pass control to the function along with arguments that may be necessary for the function to accomplish the actions

The function in turn performs its task and returns control to the interpreter as well as any result which may be stored in other objects

R has many built-in functions which can be directly called in the program without defining them first

Examples of built-in functions are: sum(), mean(), print() etc…

Syntax:

Syntax
function_name <- function(arg_1, arg_2, ...) {

		function body

	}

function_name: the actual name of the function

arguments: An argument is a placeholder. When a function is invoked, a value is passed to the argument. Arguments are optional

function body: The function body contains a collection of statements that defines what the function does

return value: The return value of a function is the last expression in the function body to be evaluated

CODE/PROGRAM/EXAMPLE
#Custom Function with Argument

	myfunction <- function(x) {
	   y <- sum(x)
	   return(y)
	}

	#Executing myfunction

	marks <- c(99,86,65,87,67,59,98)
	myfunction(marks)
	[1] 561
#Custom_Functions_in_r_language #Custom_Functions_in_r_programming #sum()_function_in_r_language #mean()_function_in_r_language #print()_function_in_r_language

(New page will open, for Comment)

Not yet commented...