Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 26th, 2005, 8:55 AM   #1
colt
Newbie
 
Join Date: Mar 2005
Posts: 7
Rep Power: 0 colt is on a distinguished road
Returning a value from a variable to the main function

Hi, I am having a big little problem to return to the main function, the value of a sum operation. As you can see in the code, all I want to do is to return the data contained in the 'res' variable (n1 + n2) to the main function.

There I call the function SUM through a call function "SUM (int res); "
to display in the end of the main function, the result of the sum: n1 + n2, but I get the following errors:

test.c: In function `main':
test.c:15: error: syntax error before "int"

So what I am doing wrong? What I need to do to sucessfully be able to return the data contained in the res variable to the main function to finally see the result of the mathematical operation?

Thanks in advance.

[code]#include <stdio.h>

int SUM (int n1, int n2);

main ()
{
int n1, n2, i;

while (5 == 5) {
printf ("To close the program, type 0: ");
scanf ("%d", &i);
if (i == 0) break;
printf ("Type the numbers that should be used to do the sum: ");
scanf ("%d %d", &n1, &n2);
SUM (int res);
}
}

int SUM (int n1, int n2)
{
int res;
res = n1 + n2;
printf ("The result is: %d", res);
return res;
}
colt is offline   Reply With Quote
Old Apr 26th, 2005, 9:34 AM   #2
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 5 Eggbert is on a distinguished road
>while (5 == 5) {
This is strange. Not only that you used <constant> == <constant>, but because you chose 5 as the constant. A more conventional way to create an infinite while loop is
while ( 1 ) {
  /* Body */
}
>SUM (int res);
A function call does not declare parameters, it takes arguments:
SUM (n1, n2);
Here is the same program with the necessary fixes:
#include <stdio.h>

int SUM (int n1, int n2);

int main ()
{
  int n1, n2, i;

  while (1) {
    printf ("To close the program, type 0: ");
    scanf ("%d", &i);
    if (i == 0) break;
    printf ("Type the numbers that should be used to do the sum: ");
    scanf ("%d %d", &n1, &n2);
    SUM (n1, n2);
  }
}

int SUM (int n1, int n2)
{
  int res;
  res = n1 + n2;
  printf ("The result is: %d", res);
  return res;
}
Eggbert is offline   Reply With Quote
Old Apr 28th, 2005, 8:11 AM   #3
mackenga
Professional Programmer
 
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 328
Rep Power: 4 mackenga is on a distinguished road
Nice solution. I just thought I'd add; another conventional way to do an infinite loop is for (;;), which arguably is even clearer since it doesn't even have the 'dummy condition' constant 1.

Last edited by mackenga; Apr 28th, 2005 at 8:11 AM. Reason: Darn smileys... :)
mackenga is offline   Reply With Quote
Old Apr 28th, 2005, 8:56 AM   #4
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,473
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
Speaking of infinite loops... here's the "cool" way...

#define EVER ;;

for (EVER)
{
...
}
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:37 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC