功能与GT;呼叫和返回值 [英] Functions> call and return value
问题描述
我的程序有问题,我认为这源于我不是
了解如何调用函数和将一个int值返回给main。
我要做的是创建一个运行数字的程序
并找到完美的数字并列出它们的因素。我不能使用
指针或表数组或公式。而且我需要从主要的所有这一切中分离出一个单独的
函数。
到目前为止,我已经找到了完美的数字部分,但它仍然是在
main。
我不明白如何从main调用函数并返回
完美数字然后列出这些因素。
我最终找到了因素部分,但这样做了所有的数字,而不是
只是完美的数字。当我尝试修复我的程序时,我失去了
我是怎么做到的。
到目前为止,我有这个。
功能
{
int number;
int factor;
int total;
for(number = 1; number< = MAX_LIMIT; ++ number)
{
total = 0;
如果(数字%因子== 0)
{
总计+ =因子;
}
}
if(number == total)
{
printf("完美数字是%5d",数字);
}
返回(0);
}
我只是在学习如何用C编程,所以我是一个基本的初学者。
任何想法或解释都将不胜感激。
谢谢
Sarah :)
Sara说:
< snip> ;
>
到目前为止我有这个。
函数
不是函数的有效声明符。目前还不太清楚这个
函数应该做什么,或者我会建议一些合理的东西。
{
int number;
int factor;
int total;
这里,您保留三个整数的存储空间,但是您不会为它们分配任何
值。对于他们中的两个,这不是什么大不了的事,因为
你以后会给他们价值。但对于因素来说,这是一个大问题。
>
for(number = 1; number< = MAX_LIMIT; ++ number)
{
total = 0;
if(number%factor == 0)
这里你尝试在数字除以因子之后取余数,
但是因子没有赋值,所以表达式是
无意义。这可能不是你唯一的问题,但它肯定是一个很大的问题,你需要在继续进行之前决定什么价值因素应该是
。 />
-
Richard Heathfield
Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk
电子邮件:rjh在上述域名中, - www。
" Sara"写道:
我的程序有问题,我认为这源于我不是
了解如何调用函数并将一个int值返回给main。
我要做的是创建一个程序,运行数字
并找到完美的数字并列出他们的因素。我不能使用
指针或表数组或公式。而且我需要从主要的所有这一切中分离出一个单独的
函数。
到目前为止,我已经找到了完美的数字部分,但它仍然是在
main。
我不明白如何从main调用函数并返回
完美数字然后列出这些因素。
我最终找到了因素部分,但这样做了所有的数字,而不是
只是完美的数字。当我尝试修复我的程序时,我失去了
我是怎么做到的。
到目前为止,我有这个。
函数
这是一个过时的形式。它假设函数返回一个int。你离开了
a一对parens不是吗?
现代形式是
类型foo(类型名称,类型名称){code}
例如
双二次方(双a,双b,双c)
有三个参数并返回一个双重
返回多个值更难。首先尝试消化它。
对于多个值返回:
简单地说,您可以返回结构或修改
$所拥有的值b $ b通过指针调用。
C按值传递参数,即a,b和c是调用者中某些内容的*副本*
。
{
int number;
int factor;
int total;
for(number = 1; number< = MAX_LIMIT; ++ number)
{
total = 0;
if(number%factor == 0)
{
总计+ =因子;
}
}
if(number == total)
{
printf(" The perfect number is%5d",number) ;
}
返回(0);
}
我是只是学习如何用C编程,所以我是一个基本的初学者。
任何想法或解释都将不胜感激。
谢谢
Sarah :)
Sara写道:
我的程序有问题,我认为它源于我不是
了解如何调用函数并将一个int值返回给main。
我要做的是创建一个贯穿数字的程序
并找到完美的数字并列出它们的因素。我不能使用
指针或表数组或公式。而且我需要从主要的所有这一切中分离出一个单独的
函数。
到目前为止,我已经找到了完美的数字部分,但它仍然是在
main。
我不明白如何从main调用函数并返回
完美数字然后列出这些因素。
我最终找到了因素部分,但这样做了所有的数字,而不是
只是完美的数字。当我尝试修复我的程序时,我失去了
我是怎么做到的。
到目前为止,我有这个。
功能
{
int number;
int factor;
int total;
for(number = 1; number< = MAX_LIMIT; ++ number)
{
total = 0;
如果(数字%因子== 0)
{
总计+ =因子;
}
}
if(number == total)
{
printf("完美数字是%5d",数字);
}
返回(0);
}
我只是在学习如何用C编程,所以我是一个基本的初学者。
你使用的书没有超过基本功能
声明,定义和使用?
Brian
Hi,
I''m having a problem with my program and I think it stems from me not
understand how to call a function and return a int value to main.
What I have to do is create a program that runs through the numbers
and find the perfect numbers and list their factors. I cannot use
pointers or a table array or formulae. And I need to have a seperate
function from main doing all of this.
So far I have done the find the perfect numbers part, but its still in
main.
I don''t understand how I can call the function from main and return
the perfect number and then list the factors.
I ended up finding the factors part, but that did all the numbers, not
just the perfect numbers. And as I tried fixing up my program, I lost
how I did that somehow.
So far I have this.
function
{
int number;
int factor;
int total;
for (number = 1; number <= MAX_LIMIT; ++number)
{
total = 0;
if (number%factor ==0)
{
total += factor;
}
}
if (number == total)
{
printf ("The perfect number is %5d ", number);
}
return (0);
}
I am just learning how to program in C, so I''m a basic beginner.
Any ideas or explanations will be appreciated.
Thanks
Sarah :)
解决方案Sara said:
<snip>>
So far I have this.
functionNot a valid declarator for a function. It''s not quite clear what this
function is supposed to be doing, or I''d suggest something sensible.
{
int number;
int factor;
int total;Here, you reserve storage for three ints, but you don''t assign any
values to them. For two of them, that''s not such a big deal, because
you give them values later on. But for ''factor'', it''s a big deal.
>
for (number = 1; number <= MAX_LIMIT; ++number)
{
total = 0;
if (number%factor ==0)Here you try to take the remainder after a division of number by factor,
but factor hasn''t been assigned a value, so the expression is
meaningless. That may not be your only problem, but it''s definitely a
huge problem, and you need to decide what value factor should have
before proceeding further.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
"Sara" writes:
I''m having a problem with my program and I think it stems from me not
understand how to call a function and return a int value to main.
What I have to do is create a program that runs through the numbers
and find the perfect numbers and list their factors. I cannot use
pointers or a table array or formulae. And I need to have a seperate
function from main doing all of this.
So far I have done the find the perfect numbers part, but its still in
main.
I don''t understand how I can call the function from main and return
the perfect number and then list the factors.
I ended up finding the factors part, but that did all the numbers, not
just the perfect numbers. And as I tried fixing up my program, I lost
how I did that somehow.
So far I have this.
functionThat''s an obsolete form. It assumes function returns an int. You left off
a pair of parens didn''t you?
The modern form is
type foo(type name, type name) { code}
For example
double quadratic(double a, double b, double c)
has three parameters and returns a double
Returning multiple values is harder. Try to digest this first.
For multiple values return:
Briefly you can return a structure or you modify the values owned by the
caller via pointers.
C passes parameters by value, that is, a, b and c are *copies* of something
in the caller.
{
int number;
int factor;
int total;
for (number = 1; number <= MAX_LIMIT; ++number)
{
total = 0;
if (number%factor ==0)
{
total += factor;
}
}
if (number == total)
{
printf ("The perfect number is %5d ", number);
}
return (0);
}
I am just learning how to program in C, so I''m a basic beginner.
Any ideas or explanations will be appreciated.
Thanks
Sarah :)
Sara wrote:
Hi,
I''m having a problem with my program and I think it stems from me not
understand how to call a function and return a int value to main.
What I have to do is create a program that runs through the numbers
and find the perfect numbers and list their factors. I cannot use
pointers or a table array or formulae. And I need to have a seperate
function from main doing all of this.
So far I have done the find the perfect numbers part, but its still in
main.
I don''t understand how I can call the function from main and return
the perfect number and then list the factors.
I ended up finding the factors part, but that did all the numbers, not
just the perfect numbers. And as I tried fixing up my program, I lost
how I did that somehow.
So far I have this.
function
{
int number;
int factor;
int total;
for (number = 1; number <= MAX_LIMIT; ++number)
{
total = 0;
if (number%factor ==0)
{
total += factor;
}
}
if (number == total)
{
printf ("The perfect number is %5d ", number);
}
return (0);
}
I am just learning how to program in C, so I''m a basic beginner.What book are you using that doesn''t go over basic function
declaration, definition, and use?
Brian
这篇关于功能与GT;呼叫和返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!