# "delta function" at a - C

This is a discussion on "delta function" at a - C ; For a while today, I thought I needed a function delta(int p) that would return a function of int t whose value is 1 if p equals t and 0 if not. I figured out how not to need it, ...

1. ## "delta function" at a

For a while today, I thought I needed a function delta(int p) that
would return a function of int t whose value is 1 if p equals t and
0 if not. I figured out how not to need it, since I was having a lot
of trouble writing it. But I'm still interested in knowing how it would
be done. I tried declaring it

int (delta(int p))(int t)

and hopefully that is right. The reason I was trying to do this was that
I wanted to be able to use the function delta(p) of t as an argument to
another function. If you know how to do this, please let me know. Thanks.
--
Ignorantly,
* Disclaimer: I am a guest and *not* a member of the MIT CSAIL. My actions and
* comments do not reflect in any way on MIT. Also, I am nowhere near Boston.
--
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
or the newsgroup name in square brackets in the subject line. Sorry.

2. ## Re: "delta function" at a

"Allan Adler" <ara@nestle.csail.mit.edu> wrote in message
news:clcm-20060917-0056@plethora.net...
> For a while today, I thought I needed a function delta(int p) that
> would return a function of int t whose value is 1 if p equals t and
> 0 if not. I figured out how not to need it, since I was having a lot
> of trouble writing it. But I'm still interested in knowing how it would
> be done. I tried declaring it
> int (delta(int p))(int t)
> and hopefully that is right. The reason I was trying to do this was that
> I wanted to be able to use the function delta(p) of t as an argument to
> another function. If you know how to do this, please let me know. Thanks.

Actually in C one has to return a pointer to the function, not a function.

However, you seem to be confusing C with mathematics. There is no
such thing in C as a "function of int t". Parameter names are merely
placeholders and have no persistent meaning. So if there is some
function delta taking a single argument of type int, that argument
cannot be compared to some other argument that is not declared
as an argument to the same function. The closest you can get is to
set aside a static object to hold the value of p, which the delta
function could then refer to when passed a value of t.
A more conventional approach is to use a 2-argument function
int delta(int p, int t).
--
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
or the newsgroup name in square brackets in the subject line. Sorry.

3. ## Re: "delta function" at a

> For a while today, I thought I needed a function delta(int p) that
> would return a function of int t whose value is 1 if p equals t and
> 0 if not.

No way. C functions can return functions, only function pointers.
And those have to be compiled before you can take their address.
Generally, what you're trying can not be done in any statically

--
Hans-Bernhard Broeker (broeker@physik.rwth-aachen.de)
Even if all the snow were burnt, ashes would remain.
--
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
or the newsgroup name in square brackets in the subject line. Sorry.

4. ## Re: "delta function" at a

> For a while today, I thought I needed a function delta(int p) that
> would return a function of int t whose value is 1 if p equals t and
> 0 if not. I figured out how not to need it, since I was having a lot
> of trouble writing it. But I'm still interested in knowing how it would
> be done. I tried declaring it
>
> int (delta(int p))(int t)
>
> and hopefully that is right. The reason I was trying to do this was that
> I wanted to be able to use the function delta(p) of t as an argument to
> another function. If you know how to do this, please let me know. Thanks.

You can do this in standard C only for special cases.
1. At any given time only one such function will be used - set a global
static variable delta_t, and always pass the same 'delta' function.
2. These functions use only a small finite number of possible values of
't'. Write down all these functions and return the right function ptr
searching in an array of these possible 't's.
3. A combination of 1 and 2 - at any given time only a small finite
number of values of 't' is used. Keep a bi-directional map between
these 't's and their function ptrs. Every function will find its 't'
using this map, and for every 't' you'll allways find the corresponding
function.

The problem is that in standard C all the functions that you use are
known in advance, and if you have only 50 functions, you cannot use 200
different 'delta' functions in your program at the same time.

Some non-standard or non-C solutions:
1. Use nested functions of GCC. Remember that you cannot use the value
of the nested function after return from the enclosing function.
2. Use function objects of C++. This is the best solution.
3. Create these functions as data, wih a 't' inside, and return a ptr
to them. This is what some functional languages do. This is not
portable, but for every machine and calling convention you can easily
find out the layout of these simple functions. This can be made almost
portable compiling a sample function, ****yzing its assembly output,
and creating a .c file containing the data and the offset of 't' in the
function using a script. For a process where the stack/data pages are
not executable this wouldn't work.
4. Create a small .c file with this function (using fprintf),
compile+link it into a shared library, load this library and get a
function ptr. Don't forget to erase these libraries on exit.

P.S. I don't know if your prototype is right. I always do typedef for
every function ptr I use, this is much more readable for me and most