创建一个 returns 特定值的函数

Creating a function which returns certain value

我有这个练习,我想不出来。

Create a function ans(x, y, c) which returns the value c*x^2*y, if x^2 <= y <= 1, and the value 0 otherwise. When you are ready input c.

我有 2 种不同的解决方案,但我不太明白如何正确组织函数。没有一个是正确的。

解决方案 1)

ans <- function(x,y,c){

if (x^2 <= y && <= 1)

return(c*x^2*y)

}

else{

return(0)

}

解决方案 2)

ans <- function(x,y,c){

 if (x^2 <= y & y <= 1)

 return(c*x^2*y)

 else if(x^2 <= 1){

 return(c*x^2*y)

 }

 else{}

 return(0)

 }

只需检查函数的格式。这里可能有您想要的东西:

ans <- function(x,y,c){
  if (x^2 <= y & y <= 1){
    return(c*x^2*y)
}else{return(0)}}