【问题标题】:The function is not returning in clang该函数没有在铿锵声中返回
【发布时间】:2021-10-02 05:06:35
【问题描述】:

我正在尝试编写一个程序来计算矩形的面积 但是函数没有返回任何值

这是cs50沙箱中的代码

#include <stdio.h>
#include <cs50.h>

int calcrectarea(int len,int wid){
    return len*wid;
}

int main(){        
    printf("enter length here:\n");         
    int x; scanf("%d",&x);

    printf("enter width here:\n");
    int y; scanf("%d", &y);

    calcrectarea(x,y);           
}

【问题讨论】:

  • 该函数确实返回了一个值,但您并没有对它做任何事情。尝试将其分配给变量或打印它。
  • 你期望什么输出? Edit 并显示输入和预期输出的示例。另请阅读:How to Ask
  • calcrectarea(x,y); 应该是int result = calcrectarea(x,y);

标签: c clang cs50


【解决方案1】:

实际上,函数返回一个值。也许你想看,所以打印结果:

#include <stdio.h>
#include <cs50.h>

int calcrectarea(int len,int wid){
    return len*wid;
}

int main(){        
    printf("enter length here:\n");         
    int x; scanf("%d",&x);

    printf("enter width here:\n");
    int y; scanf("%d", &y);

    printf("area is: %d", calcrectarea(x,y));           
}

【讨论】:

    猜你喜欢
    • 2016-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-08
    • 2012-07-08
    • 1970-01-01
    • 2013-12-01
    相关资源
    最近更新 更多