473,396 Members | 1,765 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Hiding paramter in functions

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,

Mar 2 '07 #1
6 1388
In article <11**********************@8g2000cwh.googlegroups.c om>,
Ivan <da*******@gmail.comwrote:
>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.

Mar 2 '07 #2
Ivan wrote:
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.

--
Chris "electric hedgehog" Dollin
"Go not to the Elves for counsel, for they will answer both no and yes"
/The Lord of the Rings/

Mar 2 '07 #3
Ivan wrote:
>
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)

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Mar 2 '07 #4
CBFalconer <cb********@yahoo.comwrites:
[...]
#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

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 2 '07 #5
Keith Thompson wrote:
CBFalconer <cb********@yahoo.comwrites:
[...]
> #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
Yup, editing foulup. I started by using getchar, and then decided
putchar made a better example.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Mar 2 '07 #6
"Ivan" <da*******@gmail.comwrote:

# 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).

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I hope it feels so good to be right. There's nothing more
exhilarating pointing out the shortcomings of others, is there?
Mar 3 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Lorenzo Villari | last post by:
I premise I don't know C++ well but... I wondered what is this data hiding thing... I mean, if I can look at the header (and i need it beacuse of the class), then what's hidden? Can someone give...
17
by: Bob Weiner | last post by:
What is the purpose of hiding intead of overriding a method? I have googled the question but haven't found anything that makes any sense of it. In the code below, the only difference is that...
9
by: bob | last post by:
Hi, I know there exists a good reason why the designers of c++ decided that function hiding should exist. But I don't know why. Can anybody provide a good reason/example of a case where function...
5
by: Miro | last post by:
This qustion is probably for people who have created large apps with subs / or functions that have a lot of parameters and used in a lot of places in ur whole app. ( lets say its ur own library...
2
by: subramanian100in | last post by:
Is my following understanding correct ? Data abstraction means providing the interface - that is, the set of functions that can be called by the user of a class. Information hiding means...
2
by: developer.new | last post by:
Hi I have a question regarding this concept I learned about recently: Name Hiding. Here's what I've come across: There is a base class with two functions with the same name but different...
13
by: mattia | last post by:
Hi everybody, I'm wondering how to realize a simple pattern in C: information hiding, to hide detail implementations of a data structure. How can I do that? I've also read that is preferred to use...
162
by: Sh4wn | last post by:
Hi, first, python is one of my fav languages, and i'll definitely keep developing with it. But, there's 1 one thing what I -really- miss: data hiding. I know member vars are private when you...
27
by: matt | last post by:
Hello group, I'm trying to become familiar with the information hiding design rules, and I have a lot (3) of questions for all you experts. AFAIK, a generic module has 2 files: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.