Error messages
Your C++ functions can contain calls to the stop() function to raise an exception. We've partially defined a function add_positive_numbers() for you. Complete this function to raise exceptions.
यह अभ्यास पाठ्यक्रम का हिस्सा है
Optimizing R Code with Rcpp
अभ्यास निर्देश
- If
xis negative, raise an exception with the message: "x is negative". - If
yis negative, raise an exception with the message: "y is negative".
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
cppFunction('
// adds x and y, but only if they are positive
int add_positive_numbers(int x, int y) {
// if x is negative, stop
if(___) stop("x is negative") ;
// if y is negative, stop
if(___) ___("y is negative") ;
return x + y ;
}
')
# Call the function with positive numbers
add_positive_numbers(2, 3)
# Call the function with a negative number
add_positive_numbers(-2, 3)