Hiding paramter in functions

I

Ivan

Hi,

I have a function that takes, say, two varialbles:

double g_fn(const double x, const double t)
{
...............
}

In another function I want to use g_fn as an single-parameter
function, like
gg_fn(x)

Is there a shorter or better way than to redefine
double gg_fn(x)
{
extern t;
return g_fn(x,t);
}

and calling "t" as a global parameter?

regards,
 
K

Kenny McCormack

Hi,

I have a function that takes, say, two varialbles:

double g_fn(const double x, const double t)
{
..............
}

In another function I want to use g_fn as an single-parameter
function, like
gg_fn(x)

Is there a shorter or better way than to redefine
double gg_fn(x)
{
extern t;
return g_fn(x,t);
}

and calling "t" as a global parameter?

You can probably get there with the varargs (or whatever they are
calling it this week) stuff.
 
C

Chris Dollin

Ivan said:
Hi,

I have a function that takes, say, two varialbles:

double g_fn(const double x, const double t)
{
..............
}

In another function I want to use g_fn as an single-parameter
function, like
gg_fn(x)

Is there a shorter or better way than to redefine
double gg_fn(x)
{
extern t;
return g_fn(x,t);
}

and calling "t" as a global parameter?

Not really, no. C doesn't support higher-order functions [1].
Messing around with stdargs is /possible/ but almost certainly
won't be worth it [2].

If you routinely want to do this, then you could define a
struct with a [pointer to a] function and frozen argument in it,
and then have a `call` function so that

call( fn, y ) is fn->f( fn->x, y )

However, C's type system isn't very helpful here, since you'd
need either a different struct (and version of `call`) for each
function signature, or lots of messing around with void*s.

Either pick a different language, or tell us what you're trying
to do, and we'll see if there's a more C-friendly way of doing
it.

[1] Well, it does just a /tiny/ bit - function pointers.

[2] Every function that plays this game will need to have
an early argument to say how many arguments you've supplied,
and pick up the "extra" arguments using the valist machinery.
 
C

CBFalconer

Ivan said:
I have a function that takes, say, two varialbles:

double g_fn(const double x, const double t)
{
..............
}

In another function I want to use g_fn as an single-parameter
function, like
gg_fn(x)

Is there a shorter or better way than to redefine
double gg_fn(x)
{
extern t;
return g_fn(x,t);
}

and calling "t" as a global parameter?

#define gg_fn(x) g_fn(x, constant_value_of_t)

just like the normal definition of putchar() is:

#define putchar(c) putc(c, stdin)
 
K

Keith Thompson

CBFalconer said:
#define gg_fn(x) g_fn(x, constant_value_of_t)

just like the normal definition of putchar() is:

#define putchar(c) putc(c, stdin)

stdout
 
S

SM Ryan

# Is there a shorter or better way than to redefine
# double gg_fn(x)
# {
# extern t;
# return g_fn(x,t);
# }

Not in standard C. In non-standard C, if your heap pages are
writable and executable, you can dynamically construct a small
function in the heap that adds parameters to a call to
another function. It's very hardware dependent, but if the vm
allows it, it not that hard to define function like
int f(int a,int b;
int (*g)(int) = addArgInt(f,q)
that makes g(z) call f(z,q) or f(q,z).
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top