Thread: Creating a Function

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    117

    Creating a Function

    I'm having a problem creating a function, but when I compile I get this error:
    Code:
    BIGJERK2.C: In function `int main()':
    BIGJERK2.C:7: error: `jerk' undeclared (first use this function)
    BIGJERK2.C:7: error: (Each undeclared identifier is reported only once for each
    function it appears in.)
    BIGJERK2.C: At global scope:
    BIGJERK2.C:20: error: ISO C++ forbids declaration of `jerk' with no type
    BIGJERK2.C: In function `int jerk()':
    BIGJERK2.C:20: error: `int jerk()' used prior to declaration

    Here is the source code, can't find anything wrong with it, it is typed exactly how my C book wrote it :S
    Code:
    #include <stdio.h>
    
    int main()
    {
    	printf("He calls me on the phone with nothing to say\n");
    	printf("Not once, or twice, but three times a day!\n");
    	jerk();
    	printf("He insulted my wife, my cat, my mother\n");
    	printf("He irritates and grates, like no other!\n");
    	jerk();
    	printf("He chuckles is off, his big belly a-heavin'\n");
    	printf("But he won't be laughing when I get even!\n");
    	jerk();
    	return(0);
    }
    
    /* This is the jerk() function */
    
    jerk()
    {
    	printf("Bill is a jerk\n");
    }
    Thanks

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You are compiling the code as C++, and C++ compilers insist on more precision than C compilers. (If you were not compiling as C++, you would not receive an error message referring to things that ISO C++ forbids).

    Place a declaration (aka prototype) of jerk() in the file before main(). Or move the implementation of jerk() above main().

    Also, provide a return type for jerk in your implementation. If you don't want to return anything, return void. That is recommended practice in C, even if C compilers don't insist on it. And it is essential in C++.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Location
    Ca, US
    Posts
    29
    The code is compiled from the top down, then it gets to your jerk call in your main it does not yet know the function.

    So you can move the function up top before main, or prototype the function before main.

    Also your function definition should be

    void jerk(void)

    Dylan

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    117
    Quote Originally Posted by dylan View Post
    The code is compiled from the top down, then it gets to your jerk call in your main it does not yet know the function.

    So you can move the function up top before main, or prototype the function before main.

    Also your function definition should be

    void jerk(void)

    Dylan
    This worked. But in a real application I would like to make this would be VERY troublesome because the function would be very long. Putting it after main() would be less troublesome so I don't have to scroll down.

    The book I'm using said that once it hits a fn (function) that it doesn't know, it skips down until it find the declared fn, and executes it. Wonder why it doesn't :/

    Maybe something to do with it compiling in C++ instead of C? When I installed the compiler I insalled both the C, and C++ compiler.

    I am going to uninstall and re install with only the C compiler, see if that fixes it.


    Quote Originally Posted by grumpy View Post
    You are compiling the code as C++, and C++ compilers insist on more precision than C compilers. (If you were not compiling as C++, you would not receive an error message referring to things that ISO C++ forbids).

    Place a declaration (aka prototype) of jerk() in the file before main(). Or move the implementation of jerk() above main().

    Also, provide a return type for jerk in your implementation. If you don't want to return anything, return void. That is recommended practice in C, even if C compilers don't insist on it. And it is essential in C++.
    If i put "void Jerk(void)" after main however it doesn't work...hmmm give me a couple of minutes.

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by JonathanS View Post
    If i put "void Jerk(void)" after main however it doesn't work...hmmm give me a couple of minutes.
    This is how your program should look:
    Code:
    #include <stdio.h>
    
    void jerk(void); //declare function
    
    int main(void) //note how main is defined
    {
    	printf("He calls me on the phone with nothing to say\n");
    	printf("Not once, or twice, but three times a day!\n");
    	jerk();
    	printf("He insulted my wife, my cat, my mother\n");
    	printf("He irritates and grates, like no other!\n");
    	jerk();
    	printf("He chuckles is off, his big belly a-heavin'\n");
    	printf("But he won't be laughing when I get even!\n");
    	jerk();
    	return(0);
    }
    
    /* This is the jerk() function */
    /* define the function */
    void jerk(void)
    {
    	printf("Bill is a jerk\n");
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by JonathanS View Post
    This worked. But in a real application I would like to make this would be VERY troublesome because the function would be very long. Putting it after main() would be less troublesome so I don't have to scroll down.
    If that is true, you are probably better off putting the function into a separate source file. In that case, you still need the declare a function before calling it.

    Quote Originally Posted by JonathanS View Post
    Maybe something to do with it compiling in C++ instead of C? When I installed the compiler I insalled both the C, and C++ compiler.
    Clearly you haven't bothered to read my previous post.
    Quote Originally Posted by JonathanS View Post
    I am going to uninstall and re install with only the C compiler, see if that fixes it.
    That will hide the symptoms. The easier way is to compile as C (i.e. if you have a C and a C++ compiler installed, use the C compiler).

    The better way is to to use accepted good practice, that (in this case) works in both C and C++. Adding a function declaration (aka prototype) before calling it is such good practice.

    C compilers are a little lenient, in that they don't need to see a prototype before you call a function. That feature is deprecated in C (scheduled for removal from a future standard) because it allows bad practices by programmers.

    It is generally a better idea to use good coding practices, than to jump through hoops to get your compiler to accept bad coding practices. You, however, are doing the latter.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Registered User
    Join Date
    Sep 2011
    Posts
    117
    Quote Originally Posted by AndrewHunter View Post
    This is how your program should look:
    Code:
    #include <stdio.h>
    
    void jerk(void); //declare function
    
    int main(void) //note how main is defined
    {
    	printf("He calls me on the phone with nothing to say\n");
    	printf("Not once, or twice, but three times a day!\n");
    	jerk();
    	printf("He insulted my wife, my cat, my mother\n");
    	printf("He irritates and grates, like no other!\n");
    	jerk();
    	printf("He chuckles is off, his big belly a-heavin'\n");
    	printf("But he won't be laughing when I get even!\n");
    	jerk();
    	return(0);
    }
    
    /* This is the jerk() function */
    /* define the function */
    void jerk(void)
    {
    	printf("Bill is a jerk\n");
    }
    That works and is what I need! Thank you so much.

    Last question, you said be sure to put main as "main(void)". I was wondering why? I put it as "Main()" and "Main(void)" and there seems to be no functional difference.

    Also I'm sorry if I offended anyone, it is my incompetence/ignorance, not impatience.

  9. #9
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by JonathanS View Post
    Last question, you said be sure to put main as "main(void)". I was wondering why? I put it as "Main()" and "Main(void)" and there seems to be no functional difference.
    He was referring to the argument list, not the functions name. ( Although C is case-sensitive, main() is an exception ).

    In C ( not C++ ), leaving the argument list empty means that the function takes ANY number of arguments, while putting void in it means it takes NONE.
    I don't know why this was done, probably because they wanted to make C more usable with Assembly code.
    Devoted my life to programming...

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Sipher View Post
    I don't know why this was done, probably because they wanted to make C more usable with Assembly code.
    Not really. Part of the rationale is that assembly code does similar things, and C started life as a "high level assembler".

    The real reason it stayed in C was an assumption of the day that programmers would be disciplined enough to make things line up (i.e. if a function needs three arguments, the caller would provide three arguments of expected types). It was only through subsequent usage that, in practice, programmers demonstrated - in spades - they are nowhere near that disciplined.

    Practically, the design of any programming language includes a trade-off between preventing silly errors by programmers versus the hazards of offending programmers with too much hand-holding.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by JonathanS View Post
    The book I'm using said that once it hits a fn (function) that it doesn't know, it skips down until it find the declared fn, and executes it. Wonder why it doesn't :/
    Because the book you're using is WRONG.... C compilers read a page top down. If they find a reference to something that is neither defined nor prototyped they make wild assuptions about it and then when the assumptions are proven wrong, they error out.

    You have the following two options...
    Code:
    #include <stdio.h>
    
    void jerk (void); 
    
    
    int main( void )
    {
    	printf("He calls me on the phone with nothing to say\n");
    	printf("Not once, or twice, but three times a day!\n");
    	jerk();
    	printf("He insulted my wife, my cat, my mother\n");
    	printf("He irritates and grates, like no other!\n");
    	jerk();
    	printf("He chuckles is off, his big belly a-heavin'\n");
    	printf("But he won't be laughing when I get even!\n");
    	jerk();
    	return(0);
    }
    
    
    void jerk( void )
    {
    	printf("Bill is a jerk\n");
    }
    or

    Code:
    #include <stdio.h>
    
    
    void jerk( void )
    {
    	printf("Bill is a jerk\n");
    }
    
    
    int main( void )
    {
    	printf("He calls me on the phone with nothing to say\n");
    	printf("Not once, or twice, but three times a day!\n");
    	jerk();
    	printf("He insulted my wife, my cat, my mother\n");
    	printf("He irritates and grates, like no other!\n");
    	jerk();
    	printf("He chuckles is off, his big belly a-heavin'\n");
    	printf("But he won't be laughing when I get even!\n");
    	jerk();
    	return(0);
    }
    It's got nothing to do with C or C++ compilation... they both have the same rules, in this case.

    And by the way... not wanting to scroll down is a pea arsed poor reason to use bad programming practices.

  12. #12
    Registered User
    Join Date
    Sep 2011
    Posts
    117
    Thanks all for the responses, very informative and learned alot

    and the scrolling down matters when the source code is 1000's of lines long. I know there is probably a way to make separate source code files and sew them together, but... I'm learning as I go right now.

    Thanks again, much appreciated for all your time, and love this forum. It is great.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by JonathanS View Post
    and the scrolling down matters when the source code is 1000's of lines long. I know there is probably a way to make separate source code files and sew them together, but... I'm learning as I go right now.
    Nope... still no excuse for using bad practices.

    A C purist only uses function prototypes (also called "forward references") when he has to and builds his code bottom up and in an order such that no function is called before it's seen. There are obscure technical reasons why this is better but mostly it forces you to organize your code in a more rational manner. Prototypes, while not necessarily a bad thing, defeat the rigid ordering rules and tend to lead to sloppier code. Neither practice is wrong... but I'm sure you can see why I think one is better than the other.

    You aren't writing for your own convenience. Coding is not writing a pretty page, it is leveraging the right behaviors from a very limited vocabulary and you do what you need to do to get those behaviors.

    Simply this... don't be lazy... as they say: "Anything worth doing, is worth doing right."

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by JonathanS
    But in a real application I would like to make this would be VERY troublesome because the function would be very long. Putting it after main() would be less troublesome so I don't have to scroll down.
    In a "real application" (as if what you are doing now is any less real ), you would organise your code by grouping the definitions (implementation) of related functions into different source files, and provide header files to provide the declarations of these functions (typically, function prototypes) such that they can be used, e.g., by your main function.

    Quote Originally Posted by CommonTater
    A C purist only uses function prototypes (also called "forward references") when he has to and builds his code bottom up and in an order such that no function is called before it's seen. There are obscure technical reasons why this is better but mostly it forces you to organize your code in a more rational manner. Prototypes, while not necessarily a bad thing, defeat the rigid ordering rules and tend to lead to sloppier code. Neither practice is wrong... but I'm sure you can see why I think one is better than the other.
    I do not know about "C purists", but I agree with grumpy's assertion in post #7 that forward declarations are "accepted good practice". In practice, this kind of "order such that no function is called before it's seen" approach is mainly used for functions that are implementation detail: they are usually not forward declared as they are not intended to be used anywhere but the source file in which they are defined, and in fact they are typically declared static.

    Note that this "accepted good practice" does not contradict a recommendation to use forward declarations only when you have to. You certainly should not be forward declaring functions that are implementation detail in a header file unless you really have to. However, the general notion that "prototypes (...) tend to lead to sloppier code" is not widely accepted at all.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating an scanf function inside an function
    By anserudd in forum C Programming
    Replies: 4
    Last Post: 03-25-2011, 09:19 AM
  2. difficulty creating a function
    By Inver28 in forum C Programming
    Replies: 6
    Last Post: 02-24-2008, 05:07 AM
  3. Help creating function of sin cos and tan
    By Niz in forum C Programming
    Replies: 12
    Last Post: 01-09-2007, 05:55 PM
  4. Help creating function - 2005
    By cx323 in forum C# Programming
    Replies: 1
    Last Post: 06-10-2006, 09:31 PM
  5. Creating function
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 02-25-2002, 12:24 PM