Thread: Functions without declaring

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    61

    Functions without declaring

    Hi There,

    I wonder if anyone can help lol

    Ive got some code which complies (visual studio 2008)

    It has the following functions in them.
    Code:
    // Transfer data to can buffer
                CanSetData( Port, SendData, 0, 8 );
    
    
    // Send to current port
                CanSetId( Port, 0, Id.l, NumBytes );
    these to me just seem normal functions however they aren't declared anywhere in my code and i only have #include <stdio.h>.

    Are these features of C, (i have googled but not found anything)

    Thanks In Advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by r_james14
    these to me just seem normal functions however they aren't declared anywhere in my code and i only have #include <stdio.h>.

    Are these features of C
    Yes, it is a feature, or perhaps a "mis-feature" of C. A function that is called before its declaration is seen is assumed to have parameters of type int and a return type of int. This may not hold true, so you are advised to always declare your functions before calling them. (Of course, feel free to define them after they have been called.)
    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
    Nov 2011
    Posts
    61
    okay i been looking into it more, i now have errors:

    Error 2 error C2065: 'CanSetId' : undeclared identifier c:\users\james rounds\documents\visual studio 2008\projects\playing 3\playing 3\playing 3.c 85 Playing 3

    I have no idea why these didn't come up before.

    Anyways on the original code there are #include .... which i dont recognise such as #include <Can/CanSpc.h> for example.

    Is it possible to call a function in the include files and then use it without declaring?
    Also what are these called so i know what to google, how do i go about making them, whats the process called?

    Thanks
    James

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by r_james14
    Is it possible to call a function in the include files and then use it without declaring?
    What do you mean by "call a function in the include files"? Why would you want to use a function before it is declared?
    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

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    61
    I've been given some code to develop/learn from. It is so badly wrote and annotated its taken me 3 weeks to simply work out what on earth is going on. The sample of code I gave in my original post isn't defined in the code I have in front of me, neither at the top no:

    void CanSetData( int Port,int SendData);

    For example
    Also there is nowhere the function is called, so again it doesn't say in my code what it actually does.

    Is it possible to define a function and say what it does and somehow package it up in a #include part. I have no knowledge on "header files" i think they called at all.

    Im not trying to write like this, i'm trying to understand so i can do it properly.


    Thanks

    James

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by r_james14 View Post
    I've been given some code to develop/learn from. It is so badly wrote and annotated its taken me 3 weeks to simply work out what on earth is going on. The sample of code I gave in my original post isn't defined in the code I have in front of me, neither at the top no:

    void CanSetData( int Port,int SendData);

    For example
    Also there is nowhere the function is called, so again it doesn't say in my code what it actually does.

    Is it possible to define a function and say what it does and somehow package it up in a #include part. I have no knowledge on "header files" i think they called at all.

    Im not trying to write like this, i'm trying to understand so i can do it properly.


    Thanks

    James
    You use headers like stdio.h and stdlib.h all the time... yes?
    So open one of them in your IDE and have a look at it...
    That should give you some idea how it works.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    61
    daft question but how do I do that?

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by r_james14 View Post
    daft question but how do I do that?
    The same way you open any file in your IDE.

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    61
    before i look completly mad am i correct in thinking the correct header files have been made?

    #include <Can/Canmon.h>
    #include <Can/Candat.h>
    #include <Can/CanSpc.h>
    #include <Can/CanOpen.h>
    #include <Can/CanOpenLog.h>
    #include <AlgoP.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <p-sysutils.h>
    #include <p-configutils.h>

    i have googled but found no info for any of them.

    Thanks

    James

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    That will depend entirely on the programming...

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by r_james14 View Post
    I've been given some code to develop/learn from. It is so badly wrote and annotated its taken me 3 weeks to simply work out what on earth is going on.
    Why do you want to do that?

    Also there is nowhere the function is called, so again it doesn't say in my code what it actually does.

    Is it possible to define a function and say what it does and somehow package it up in a #include part. I have no knowledge on "header files" i think they called at all.
    Unless there are comments, the header will not tell you any more about the function than the prototype declaration, because that's all it will contain WRT to this function. Very likely, the complete definition of the function is in a source file that may not be installed on your system. They work because they are already compiled into libraries. The #include header allows you to make use of functions in the library.

    Ie, if all you have is that declaration without documentation or the definition source, all you can do is guess.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Declaring trig functions?
    By Raen in forum C Programming
    Replies: 6
    Last Post: 11-06-2010, 05:55 PM
  2. declaring and using functions in a class
    By quiet_forever in forum C++ Programming
    Replies: 1
    Last Post: 09-16-2007, 02:22 PM
  3. Syntax for Declaring Functions in Dev-C++
    By Muz in forum C Programming
    Replies: 4
    Last Post: 07-03-2007, 04:53 AM
  4. Declaring multiple functions
    By samuel in forum C++ Programming
    Replies: 1
    Last Post: 10-22-2001, 12:15 AM
  5. Declaring Functions
    By Thantos in forum C Programming
    Replies: 3
    Last Post: 09-25-2001, 10:24 PM