简体   繁体   English

我的函数没有返回任何东西,但可以编译并且看起来是正确的

[英]My function isn't returning anything but does compile and seems correct

Having a hard time trying to figure out why my code doesn't return anything.很难弄清楚为什么我的代码不返回任何内容。 I want it to return '3628800' as 10!我希望它返回 '3628800' 为 10! (factorial). (阶乘)。 Any help is appreciated.任何帮助表示赞赏。

#include <stdio.h>

void ft_iterative_factorial(int nb);

int main()
{
    ft_iterative_factorial(10);
    return 0;
}

void ft_iterative_factorial(int nb)
{
    int i;
    int fact;
    int num;

    fact = 1;
    if (num <= 0)
        fact = 1;
    else
    {
        i = 1;
        while (i <= num)
        {
            fact = fact * i;
            i++;
        }
    }
}

You need to specify a return type, so you would have something like this.你需要指定一个返回类型,所以你会有这样的东西。

#include <stdio.h>

int ft_iterative_factorial(int nb);

int main()
{
    int num;
    num = ft_iterative_factorial(10);
    return 0;
}

int ft_iterative_factorial(int nb)
{
    int i;
    int fact;
    int num = nb;

    fact = 1;
    if (num <= 0)
        fact = 1;
    else
    {
        i = 1;
        while (i <= num)
        {
            fact = fact * i;
            i++;
        }
    }

   return fact;
}

Hi your function 'ft_iterative_factorial' does not have a return type.嗨,您的函数 'ft_iterative_factorial' 没有返回类型。 Function should have a return type to return some value to the calling function.函数应该有一个返回类型来向调用函数返回一些值。 Also you are not using the passed parameter 'nb' anywhere in your factorial function.此外,您没有在阶乘函数中的任何地方使用传递的参数“nb”。

Here is the corrected code:这是更正后的代码:

#include <stdio.h>

//function should have a return value to return something
int ft_iterative_factorial(int nb);

int main()
{
    printf("%d",ft_iterative_factorial(10));
    return 0;
}

int ft_iterative_factorial(int nb)
{
    int i;
    int fact;
    int num = nb;// You have not assigned nb to num
    // In your case num is not initailised

    fact = 1;
    if (num <= 0)
        fact = 1;
    else
    {
        i = 1;
        while (i <= num)
        {
            fact = fact * i;
            i++;
        }
    }
    return fact;
}

If the code compiles, you should simulate the execution of your program step by step:如果代码编译通过,您应该逐步模拟程序的执行:

  • the execution enters in main function;执行进入main函数;
  • the execution enters in ft_iterative_factorial , with argument 10 ;执行进入ft_iterative_factorial ,参数为10
  • the variable fact is initialized to 1 ;变量fact被初始化为1
  • [...] (computations, that seem to be correct); [...](计算,似乎是正确的);
  • the execution leaves ft_iterative_factorial , and therefore discards all variables declared within its scope...执行离开ft_iterative_factorial ,因此丢弃在其范围内声明的所有变量......

... and here is the problem: the value of fact is lost. ……问题就在这里: fact的价值丢失了。

If you want that value to be passed back to main function you should for example declare the function ft_iterative_factorial as如果您希望将该值传递回主函数,您应该例如将函数ft_iterative_factorial声明为

int ft_iterative_factorial(int nb);

and add并添加

return fact;

at the end of its body.在它的身体末端。

My function isn't returning anything but does compile and seems correct我的函数没有返回任何东西,但可以编译并且看起来是正确的

Your code is not correct at all.您的代码根本不正确。 In order to check that just print the value of fact inside your ft_iterative_factorial function.为了检查是否在ft_iterative_factorial函数中打印了fact的值。 By change the return type from void to int , you could solve your return issue but I think you should check the body of ft_iterative_factorial carefully.通过将返回类型从void更改为int ,您可以解决您的返回问题,但我认为您应该仔细检查ft_iterative_factorial的主体。

Hint::暗示::

void ft_iterative_factorial(int nb) //<---change the return type to int
{
    int i;
    int fact;
    int num;

    fact = 1;
    if (num <= 0)  //<-----what is num????? should be nb
        fact = 1;
    else
    {
        i = 1;
        while (i <= num) //<-----what is num????? should be nb
        {
            fact = fact * i;
            i++;
        }
    }
           //<------ just add return statement

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM