473,548 Members | 2,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(doub le * a1, double * a2)
{
...
}

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

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

Nov 14 '05 #1
5 2584
Rolf Wester <ro*********@il t.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(doub le * a1, double * a2)
{
...
} void my_qsort(double * a, int n, (* comp_func))
{ ???????


void my_sqsort(doubl e *a, int n, int (*comp_func)(do uble *a1, double *a2))

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"My absolute aspect is probably..."
- Mato Valtonen
Nov 14 '05 #2
Joona I Palaste wrote:
Rolf Wester <ro*********@il t.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(doub le * a1, double * a2)
{
...
}


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

void my_sqsort(doubl e *a, int n, int (*comp_func)(do uble *a1, double *a2))

Thank you very much.

Rolf
Nov 14 '05 #3
On Mon, 08 Nov 2004 12:13:01 +0100, Rolf Wester
<ro*********@il t.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*********@il t.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(doub le * a1, double * a2)
{
...
}

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

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

Nov 14 '05 #5
On 8 Nov 2004 11:19:05 GMT, Joona I Palaste <pa*****@cc.hel sinki.fi>
wrote:
Rolf Wester <ro*********@il t.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(doubl e *a, int n, int (*comp_func)(do uble *a1, double *a2))


Or you can omit the parameter names, if you prefer:
void my_sqsort (double *a, int n,
int (*comp_func)(do uble *, 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.ne t
Nov 14 '05 #6

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

Similar topics

1
2104
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 automatically builds querystrings for my redirect URLS. The code for this follows. It defines an ASP Dictionary object, and key/value pairs for each,...
2
3764
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 produce two messages having the same message digest. That conjecture is false, as demonstrated by Wang, Feng, Lai and Yu in 2004 . Just recently,...
4
3803
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
3341
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 manipulation routines in my Class: ******************************************************************************* imports System NameSpace MyFunctions
0
5381
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 well-known example is the use of CallWindowProc to call a function gotten via LoadLibrary/GetProcAddress. For example, I've seen code similar to the...
5
1732
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; // function object
13
2507
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 functions in common implementations? And where is the 'this' ptr tucked away at for POD structs with member functions? John
0
6999
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 OBJECT WITHIN THE DATABASE. 2.IT MUST RETURN A VALUE TO THE CALLING PROCEDURE. 3.ONCE A FUNCTION IS CREATED IT BECOMES PART OF THE DUAL TABLE. 4.IF...
11
2561
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 modules, but for some reason it only works using the "import <mod>" syntax, and not "from <modimport *" syntax... i.e. in the latter case, the...
2
5315
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. The long story: I have been trying to develop a data driven method to call C++ functions, that allows calling them with minimal overhead. The...
0
7518
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7711
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7954
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7805
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5367
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5085
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3497
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3478
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
755
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.