Functions

In R, the last executed line of a function is automatically returned, otherwise use return() to be sure you know what the function is giving back to you.

increment_me(4), will return 5

increment_me(4, 6), will return 10

Sourcing

Once you have created a useful function, you can access it readily by storing it in its own script, with nothing but the code to define the function. Then the script can be called from any other script where you need the function:

source('scripts/fxnscript.R')

If configured properly, this will load the function into your environment, so that it is ready to call.

n.b. once you have a few of these scripts building up, you will be well on your way to creating your own packageā€¦

Do Exercise 3 - Create function.