473,396 Members | 1,968 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.

Functions as function parameter

Hi,

I want to pass a C-function as a function parameter but I don't know
how to that correctly. In the example below how would I have to declare
the function argument in the my_sort function definition?
Thank you in advance for any help.

Regards

Rolf

int comp_func1(double * a1, double * a2)
{
...
}

void my_qsort(double * a, int n, (* comp_func))
{ ???????

...
comp_func(&a[i], &a[j]);
...
}
int main()
{
...
my_qsort(a,n,comp_func1);
}

Nov 14 '05 #1
5 2580
Rolf Wester <ro*********@ilt.fraunhofer.de> scribbled the following:
Hi, I want to pass a C-function as a function parameter but I don't know
how to that correctly. In the example below how would I have to declare
the function argument in the my_sort function definition?
Thank you in advance for any help. Regards Rolf int comp_func1(double * a1, double * a2)
{
...
} void my_qsort(double * a, int n, (* comp_func))
{ ???????


void my_sqsort(double *a, int n, int (*comp_func)(double *a1, double *a2))

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"My absolute aspect is probably..."
- Mato Valtonen
Nov 14 '05 #2
Joona I Palaste wrote:
Rolf Wester <ro*********@ilt.fraunhofer.de> scribbled the following:
Hi,


I want to pass a C-function as a function parameter but I don't know
how to that correctly. In the example below how would I have to declare
the function argument in the my_sort function definition?
Thank you in advance for any help.


Regards


Rolf


int comp_func1(double * a1, double * a2)
{
...
}


void my_qsort(double * a, int n, (* comp_func))
{ ???????

void my_sqsort(double *a, int n, int (*comp_func)(double *a1, double *a2))

Thank you very much.

Rolf
Nov 14 '05 #3
On Mon, 08 Nov 2004 12:13:01 +0100, Rolf Wester
<ro*********@ilt.fraunhofer.de> wrote:
Hi,

I want to pass a C-function as a function parameter but I don't know
You cannot pass a function as an argument in a function call. You
can, however, pass a pointer to function which will probably let you
do everything you had in mind originally.

If that is your intent, look up qsort and see how it declares the
function it receives as an argument. Most C manuals usually describe
how to set such a function up also.
how to that correctly. In the example below how would I have to declare
the function argument in the my_sort function definition?


snip
<<Remove the del for email>>
Nov 14 '05 #4
Rolf Wester <ro*********@ilt.fraunhofer.de> wrote in message news:<41********@news.fhg.de>...
Hi,

I want to pass a C-function as a function parameter but I don't know
how to that correctly. In the example below how would I have to declare
the function argument in the my_sort function definition?
Thank you in advance for any help.
First Of always remember that you can not pass the function as the
parameter to another function as the argument. Rather we Pass Only the
address (Pointer) of the Fucntion as an argument.
Regards

Rolf

int comp_func1(double * a1, double * a2)
{
...
}

void my_qsort(double * a, int n, (* comp_func)) Void my_qsort(double *a, int n, (*comp_func)(double * a1, double * a2)) { ???????

...
comp_func(&a[i], &a[j]);
...
}
int main()
{
...
my_qsort(a,n,comp_func1);
}

Nov 14 '05 #5
On 8 Nov 2004 11:19:05 GMT, Joona I Palaste <pa*****@cc.helsinki.fi>
wrote:
Rolf Wester <ro*********@ilt.fraunhofer.de> scribbled the following:
I want to pass a C-function as a function parameter but I don't know
how to that correctly. In the example below how would I have to declare
the function argument in the my_sort function definition?

<snip> void my_sqsort(double *a, int n, int (*comp_func)(double *a1, double *a2))


Or you can omit the parameter names, if you prefer:
void my_sqsort (double *a, int n,
int (*comp_func)(double *, double *) )
Since the parameter names aren't used/usable within this function
(only the function to which this parameter points) this is arguably
clearer; however it differs from the target function definition (even)
if that is (also) in prototype format which you may dislike.

Or you can omit the parameter description entirely:
void my_sqsort (double *a, int n, int (*comp_func) () )
which is less safe as it doesn't (at least isn't required to) check
compatibility of calling signatures, but more powerful as it allows
you to pass functions (pointers) with differing signatures -- and you
must take responsibility for ensuring that you call them correctly.
- David.Thompson1 at worldnet.att.net
Nov 14 '05 #6

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

Similar topics

1
by: Ken Fine | last post by:
I have a menu system that has nodes that can be opened or closed. In an effort to make my code more manageable, I programmed a little widget tonight that keeps track of the open/active item and...
2
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to...
4
by: sam1967 | last post by:
How do I get a function to return a GMP integer type mpz_t when i try it i get an error message. i am trying mpz_t hooch (int x) { mpz_t y; ........
11
by: tshad | last post by:
I am setting up some of my functions in a class called MyFunctions. I am not clear as to the best time to set a function as Shared and when not to. For example, I have the following bit...
0
by: Mike S | last post by:
I've seen examples of using the CallWindowProc Windows API function to call functions through their addresses in VB6 -- a clever workaround to emulate C-like function pointer semantics. A...
5
by: krishnaroskin | last post by:
Hey all, I've been running into a problem with default values to template'd functions. I've boiled down my problem to this simple example code: #include<iostream> using namespace std; //...
13
by: JohnQ | last post by:
The implementation of classes with virtual functions is conceptually easy to understand: they use vtables. Which begs the question about POD structs: how are they associated with their member...
0
debasisdas
by: debasisdas | last post by:
This thread contains some useful tips/samples regarding FUNCTIONS in oracle, that the forum members may find useful. FUNCTION: ========== 1.IT IS A COMPILED BLOCK OF CODE WHICH IS STORED AS AN...
11
by: Nadeem | last post by:
Hello all, I'm trying to write a function that will dynamically generate other functions via exec. I then want to be able to import the file (module) containing this function and use it in other...
2
by: unauthorized | last post by:
The short story: I need to be able to cast a function pointer for any function and class to an intermediate type, so I could call it from any point of my program using the "__asm call" instruction....
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.