01c Custom Functions

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Custom Functions

Prof. Jacob Escobar (jacob.escobar@tec.mx)


Custom Functions: Syntax

custom_name <- function ( arglist ) {


code block
return( value )
}
Custom Functions: arguments list

The arglist can be empty or can have several arguments, which can have a
default value or not, as exemplified in the scenarios below:

custom_name <- function ( )


this function is called without arguments, but the () are still required
custom_ name <- function (w, x, y = 4, z = 3)
this function needs to be provided arguments w & x all the time,
while y and z are optional since they have default values
Custom Functions: return ( value )

Functions’ internal variables aren’t stored in the environment.

Ways in which functions return values or show outputs:


return( value ) --> usual way
print( text ) --> error messages, clarifications, troubleshooting
View ( variable ) --> visualization, notice the large-cap V (RStudio feature)
plot ( data ) --> visualization
Parenthesis Summary

Functions’ arguments are specified in ( )


Arrays’ elements are referenced in [ ]
Code Blocks are delimited by { }
Derechos Reservados 2018 Tecnológico de Monterrey
Prohibida la reproducción total o parcial de esta obra sin
expresa autorización del Tecnológico de Monterrey.

You might also like