1. Writing functions in C++
We've seen previously that only functions decorated with the special comment "Rcpp::export" are exported to the R side. Only these functions can be called from R, and R has absolutely no knowledge of the other functions which are typically called internal, private or unexported.
2. Just don't export internal functions
Internal functions are a great way to modularizing your implementation by making a series of small functions (the internal functions) to perform subtasks, while only exposing the main entry point (the exported functions) to the user.
There is nothing you need to do to internal functions, because by definition if they are not preceded by the "Rcpp::export" comment, they stay hidden.
Here we have a function called "universal()" that takes no argument and always returns 42, the answer to the ultimate question of life, the universe and everything.
The function is implemented by calling an internal function called "twice()".
3. Just don't export internal functions
Only the universal function is exported, hence when the file is loaded into the R session with "sourceCpp()", the "universal()" function is the only function available from R.
In other words, there is no way to call the twice function from R as it has not been exported.
4. C++ comments
In addition to the double forward slash comments, that are similar to R comments, C++ has comments spanning multiple lines of code.
Multiple line comments start with a forward slash(/) and a star(*) and finish with a star(*) and a forward slash(/).
Everything in between is comments.
5. R code special comment
Rcpp defines another special kind of comment, typically added at the end of your C++ file to embed actual R code directly in your C++ file.
This is very useful when you develop C++ code and you want to test it right away, or when you want to include benchmarking code directly.
These comments start with a forward slash(/) followed by three stars(***), space and the capital letter "R". Everything between this and the end of the comment, which is represented by a star(*) and a forward slash(/), is interpreted as R code and is executed directly after the C++ file is compiled.
6. if and else
We will now move on to write control flow statements. Let's start with "if-else".
The C++ syntax for "if-else" is straightforward and very similar to its R equivalent.
First, the keyword "if", then a condition in parentheses, followed by the true branch inside curly braces. The code inside the braces only runs if the condition is met.
The other branch, known as the "else" branch, is executed if the condition in "if" is not true.
The else branch is optional.
7. if/else example
The else branch can, in turn, be made of another if branch when there are several cases to be considered.
This code defines a C++ function that prints a message depending on the value of the x argument. As you can see, the body of the function is a set of if-else if-else statements.
That's right, "else if" works the same in R and C++! We use the "Rprintf()" function to print the messages.
The function "info()" does not return anything as indicated by the void keyword, so there is no need for a return statement.
8. Let's practice!
Time to put this into practice.