Functions> call and return value

S

Sara

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

Richard Heathfield

Sara said:

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.
 
O

osmium

Sara said:
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.
 
D

Default User

Sara said:
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
 
M

Martin Ambuhl

Sara said:
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.
 
J

Jim Langston

Sara said:
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?
 
F

Francis Glassborow

Sara said:
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.)
 
R

Richard Heathfield

Francis Glassborow said:

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?
 
F

Francis Glassborow

Richard said:
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.
 
G

gw7rib

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.
 
B

Bond

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.
 
F

Flash Gordon

Bond wrote, On 26/04/07 22:22:

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.
 
O

osmium

Flash Gordon said:
Bond wrote, On 26/04/07 22:22:



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.
 
F

Francis Glassborow

Flash said:
Bond wrote, On 26/04/07 22:22:



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.
 
C

Charles Richmond

CBFalconer said:
Bond wrote:
... snip ...
<

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top