Mensagens de erro
Suas funções em C++ podem conter chamadas à função stop() para lançar uma exceção. Já deixamos parcialmente definida a função add_positive_numbers() para você. Complete essa função para lançar exceções.
Este exercicio faz parte do curso
Otimizando código R com Rcpp
Instruções do exercicio
- Se
xfor negativo, lance uma exceção com a mensagem: "x is negative". - Se
yfor negativo, lance uma exceção com a mensagem: "y is negative".
exercicio interativo prático
Tente este exercicio completando este código de exemplo.
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)