函数调用与返回值的问题

RyanJao 2010-12-20 03:55:01
#include<stdio.h>
float cube(float num);
int main(void)
{
float num, a;

printf("Please enter a number: ");
while(scanf("%f", &num) == 1)
{
a = cube(float num);
printf("the cube is %f", a);
}
return 0;
}

float cube(float b)
{
float b, cubed;
cubed = b * b * b;
return cubed;
}


请问,哪里出现问题了?
感觉很乱啊.
...全文
109 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
RyanJao 2010-12-20
  • 打赏
  • 举报
回复
感谢各位,终于没问题了,答案给的代码是不要返回值.
/* Programming Exercise 5-7 */
#include <stdio.h>
void showCube(double x);
int main(void) /* finds cube of entered number */
{
double val;

printf("Enter a floating-point value: ");
scanf("%lf", &val);
showCube(val);

return 0;
}

void showCube(double x)
{
printf("The cube of %e is %e.\n", x, x*x*x );
}


可能我一开始就和题意想扭了..

谢谢大家
luciferisnotsatan 2010-12-20
  • 打赏
  • 举报
回复
a = cube(float num); float??


float cube(float b) //参数b
{
float b, cubed; //有一个b
cubed = b * b * b;
return cubed;
}
liuintermilan 2010-12-20
  • 打赏
  • 举报
回复
#include<stdio.h>
float cube(float num);
int main(void)
{
float num, a;

printf("Please enter a number: ");
while(scanf("%f", &num) == 1)
{
a = cube(num);
printf("the cube is %f", a);
}
return 0;
}

float cube(float b)
{
float cubed;
cubed = b * b * b;
return cubed;
}
liuintermilan 2010-12-20
  • 打赏
  • 举报
回复
float cube(float b)
{
float b, cubed;
cubed = b * b * b;
return cubed;
}

这个。。。重定义b了吧。
一楼,好像说错了吧,他就是返回一个值,这样写没什么问题。
RyanJao 2010-12-20
  • 打赏
  • 举报
回复
看书还没有看到函数这部分,零零散散的介绍了点函数调用的东西.

这道题要求这样做.

请教下,按照我的这个意思,正确该怎么写?
jackyjkchen 2010-12-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 walkersfaint 的回复:]
....
a = cube(float num);
改为
a = cube(num);
还有就是你想返回一个局部变量是没有效果的,局部变量在函数结束后就自动释放了
[/Quote]
返回值传递的局部变量没关系,不能返回的是局部变量的引用或者指针
pengzhixi 2010-12-20
  • 打赏
  • 举报
回复
a = cube(num);
float cube(float b)
{
float b, cubed;//定义一个局部变量和参数名一样?
walkersfaint 2010-12-20
  • 打赏
  • 举报
回复
....
a = cube(float num);
改为
a = cube(num);
还有就是你想返回一个局部变量是没有效果的,局部变量在函数结束后就自动释放了

70,037

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧