In R, to create a function, you can use the function
keyword followed by the function's name and its arguments in parentheses. The code inside the function is enclosed in curly braces {}
. Here's an example of how to create a function that returns the sum of two numbers:
main.r45 chars4 lines
In this example, my_sum
is the name of the function, and x
and y
are the arguments that it expects. The function body simply returns the sum of x
and y
.
You can then call the function by passing it the required arguments:
main.r37 chars3 lines
This will output 5
, which is the result of calling the my_sum
function with arguments 2
and 3
.
gistlibby LogSnag