我的子程序在C语言中不工作。

huangapple go评论60阅读模式
英文:

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 set result to some value, but you do not return anything
  • else if( x >= 40 ): you set result to some value, but do not return anything
  • else if( x >= 0 ): you set result to some value, but do not return anything
  • else: You do not initialize result, but you return it

Let's not forget that else if means that none of the conditions of the if and earlier else ifs 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.

huangapple
  • 本文由 发表于 2023年5月17日 19:11:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76271453.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定