英文:
My subprogram does not work in C language
问题
I wrote a subprogram in C with Xcode that calculates a partial function but it does not work.
我在Xcode中编写了一个用于计算部分函数的子程序,但它不起作用。
I know there is a a lot wrong with " return " statements but i couldn't find a way to make it better.
我知道"return"语句存在很多问题,但我找不到改进的方法。
I made "return (result * 1 )" and "return(result = result)" for return, took every function between return parentheses like " return(result = pow(x,5) + pow(y,3)); " but none of them worked.
我尝试过"return (result * 1 )"和"return(result = result)"来返回,还尝试了像" return(result = pow(x,5) + pow(y,3)); "这样的返回语句,但都没有成功。
Can you please show me how to make it right and explain why?
请问您能否向我展示如何正确编写以及解释原因?
英文:
I wrote a subprogram in C with Xcode that calculates a partial function but it does not work.
I know there is a a lot wrong with " return " statements but i couldn't find a way to make it better.
I made "return (result * 1 )" and "return(result = result)" for return, took every function between return parentheses like " return(result = pow(x,5) + pow(y,3)); " but none of them worked.
Can you please show me how to make it right and explain why?
#include <stdio.h>
#include <math.h>
double
funmath(int x, int y)
{
double result;
if( x >= 50 )
result = pow(x,5) + pow(y,3);
else if( x >= 40 )
result = 5 * x + y + 9;
else if( x >= 0 )
result = x / 5 + 7 * y;
else
return(result);
}
答案1
得分: 3
以下是已翻译好的内容:
你要求一个解决方案和解释。
解决方案
#include <stdio.h>
#include <math.h>
double
funmath(int x, int y)
{
double result = -42; //如果你愿意的话,可以使用其他默认值
if( x >= 50 )
result = pow(x,5) + pow(y,3);
else if( x >= 40 )
result = 5 * x + y + 9;
else if( x >= 0 )
result = x / 5 + 7 * y;
return(result);
}
解释
你的 double
函数在任何情况下都需要 return
一些东西。你的函数有以下情况:
if (x >= 50)
: 你将result
设置为某个值,但没有return
任何东西else if( x >= 40 )
: 你将result
设置为某个值,但没有return
任何东西else if( x >= 0 )
: 你将result
设置为某个值,但没有return
任何东西else
: 你没有初始化result
,但你返回它
不要忘记 else if
意味着在当前条件之前的 if
和较早的 else if
条件都不成立,但当前 else if
的条件成立,不要忘记 else
意味着“以上条件都不成立”。由于你在 else
内部有 return
,所以在前三种情况下你都没有 return
任何东西,因此,你违反了语言的最佳实践。这就是为什么解决方案是无论如何都要 return
一些东西。
英文:
You asked for a solution and an explanation.
The solution
#include <stdio.h>
#include <math.h>
double
funmath(int x, int y)
{
double result = -42; //You can use some other default value instead if you prefer
if( x >= 50 )
result = pow(x,5) + pow(y,3);
else if( x >= 40 )
result = 5 * x + y + 9;
else if( x >= 0 )
result = x / 5 + 7 * y;
return(result);
}
Explanation
Your double
function needs to return
something in any case. Your function has these cases:
if (x >= 50)
: you setresult
to some value, but you do notreturn
anythingelse if( x >= 40 )
: you setresult
to some value, but do notreturn
anythingelse if( x >= 0 )
: you setresult
to some value, but do notreturn
anythingelse
: You do not initializeresult
, but you return it
Let's not forget that else if
means that none of the conditions of the if
and earlier else if
s than the current condition was true, but the condition of the current else if
is true and let's not forget that else
means that "none of the above is true". Since you had your return
inside your else
, in the first three cases you do not return
anything, hence, you violate the best practices of the language. This is why, the solution is to return
something, no matter what.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论