How do I write a function which has two parameters F3 and F4, and it will return the multiplication of F3 and F4. (using R )
f1 <- function(n1,n2){
sum(n1*n2)
}
f1(10,20)
is this correct?
f1 <- function(n1,n2){
sum(n1*n2)
}
f1(10,20)
As per the given question given that 2 parameter are there (It is not given what is the datatype of parameters ) so in that case above program works fine because it handles integer and float both.
But if it given that parameters should be of type integer or float only then we have to add check to see if they are as per mentioned in question.
For example below code ensure parameter to be integer only
isInt<- function(num){
test <- all.equal(num, as.integer(num), check.attributes = FALSE)
isTRUE(test)
}
f1 <- function(n1, n2) {
if (isInt(n1) && isInt(n2)) {
sum(n1*n2)
} else {
NA
}
}
f1(10,45.57)
Get Answers For Free
Most questions answered within 1 hours.