473,320 Members | 2,083 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Functions> call and return value

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 :)

Apr 24 '07 #1
17 1949
Sara said:

<snip>
>
So far I have this.
function
Not 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.
Apr 24 '07 #2
"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.
function
That'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 :)

Apr 24 '07 #3
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
Apr 24 '07 #4
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.
int function(/* arguments */
{
/* code */
return 3; /* or something else */
}

int main(void)
{
int something;
something = function(/* arguments */);
return 0;
}

Since this is covered early in almost any C textbook, I have to think
you are trying to code without bothering to learn a damn thing first.
Get a textbook and read it, doing the exercises.
Apr 24 '07 #5

"Sara" <sa*****@gmail.comwrote in message
news:11*********************@r30g2000prh.googlegro ups.com...
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
Make that
int function()
{
int number;
int factor;
factor needs a value depending on what you're doing with it. Right now it
contains some random number (maybe 0 if you're lucky). Change to:
int factor = 0;
or whatever the value is supposed to be.
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);
So number is the value you were trying to find?
}

return (0);
Instead of returning 0, return the number.

return ( number );
}

I am just learning how to program in C, so I'm a basic beginner.

Any ideas or explanations will be appreciated.
I don't quite understand what you are trying to do in this function, since
I'm not sure what a perfect number is. But do you really want to go to
MAX_LIMIT or do you want to go to some other value passed into the function?
And what is factor supposed to be? Should that also be passed in?
Apr 24 '07 #6
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.

Any ideas or explanations will be appreciated.

Thanks
Sarah :)
Lots of other have pointed out the many flaws in your code just as code.
However there are also logic errors in your function. Consider the
following function and then try to determine what is wrong with yours
(from the logic view)

bool is_perfect(int number){
int total = 0;
for(int i=0; i < number; ++i){
if(number % i == 0) total += i;
}
return(number == total);
}
{And for the non-number theorists among you, a perfect number is a
number that equals the sum of its proper factors. E.g. 6 = 3 + 2 + 1 and
28 = 14 + 7 +4 + 2 +1. There is a known relationship between even
perfect numbers and Mersenne primes. However when I last looked it was
still an open issue as to whether there are any odd perfect numbers.)
Apr 25 '07 #7
Francis Glassborow said:

<snip>
Lots of other have pointed out the many flaws in your code just as
code.
Um... yes, quite so.
However there are also logic errors in your function. Consider
the following function and then try to determine what is wrong with
yours (from the logic view)

bool is_perfect(int number){
int total = 0;
for(int i=0; i < number; ++i){
if(number % i == 0) total += i;
}
return(number == total);
}
Did you try running this, Francis? If so, what happened on the first
iteration of the loop?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Apr 25 '07 #8
Richard Heathfield wrote:
>bool is_perfect(int number){
int total = 0;
for(int i=0; i < number; ++i){
if(number % i == 0) total += i;
}
return(number == total);
}

Did you try running this, Francis? If so, what happened on the first
iteration of the loop?
Didn't need to because I knew there was an error but perhaps I should
have warned the OP that he needs to check code rather than take it on trust.
Apr 25 '07 #9
On 24 Apr, 15:19, Sara <saum...@gmail.comwrote:
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.
This is quite a fundamental thing to understand, so I'd suggest
reading your book until it makes sense to you.

As an example, here is a very simple function:

int add(int a, int b) {
int c;

c = a + b;
return c;
}

and you would call it like the following (either as part of main or as
part of another function):

int x;
x = add(3, 4);

What happens here is that the code for the function "add" is executed.
a starts off with the value 3 (as that's the first parameter, ie the
first thing in the brackets when we call the function) and b starts
off as 4. During the course of the function running, c gets the value
7. And this is the value that gets sent back to the calling routine,
so x gets set to this value. So in this very simple example, two
values get sent into the function, and one comes out. Different
functions may send more (or fewer) values in, but sending more than
one value back is a little tricky.
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 :)
I'm not quite sure what you are trying to do here. One thing you could
do is write a function that tests whether the number sent in is
perfect and, if it is, prints out all its factors. Then main would
just be a loop that sends all the numbers to this function one by one
- sometimes the function will print out something and other times it
won't. Another way would be to write a function which returns 1 if the
number sent in is perfect, and 0 if it isn't. In this case, the
routine in main would read this value and print all the factors, or
not, as a result of it. This would mean that both the function and the
main routine have the job of working out all the factors of the
number, which seems a little wasteful.

Hope this helps.
Paul.

Apr 25 '07 #10
On Apr 24, 7:19 pm, Sara <saum...@gmail.comwrote:
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 :)
hey sara..
wud u plz make it clear what do u mean by factor n perfect number over
here...
if u do so i hav got a perfect program for u its very simple but b4
dat make it clear.

Apr 26 '07 #11
Bond wrote, On 26/04/07 22:22:

<snip>
hey sara..
wud u plz make it clear what do u mean by factor n perfect number over
here...
if u do so i hav got a perfect program for u its very simple but b4
dat make it clear.
This, as far as I am concerned, is completely unreadable. If you want to
communicate make an effort to make your posts understandable. If you are
not prepared to make an effort, then you will probably find that most of
the experts are not going to make any effort to help you.
--
Flash Gordon
Apr 26 '07 #12
"Flash Gordon" writes:
Bond wrote, On 26/04/07 22:22:

<snip>
>hey sara..
wud u plz make it clear what do u mean by factor n perfect number over
here...
if u do so i hav got a perfect program for u its very simple but b4
dat make it clear.

This, as far as I am concerned, is completely unreadable. If you want to
communicate make an effort to make your posts understandable. If you are
not prepared to make an effort, then you will probably find that most of
the experts are not going to make any effort to help you.
I think your irony detector need a tune up.
Apr 26 '07 #13
osmium wrote:
"Flash Gordon" writes:
>Bond wrote, On 26/04/07 22:22:

<snip>
>>hey sara..
wud u plz make it clear what do u mean by factor n perfect number over
here...
if u do so i hav got a perfect program for u its very simple but b4
dat make it clear.
This, as far as I am concerned, is completely unreadable. If you want to
communicate make an effort to make your posts understandable. If you are
not prepared to make an effort, then you will probably find that most of
the experts are not going to make any effort to help you.

I think your irony detector need a tune up.

His shit detector is working as it should, though.
Apr 27 '07 #14
Bond wrote:
>
.... snip ...
<
wud u plz make it clear what do u mean by factor n perfect number
over here...
if u do so i hav got a perfect program for u its very simple but
b4 dat make it clear.
I make it 11 badly misspelled words, and 27 more or less correct.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Apr 27 '07 #15
Flash Gordon wrote:
Bond wrote, On 26/04/07 22:22:

<snip>
>hey sara..
wud u plz make it clear what do u mean by factor n perfect number over
here...
if u do so i hav got a perfect program for u its very simple but b4
dat make it clear.

This, as far as I am concerned, is completely unreadable. If you want to
communicate make an effort to make your posts understandable. If you are
not prepared to make an effort, then you will probably find that most of
the experts are not going to make any effort to help you.
And contributors who are hooked on txt spelling should remember that
they are not charged by the character to post here and many readers
struggle to read English (it not being their native language) let alone
trying to understand the compressed form used for txt.
Apr 27 '07 #16
CBFalconer wrote:
Bond wrote:
... snip ...
<
>wud u plz make it clear what do u mean by factor n perfect number
over here...
if u do so i hav got a perfect program for u its very simple but
b4 dat make it clear.

I make it 11 badly misspelled words, and 27 more or less correct.
2 Y's U R 2 Y's U B I C U R 2 Y's 4 me ;-)

(M R 2 ducks! C M wings???)

--
+----------------------------------------------------------------+
| Charles and Francis Richmond richmond at plano dot net |
+----------------------------------------------------------------+
Apr 29 '07 #17

"Charles Richmond" <fr*****@tx.rr.comwrote in message
news:46**************@tx.rr.com...
CBFalconer wrote:
>Bond wrote:
... snip ...
<
>>wud u plz make it clear what do u mean by factor n perfect number
over here...
if u do so i hav got a perfect program for u its very simple but
b4 dat make it clear.

I make it 11 badly misspelled words, and 27 more or less correct.

2 Y's U R 2 Y's U B I C U R 2 Y's 4 me ;-)

(M R 2 ducks! C M wings???)

--
+----------------------------------------------------------------+
| Charles and Francis Richmond richmond at plano dot net |
+----------------------------------------------------------------+
Make that: C D E D B D wings?

Gary
May 1 '07 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: lawrence | last post by:
If I want a function to return by reference, I do this? function & myCoolFunction() { $queryObject = new queryObject(); return $queryObject; } I get back a reference to the object...
3
by: Zork | last post by:
Hi, I am a little confused with const functions, for instance (here i am just overloading the unary+ operator) if i define: 1) Length Length :: operator+ ( void ) const {return * this;} ... I...
2
by: Rufus | last post by:
Hi all, I have a problem with the next example code. I can't compile it without warnings with gcc. The compilation out is: $ gcc -Wall aPoint.c aPoint.c: In function 'printaPoint2':...
127
by: sanjay.vasudevan | last post by:
Why are the following declarations invalid in C? int f(); int f(); It would be great if anyone could also explain the design decision for such a language restricton. Regards, Sanjay
4
by: The Doctor | last post by:
I have a question about virtual functions, and all that stuff. Let's say, I have three classes. class Base; class firstDerived; class secondDerived; I defined them as following:
0
by: Chris Rebert | last post by:
On Wed, Nov 19, 2008 at 11:24 PM, Barak, Ron <Ron.Barak@lsi.comwrote: As I believe someone else pointed out recently in a extremely similar thread, GzipFile apparently doesn't check that the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.