Thread: Functions confuse me so, what am i doing wrong?

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    50

    Functions confuse me so, what am i doing wrong?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    #include <ctype.h>
    
    
    int main()
    {
        printf("He calls me on the phone with nothing 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 it off, his big belly a heavin'\n");
        printf("But he wont be laughin when I get even!\n");
        jerk();
        return 0;
    }
    
    
    //This is the jerk fiunction
    jerk()
    {
        printf("Bill is a jerk\n");    
    }
    Why am I getting an undeclared identifier error? I dont know what to do here everything I’m reading says this should work. I’m getting the error on line 11, the first jerk call.
    Last edited by Ctylersills; 04-18-2019 at 01:11 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You must declare the function before calling it. This can be done either with a forward declaration, or by moving the function definition to before the definition of the function that calls it, since a definition is also a declaration.

    You must specify the function return type, presumably void in this case.

    You should specify void as the parameter list since the function takes no arguments.
    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

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    62
    You're doing everything right... almost! The compiler only knows what it's seen so far. As it's reading your program from top to bottom, it encounters the function calls on lines 12, 15 and 18. It doesn't know what the jerk function is because it hasn't seen it yet. There are two ways to fix this. You could move the jerk function above the main function, that way the compiler knows what the jerk function is before you call it. Or you can leave the jerk function where it is and put a function prototype above your main function. The function prototype is just the beginning part of the jerk function with a semicolon at the end, like this.

    Code:
    void jerk(void);
    If you put that above the main function, the C compiler will know that there is a jerk function, that it doesn't return anything, and that it doesn't take any parameters. It doesn't know what the jerk function actually does yet, but it will make a note, so to speak, that the jerk function exists somewhere else in the program and it'll fill in the details later.

    There are two minor issues here, as well. If you have a function that doesn't return any value, like jerk, you should state the return type as void. Also, if you have no function parameters like your void function, you should put (void) instead of ().

  4. #4
    Registered User I C everything's Avatar
    Join Date
    Apr 2019
    Posts
    101
    for bigger projects or if you make a great function you want include in another program in the future easily,you can put the function in a separate "sourcefile2.cpp" and
    Code:
    #include "sourcefile2.cpp"
    in your main source file after all necerary #include <> includes

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by I C everything View Post
    for bigger projects or if you make a great function you want include in another program in the future easily,you can put the function in a separate "sourcefile2.cpp" and
    Code:
    #include "sourcefile2.cpp"
    in your main source file after all necerary #include <> includes
    Only if you wish to do thing the wrong/or at least the way off normal way.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    What stahta01 means is that you shouldn't "#include" source files. Every source file should be its own translation unit. The proper way of doing it, if you want to define functions in another source file, is to include a header file with the function prototypes.
    Devoted my life to programming...

  7. #7
    Registered User I C everything's Avatar
    Join Date
    Apr 2019
    Posts
    101
    Quote Originally Posted by GReaper View Post
    What stahta01 means is that you shouldn't "#include" source files. Every source file should be its own translation unit. The proper way of doing it, if you want to define functions in another source file, is to include a header file with the function prototypes.
    sorry,not used to create my own header files and I am used to structure big source code into several files or code directly functions in a separate source file that while its developed and can be also be used somewhere else

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    *.cpp is a c++ file - I think that you mean *.c

    There are a few small differences between the two languages that might catch you out

    ... But as everyone else has said, including *.h is the way to go
    Last edited by Click_here; 04-18-2019 at 05:35 PM.

  9. #9
    Registered User
    Join Date
    Apr 2019
    Posts
    62
    Quote Originally Posted by I C everything View Post
    for bigger projects or if you make a great function you want include in another program in the future easily,you can put the function in a separate "sourcefile2.cpp" and
    Code:
    #include "sourcefile2.cpp"
    in your main source file after all necerary #include <> includes
    This is valid, but not recommended. If you're compiling from the command line, just list both .c files when you compile. If you're working in an IDE, just add both .c files to the project. There are legit reasons to include a .c file, but none of them are relevant for a beginner. This whole discussion is irrelevant to the person asking the question as well, and is probably only confusing matters.

  10. #10
    Registered User I C everything's Avatar
    Join Date
    Apr 2019
    Posts
    101
    Quote Originally Posted by gaxio View Post
    This is valid, but not recommended. If you're compiling from the command line, just list both .c files when you compile. If you're working in an IDE, just add both .c files to the project. There are legit reasons to include a .c file, but none of them are relevant for a beginner. This whole discussion is irrelevant to the person asking the question as well, and is probably only confusing matters.
    sorry,I have habits from other languages without .h files,also prefer to write a library of functions in separate source file,beside main source file rather than scroll alot

  11. #11
    Registered User
    Join Date
    Apr 2019
    Posts
    62
    Quote Originally Posted by I C everything View Post
    sorry,I have habits from other languages without .h files,also prefer to write a library of functions in separate source file,beside main source file rather than scroll alot
    You can still write them in a different source file, but you compile them separately. Using include on a .c file will cause it to become part of the same compilation unit (the same object file), as well as merge the file scope of both files together. Like I said, it's valid C to do that, but there are implications of building that way that can break things and cause a real mess.

  12. #12
    Registered User I C everything's Avatar
    Join Date
    Apr 2019
    Posts
    101
    Quote Originally Posted by gaxio View Post
    You can still write them in a different source file, but you compile them separately. Using include on a .c file will cause it to become part of the same compilation unit (the same object file), as well as merge the file scope of both files together. Like I said, it's valid C to do that, but there are implications of building that way that can break things and cause a real mess.
    thanks,its nice to show me bad example where you break things and a good example howto prevent break things

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I'm new to C, confuse with something..
    By Milo Juak MuTou in forum C Programming
    Replies: 5
    Last Post: 05-14-2012, 09:38 AM
  2. PayPal confuse me
    By Akkernight in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 04-20-2009, 11:56 AM
  3. a little confuse o_0???
    By omarbags in forum C++ Programming
    Replies: 15
    Last Post: 01-24-2008, 01:03 AM
  4. Too confuse
    By dv007 in forum C++ Programming
    Replies: 6
    Last Post: 07-25-2002, 05:33 PM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM

Tags for this Thread