Function definitions, function calls and source files
This lesson is all about correctly writing a function - its definition, call and source file.
Function Definitions and Calls
Function definitions should first list arguments without default values, followed by those with default values.
In both function definitions and function calls, multiple arguments per line are allowed; line breaks are only allowed between assignments.
Function Documentation
Functions should contain a comments section immediately below the function definition line. These comments should consist of
- A one-sentence description of the function;
- A list of the function’s arguments, denoted by
Args:, with a description of each (including the data type), and, - A description of the return value, denoted by
Returns:.
The comments should be descriptive enough that a caller can use the function without reading any of the function’s code.
Have a look at the sample function below:
As an alternative, you can also use the comment(FunctionName) function in R to comment your function, so that when anyone else uses that function they can actually see the comment directly.
This will allow you to see the comments directly simply by calling str(FunctionName).