Calling a function...

This is a discussion on Calling a function... within the C Programming forums, part of the General Programming Boards category; Code: int main() { char weight[6]; int validity, break_flag; while(1) { break_flag = getinput (weight); if (break_flag == 1) break; ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    127

    Calling a function...

    Code:
    int main()
    {
       char weight[6];
       int validity, break_flag;
    
       while(1)
       {
          break_flag = getinput (weight);
          if (break_flag == 1)
             break;
          validity = validate(weight);
          if (validity == 1)
             printf("%s - Too Heavy\n", weight);
          else
             printf("%s - Cleared\n", weight);
       }
       return 0;
    }
    I have the following code as my main function but I have just been told I'm not allowed to have a loop in my main. I tried moving the while loop to new function and using main to call that function (as below) but got the following errors:

    weight.c: In function `loop':
    weight.c:111: warning: control reaches end of non-void function
    weight.c: In function `main':
    weight.c:115: warning: statement with no effect

    The program works fine before the changes.


    Code:
    int loop()
    {
       char weight[6];
       int validity, break_flag;
    
       while(1)
       {
          break_flag = getinput (weight);
          if (break_flag == 1)
             break;
          validity = validate(weight);
          if (validity == 1)
             printf("%s - Too Heavy\n", weight);
          else
             printf("%s - Cleared\n", weight);
       }
    }
    
    int main()
    {
       loop;
    
       return 0;
    }

  2. #2
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    You forgot the parentheses.

    Code:
    int main()
    {
       loop();
    
       return 0;
    }
    And as your function does not return anything, it should be declared as void loop();
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    When you call a function you need parenthesis after the identifier regardless of whether it takes arguments or not. What do you mean "not allowed to have a loop in my main". You mean as per your assignment or as a general programming rule?
    Sent from my iPadŽ

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    According to my teacher it is a programming rule.

    Thanks for your help.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Not a programming rule I've ever heard of. Your original code is better than your new code.
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    I guess it will just be a rule to pass the subject then I'll forget about it in that case lol

  7. #7
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    I'm with sly. There are times when certain functionality should be farmed off to functions but this just creates disjointed code for no real benefit.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Question on function syntax and calling function
    By cbrman in forum C Programming
    Replies: 10
    Last Post: 10-05-2003, 05:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21