Functions in C

D

deltaquattro

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
 
E

Eric Sosman

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.
 
D

deltaquattro

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.

Thanks,

deltaquattro
 
B

Ben Bacarisse

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

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 > 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.
 
E

Eric Sosman

[...]
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?
 
D

deltaquattro

Il giorno martedì 24 luglio 2012 14:44:34 UTC+2, Eric Sosman ha scritto:
>[...]
>> 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!
 
R

ralph

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
 
A

aftnix

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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top