Get startedGet started for free

Inline functions with cppFunction

1. Inline functions with cppFunction

So far, you have only used "evalCpp()" to evaluate short C++ expressions.

2. Limits of evalCpp

This limits you to very simple code, the next step is to define your own C++ functions. In the next chapter, you will learn how to write C++ functions in .cpp files, but for now, let's enjoy the comfort of the R console and define functions directly.

3. cppFunction

Similar to evalCpp(), "cppFunction()" takes a string of valid C++ code. But this time the string contains code that defines an entire C++ function. You will learn about C++ functions in a minute, so hang in there if you don't quite understand them just yet.

4. Internal things Rcpp does on your behalf

In between the C++ code you type and the R function you call from the R console,

5. Internal things Rcpp does on your behalf

there are many layers of engineering. The strength of Rcpp is that you never need to worry about any of that.

6. cppFunction

It all just works. You only have to write the C++ code of the function, and cppFunction() takes care of everything for you: Surround your code with boilerplate and save it to a temporary .cpp file. Compile the cpp file with the C++ compiler and load it directly in the global environment. You can then call your function just like any other R function.

7. Dynamic vs Static

In R, variables can first be defined as one type and then later be redefined as something else. This is because R is what is called a dynamically typed language, you can generally not infer the type of a variable before the runtime. On the other hand, C++ is a statically typed language, that means that the compiler must know the type of each object and that type can never change.

8. Type declaration of function arguments and return value

Consequently, it also means that functions must declare the type of each of their arguments

9. Type declaration of function arguments and return value

and the type of the objects they return.

10. Type declaration of function arguments and return value

Thus writing C++ code is more work, but having that information allows the compiler to optimize the code and it's one of the reasons C++ is much faster than R.

11. An addition example

In this example, we define a function which takes two doubles, as parameters, namely double x and double y. The type of the object the function returns is indicated just before the function's name - here "add()" is the name of the function and the keyword double before the name indicates that the function add() returns an object of type double. A function body consists of a sequence of statements, separated by semicolons. In this example, we create a local variable called res which is also a double, to host the result of x + y. That double is then returned with the return keyword.

12. Let's practice!

And now it's time to write your own C++ functions!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.