Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Functions in C (http://www.velocityreviews.com/forums/t948482-functions-in-c.html)

deltaquattro@gmail.com 07-24-2012 11:40 AM

Functions in C
 
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

Noob 07-24-2012 11:56 AM

Re: Functions in C
 
deltaquattro wrote:

> #define PI 93.1415926536


Errr...


Eric Sosman 07-24-2012 11:58 AM

Re: Functions in C
 
On 7/24/2012 7:40 AM, deltaquattro@gmail.com 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
esosman@ieee-dot-org.invalid

deltaquattro@gmail.com 07-24-2012 12:22 PM

Re: Functions in C
 
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
> esosman@ieee-dot-org.invalid


Thanks,

deltaquattro

Ben Bacarisse 07-24-2012 12:38 PM

Re: Functions in C
 
deltaquattro@gmail.com 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.

Eric Sosman 07-24-2012 12:44 PM

Re: Functions in C
 
On 7/24/2012 8:22 AM, deltaquattro@gmail.com 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
esosman@ieee-dot-org.invalid

deltaquattro@gmail.com 07-24-2012 08:17 PM

Re: Functions in C
 
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?


****! 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
> esosman@ieee-dot-org.invalid



ralph 07-25-2012 01:43 AM

Re: Functions in C
 
On Tue, 24 Jul 2012 20:41:42 -0500, ralph <nt_consulting64@yahoo.com>
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


aftnix 07-26-2012 08:34 PM

Re: Functions in C
 
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




All times are GMT. The time now is 07:31 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.