Thread: Functions - Help!

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    3

    Functions - Help!

    Hi, I'm not so great with C programming, however in this assignment I'm supposed to use this function:

    Code:
    int
    find_div(int n)
    {
      int trial,
          divisor;
    
      if (even(n)) {
        divisor = 2; 
    
      }else{
        divisor = 0;
        trial = 3;
      }
    
      while (divisor == 0) {
        if (trial > sqrt (n))
          divisor = n;
        else if ((n % trial)==0)
          divisor = trial;
        else
          trial += 2;
      }
      return (divisor);
    }
    This is pretty much copied directly out of the book but it will not compile. I'm pulling my hair out trying to figure it out but I'm sure someone here could spot the problem in a second. Thanks!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It would help quite a bit if you indicated:
    1. What the error message is.
    2. Mark the line in the post that the error refers to.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    3
    Sorry, the one I can't seem to shake no matter what is that it is expecting a token before the initial { after "int find_div(int n)"

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by autoprogrammer View Post
    Sorry, the one I can't seem to shake no matter what is that it is expecting a token before the initial { after "int find_div(int n)"
    Then you most likely have something wrong on the line BEFORE that, OR you have some invisible character in the file that we can't see here.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    You haven't posted the whole code. Maybe you just miss a } in your main program? I mean, this cannot compile without a main...

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    3
    I got rid of the token error but now it tells me the "called object is not a function" in the line where it says "divisor = 2;"

    Code:
    #include <stdio.h> /* printf, scanf definitions */
    #include <math.h>
    
    int
    main (void){
    
      int even,
     
    
    int
    find_div(int n)
    {
      int trial,
          divisor;
    
      if (even(n)) {
        divisor = 2; 
    
      }else{
        divisor = 0;
        trial = 3;
      }
    
      while (divisor == 0) {
        if (trial > sqrt (n))
          divisor = n;
        else if ((n &#37; trial)==0)
          divisor = trial;
        else
          trial += 2;
      }
      return (divisor);
    }
    
    }
    I haven't really gotten much further than this yet...

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    int
    main (void){
    
      int even,
    is not a complete function, so perhaps you need to consider completing the main function - at least to such an extent that the compiler can proceed to compile the next function. In this case, you have an incomplete line of variable declaration, and a missing brace to complete the main - at the very least.

    It is a good idea to compile often, but it's not particularly meaningfull to compile unfinished code - in this case it's a bit like the painters coming into your new house to paint your front room, but the bricklayer is still there finishin the second half of the wall.

    Often, when working on a project, you may find that making place-holder functions that are "incomplete code, but framework complete" - that is, you have all the "walls" in place, but not all details of the code "the bits that go on the walls (radiators, plug sockets, phone jacks, etc)".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    California
    Posts
    19
    You should note that one does not include function declarations and definitions inside of main! What you're doing now is just that; your bracket finishing main is at the end of the entire program! Usually programmers write additional functions like this:

    Code:
    int main(void) {
    //Do some stuff, add things, subtract others, etc:
    int a1, a2;
    a1 = function1(5, 3, ...);
    
    }
    
    int function1(int arg1, int arg2, ...) {
    // Define the function here
    }
    
    int function2(int arg2, int arg2, ...) {
    //Define another function
    }
    The "walls" in matsp's analogy are these function declarations. You can in fact just declare them with some arguments and return some generic value (NULL works great for this) and then call them in your main, and the compiler will happily trundle along, like so:

    Code:
    int function1(int arg1, int arg2){
    return NULL;
    }
    But this is beside the point of what you are doing. Look at the framework I gave, and look at your program. What would you do to fix it to make it structured like mine?

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    6
    you should use a coding standard so everybody would understand it

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Smattacus, I am aware of your religious beliefs which formally exclude the usage of proper tabbing, but feel free to just humor those of us who don't like to reformat your code to read it, copy and paste it into VC++ and hit ctrl+k,ctrl+f, or simply just use our years of reading crappy coding experience.

  11. #11
    Registered User
    Join Date
    Sep 2008
    Location
    California
    Posts
    19
    My bad I forgot since I was just writing out a semi - psuedo code example in the thread. Here's a tabbed version for those who care.

    Code:
    int main(void) {
    	//Do some stuff, add things, subtract others, etc:
    	int a1, a2;
    	a1 = function1(5, 3, ...);
    
    }
    
    int function1(int arg1, int arg2, ...) {
    	// Define the function here
    }
    
    int function2(int arg2, int arg2, ...) {
    	//Define another function
    }
    Hooray!

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Thanks I know sometimes I seem like I am being both petty and pedantic, but newcomers may benefit from your code too! So I think its only right to give them an opportunity to also be able to fully have a fighting chance in hell to understand your code and not get hung up on syntax.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM