473,769 Members | 2,382 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1982
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(doubl e 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******** *************@r 30g2000prh.goog legroups.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

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

Similar topics

6
11092
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 automatically now?
3
1600
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 get no error... 2) Length & Length :: operator+ ( void ) const {return * this;} ... I get the error -> 'return' : cannot convert from 'const class Length' to
2
1486
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': aPoint.c:23: warning: passing argument 1 of 'printaPoint1' from incompatible pointer type aPoint.c: In function 'printaPoint3': aPoint.c:30: warning: passing argument 1 of 'printaPoint1' from incompatible
127
4918
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
1089
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
1260
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 underlying file is actually in gzip format until the .read() call, hence why its placement affects where the exception is thrown and thus how it's handled. read2() has the .read() for the gzipped case within the `try`, so the exception, if it's going...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10043
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8869
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3956
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.