Functions in C

Discussion in 'C Programming' started by deltaquattro@gmail.com, Jul 24, 2012.

  1. Guest

    Hi,

    it has been quite some time (heck, 10 years!) since I last wrote some C code, and now I need to write an UDF (user defined function) for a commercial software. The UDF is written in plain C, with only three differences:

    1. all the headers of the standard library are included by including the special header udf.h;
    2. some prebuilt macros are available, identified by their names in capital letters;
    3. a new data type, real, is defined, which the commercial software translates to float or double, depending on the user settings (i.e., whether one runs the software in single or double precision).

    Having said that, I would like to know how I define a function in a C source file, so that it can be called by another function in the same file. Specifically, I have:

    /*************************************************************************/
    /* foo.c */
    /*************************************************************************/

    #include "udf.h"
    #define PI 93.1415926536


    real myfun(real x, real y, real t)
    {
    real theta;
    real u0=1.0;
    real omega = 2*PI;
    real u;

    theta = atan2(x,y);
    u = u0+sin(omega*t+theta);
    return u
    }

    How do I instruct another function in the same source file to use myfun? Thanks,

    Best Regards

    deltaquattro
     
    , Jul 24, 2012
    #1
    1. Advertising

  2. Noob Guest

    deltaquattro wrote:

    > #define PI 93.1415926536


    Errr...
     
    Noob, Jul 24, 2012
    #2
    1. Advertising

  3. Eric Sosman Guest

    On 7/24/2012 7:40 AM, wrote:
    > Hi,
    >
    > it has been quite some time (heck, 10 years!) since I last wrote some C code, and now I need to write an UDF (user defined function) for a commercial software. The UDF is written in plain C, with only three differences:
    >
    > 1. all the headers of the standard library are included by including the special header udf.h;
    > 2. some prebuilt macros are available, identified by their names in capital letters;
    > 3. a new data type, real, is defined, which the commercial software translates to float or double, depending on the user settings (i.e., whether one runs the software in single or double precision).
    >
    > Having said that, I would like to know how I define a function in a C source file, so that it can be called by another function in the same file. Specifically, I have:
    >
    > /*************************************************************************/
    > /* foo.c */
    > /*************************************************************************/
    >
    > #include "udf.h"
    > #define PI 93.1415926536


    Adjusted for inflation?

    > real myfun(real x, real y, real t)
    > {
    > real theta;
    > real u0=1.0;
    > real omega = 2*PI;
    > real u;
    >
    > theta = atan2(x,y);
    > u = u0+sin(omega*t+theta);
    > return u


    Missing a semicolon here.

    > }
    >
    > How do I instruct another function in the same source file to use myfun? Thanks,


    Somewhere in the body of the other function, you write
    an expression that calls myfun. The expression will look
    something like myfun's own calls to atan2 and to sin, except
    with three arguments.

    --
    Eric Sosman
    d
     
    Eric Sosman, Jul 24, 2012
    #3
  4. Guest

    Il giorno martedì 24 luglio 2012 13:58:32 UTC+2, Eric Sosman ha scritto:
    > On 7/24/2012 7:40 AM, deltaquattro wrote:
    > > Hi,
    > >
    > > it has been quite some time (heck, 10 years!) since I last wrote some C code, and now I need to write an UDF (user defined function) for a commercial software. The UDF is written in plain C, with only three differences:
    > >
    > > 1. all the headers of the standard library are included by includingthe special header udf.h;
    > > 2. some prebuilt macros are available, identified by their names in capital letters;
    > > 3. a new data type, real, is defined, which the commercial software translates to float or double, depending on the user settings (i.e., whether one runs the software in single or double precision).
    > >
    > > Having said that, I would like to know how I define a function in a C source file, so that it can be called by another function in the same file. Specifically, I have:
    > >
    > > /*************************************************************************/
    > > /* foo.c */
    > > /*************************************************************************/
    > >
    > > #include "udf.h"
    > > #define PI 93.1415926536
    >
    > Adjusted for inflation?


    AHAHAHAHAHAH, oh man, you made me laugh so much! And given the current economic trend, I should really have adjusted it for deflation (I'm from Europe;)

    >
    > > real myfun(real x, real y, real t)
    > > {
    > > real theta;
    > > real u0=1.0;
    > > real omega = 2*PI;
    > > real u;
    > >
    > > theta = atan2(x,y);
    > > u = u0+sin(omega*t+theta);
    > > return u
    >
    > Missing a semicolon here.


    whoops! slip of the keyboard, again.

    >
    > > }
    > >
    > > How do I instruct another function in the same source file to use myfun? Thanks,
    >
    > Somewhere in the body of the other function, you write
    > an expression that calls myfun. The expression will look
    > something like myfun's own calls to atan2 and to sin, except
    > with three arguments.
    >


    Ok! Very simple, then. I sort of recalled you would have to include a file containing the function headers. Either I was wrong, or maybe that was required only when referring functions defined in other source files.

    > --
    > Eric Sosman
    > d


    Thanks,

    deltaquattro
     
    , Jul 24, 2012
    #4
  5. writes:

    > Il giorno martedì 24 luglio 2012 13:58:32 UTC+2, Eric Sosman ha scritto:

    <snip>
    >> &gt; real myfun(real x, real y, real t)
    >> &gt; {
    >> &gt; real theta;
    >> &gt; real u0=1.0;
    >> &gt; real omega = 2*PI;
    >> &gt; real u;
    >> &gt;
    >> &gt; theta = atan2(x,y);
    >> &gt; u = u0+sin(omega*t+theta);
    >> &gt; return u
    >> &gt; }
    >> &gt;
    >> &gt; How do I instruct another function in the same source file to use myfun? Thanks,
    >>
    >> Somewhere in the body of the other function, you write
    >> an expression that calls myfun. The expression will look
    >> something like myfun's own calls to atan2 and to sin, except
    >> with three arguments.


    The new Google groups messes up (in new and surprising ways) when
    posting to Usenet (this is a Usenet group, by the way). If you switch
    to the old interface (or, better, get a real newsreader) we won't see
    all these &gt; and ' translations.

    > Ok! Very simple, then. I sort of recalled you would have to include a
    > file containing the function headers. Either I was wrong, or maybe
    > that was required only when referring functions defined in other
    > source files.


    What you need is that at the point of call, the compiler has seen what
    is call a prototype for the function. It declares the return type and
    the types of the parameters. The definition that you've given for the
    function act as a prototype, so if the call is below myfun (or, indeed,
    in it) you've fine. If you want to write the call in another function
    that comes before the definition of myfunc you either have to move that
    definition of add this line

    real myfun(real x, real y, real t);

    at the top of the file (that's the usual place to put it).

    The reason you are thinking of include files, is that one role they
    often play is to contain a bunch of the prototypes.

    --
    Ben.
     
    Ben Bacarisse, Jul 24, 2012
    #5
  6. Eric Sosman Guest

    On 7/24/2012 8:22 AM, wrote:
    >[...]
    >> theta = atan2(x,y);


    Aside: Pay close attention to the order of the arguments
    of atan2. What you've written may in fact be what you intend
    (for example, if North is zero and East is pi/2, with angles
    increasing clockwise instead of counter-clockwise), but take a
    second look to be sure, okay?

    --
    Eric Sosman
    d
     
    Eric Sosman, Jul 24, 2012
    #6
  7. Guest

    Il giorno martedì 24 luglio 2012 14:44:34 UTC+2, Eric Sosman ha scritto:
    > &gt;[...]
    > &gt;&gt; theta = atan2(x,y);
    >
    > Aside: Pay close attention to the order of the arguments
    > of atan2. What you've written may in fact be what you intend
    > (for example, if North is zero and East is pi/2, with angles
    > increasing clockwise instead of counter-clockwise), but take a
    > second look to be sure, okay?


    Shit! I intended it to be the other way round, i.e.

    theta(x=1,y=0)=0
    theta(x=0,y=1)=pi/2

    so it should be theta=atan2(y,x). Thanks for pointing that out!

    >
    > --
    > Eric Sosman
    > d
     
    , Jul 24, 2012
    #7
  8. ralph Guest

    On Tue, 24 Jul 2012 20:41:42 -0500, ralph <>
    wrote:


    >>
    >>Ok! Very simple, then. I sort of recalled you would have
    >>to include a file containing the function headers. Either I was
    >>wrong, or maybe that was required only when referring
    >> functions defined in other source files.
    >>

    >
    >While appearing as over-kill for just one function and as you said the
    >same source file, IMHO, you are still better off to include prototypes
    >in a separate header file with include guards. Just a good habit.
    >
    >Cuts down on the opportunities for multiple define errors and typos,
    >and simply because projects always seem to grow. One function, one
    >source file now, two a little later, four next week, another project
    >... <g>
    >
    >// MyFunctions.h
    >#ifndef MYFUNCTIONS_H_INCLUDED
    >#define MYFUNCTIONS_H_INCLUDED
    >
    >real myfun(real x, real y, real t) ;
    >
    >#endif
    >// end header
    >
    >// some_source.c
    >#include "MyFunctions.h"
    >
    >This is in no way contrary to Eric Sosman's spot on advice.
    >
    >-ralph
     
    ralph, Jul 25, 2012
    #8
  9. aftnix Guest

    On Tuesday, July 24, 2012 5:40:58 PM UTC+6, (unknown) wrote:
    > Hi,
    >
    > it has been quite some time (heck, 10 years!) since I last wrote some C code, and now I need to write an UDF (user defined function) for a commercial software. The UDF is written in plain C, with only three differences:
    >


    What language you have been coding on? excluding functional languages, most of the languages inherited some syntax from c , at least function calling convention!

    > 1. all the headers of the standard library are included by including the special header udf.h;
    > 2. some prebuilt macros are available, identified by their names in capital letters;
    > 3. a new data type, real, is defined, which the commercial software translates to float or double, depending on the user settings (i.e., whether one runs the software in single or double precision).
    >
    > Having said that, I would like to know how I define a function in a C source file, so that it can be called by another function in the same file. Specifically, I have:
    >
    > /*************************************************************************/
    > /* foo.c */
    > /*************************************************************************/
    >
    > #include &quot;udf.h&quot;
    > #define PI 93.1415926536
    >
    >
    > real myfun(real x, real y, real t)
    > {
    > real theta;
    > real u0=1.0;
    > real omega = 2*PI;
    > real u;
    >
    > theta = atan2(x,y);
    > u = u0+sin(omega*t+theta);
    > return u
    > }
    >
    > How do I instruct another function in the same source file to use myfun? Thanks,
    >
    > Best Regards
    >
    > deltaquattro
     
    aftnix, Jul 26, 2012
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tshad
    Replies:
    11
    Views:
    843
    tshad
    May 27, 2005
  2. Timothy Wong
    Replies:
    3
    Views:
    430
    Timothy Wong
    May 20, 2004
  3. ILLOGIC
    Replies:
    1
    Views:
    379
    Rob Williscroft
    Jun 1, 2004
  4. Xiangliang Meng
    Replies:
    1
    Views:
    1,652
    Victor Bazarov
    Jun 21, 2004
  5. Steven T. Hatton

    Passing member functions to C functions?

    Steven T. Hatton, Oct 4, 2004, in forum: C++
    Replies:
    7
    Views:
    1,329
    David Hilsee
    Oct 7, 2004
Loading...

Share This Page