473,793 Members | 2,810 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

function prototypes and function addresses

Hi,
is it possible to write two functions which both require a function as
an argument and both being able to use the other function as an
argument? Afaik the address of a function is not known upon declaration
but only once its defined.

Thanks
Jan 9 '08 #1
6 1640
In article <47************ ***********@new sspool1.arcor-online.net>,
Syren Baran <sy***@gmx.dewr ote:
>is it possible to write two functions which both require a function as
an argument and both being able to use the other function as an
argument?
Yes.
>Afaik the address of a function is not known upon declaration
but only once its defined.
The address of a function is not known until link time, or
possibly not until run time.

But that doesn't really matter, because you asked about being
able to use the other function as an argument, which is something
that happens at run time, not at compile time. You did not ask
that each has static calls to each other.

For example,

int fun1(int (*auxfun)()) { return (auxfun == NULL) ? 5 : 17 * auxfun(NULL); }

int fun2(int (*auxfun)()) { return (auxfun == NULL) ? 42 : 9 - auxfun(NULL); }

int fun3(void) { return fun1(fun2) + fun2(fun1); }
--
"There are some ideas so wrong that only a very intelligent person
could believe in them." -- George Orwell
Jan 9 '08 #2
On Jan 9, 7:43 pm, Syren Baran <sy...@gmx.dewr ote:
Hi,
is it possible to write two functions which both require a function as
an argument and both being able to use the other function as an
argument? Afaik the address of a function is not known upon declaration
but only once its defined.
It is not about the address, but the prototype of the function.
After a while you will realise it is impossible to write two functions
'foo' and 'bar' that work like this:
--
foo() { bar(foo); }
bar() { foo(bar); }
--
Types/arguments are missing on purpose because as I said it is not
possible to define such functions.
Jan 9 '08 #3
On Wed, 09 Jan 2008 17:53:22 +0000, Walter Roberson wrote:
int fun1(int (*auxfun)()) { return (auxfun == NULL) ? 5 : 17 *
auxfun(NULL); }

int fun2(int (*auxfun)()) { return (auxfun == NULL) ? 42 : 9 -
auxfun(NULL); }

int fun3(void) { return fun1(fun2) + fun2(fun1); }
Whether NULL is defined as an integer or as a pointer, it won't be a
function pointer, and since auxfun lacks a prototype, it won't be
implicitly converted to one either. This is one of the situations where
you should use a cast.

int fun1(int (*auxfun)()) {
return (auxfun == NULL) ? 5 : 17 * auxfun((int(*)( ))NULL);
}

int fun2(int (*auxfun)()) {
return (auxfun == NULL) ? 42 : 9 - auxfun((int(*)( ))NULL);
}

int fun3(void) { return fun1(fun2) + fun2(fun1); }

You could also expand your function pointer declaration:

int fun1(int (*auxfun)(int (*)())) {
return (auxfun == NULL) ? 5 : 17 * auxfun(NULL);
}

int fun2(int (*auxfun)(int (*)())) {
return (auxfun == NULL) ? 42 : 9 - auxfun(NULL);
}

int fun3(void) { return fun1(fun2) + fun2(fun1); }

but the cast is just as easy to type and easier to read, in my opinion.
Jan 9 '08 #4
Walter Roberson schrieb:
>Afaik the address of a function is not known upon declaration
but only once its defined.

The address of a function is not known until link time, or
possibly not until run time.

But that doesn't really matter, because you asked about being
able to use the other function as an argument, which is something
that happens at run time, not at compile time. You did not ask
that each has static calls to each other.
Thanks, that helps. Was unsure about when the address is calculated.
In other words, if something like
declare function1
use function1 as an argument
define function1
doesnt work, the problem lies elsewhere.
>
For example,
Neat :)
Jan 9 '08 #5
Syren Baran wrote:
Hi,
is it possible to write two functions which both require a function as
an argument and both being able to use the other function as an
argument? Afaik the address of a function is not known upon declaration
but only once its defined.
No, functions are not allowed as arguments to C functions. The result
of a function call is a value that can be passed as an argument.
Function pointers can be passed as an argument. But functions
themselves cannot. If it seems otherwise, it's probably because a
function name is automatically converted into a pointer to the named
function in most contexts. If you're talking about function pointers,
then yes, it is possible.

The tricky detail is declaring the types in the function prototypes.
The problem, as you'll find out if you try it, is that fully declaring
the argument types requires infinite recursion. The solution is, at
some point, to take advantage of the fact that C doesn't require you
to declare the argument type - instead of using a function prototype,
use an old-style function declaration that leaves the argument list
unspecified.

It's very easy, when writing code like this, to produce infinitely
recursive function calls. Remember to insert something to prevent
infinite recursion. In the following rather silly code, 'count' serves
that purpose.

#include <stdio.h>
int func1(unsigned count, int(*f)())
{
printf("func1 +%ux\n", count);
if(count && f != NULL)
count = f(count-1, f);
printf("func1 -%ux\n", count);
return count;
}

int func2(unsigned count, int(*f)())
{
printf("func2 +%ux\n", count);
if(count>0 && f!=NULL)
count = f(count-1,func1);
printf("func2 -%ux\n", count);
return count;
}

int main(int argc, char *argv[])
{
func2(argc, func2);
return 0;
}

Note: if you want func1() to be able to refer to func2 by name the
same way that func2 refers to func1, you'll have to forward declare
func2 before defining func1.
Jan 9 '08 #6
vipps...@gmail. com wrote:
On Jan 9, 7:43 pm, Syren Baran <sy...@gmx.dewr ote:
Hi,
is it possible to write two functions which both require a function as
an argument and both being able to use the other function as an
argument? Afaik the address of a function is not known upon declaration
but only once its defined.
It is not about the address, but the prototype of the function.
After a while you will realise it is impossible to write two functions
'foo' and 'bar' that work like this:
--
foo() { bar(foo); }
bar() { foo(bar); }
--
Types/arguments are missing on purpose because as I said it is not
possible to define such functions.
void foo(void(*)());
void bar(void(*)());

void foo(void(*f)()) {bar(foo);}
void bar(void(*f)()) {foo(bar);}
Works for me. It's infinitely recursive, of course, but I assume that
you didn't mean your example to be taken literally; in any useful
context, the would be some mechanism to terminate the recursion before
it became infinite.
Jan 9 '08 #7

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

Similar topics

21
3860
by: Rob Somers | last post by:
Hey people, I read a good thread on here regarding the reason why we use function prototypes, and it answered most of my questions, but I wanted to double check on a couple of things, as I am writing something up on functions, and I don't like writing about things I am not sure about. Ok, then, here we go: I initially thought that one would only really need to use a function
6
7995
by: Daniel Nichols | last post by:
I've noticed that in a C module (.c, .h file combination) that if you create a function's definition before it is used in other functions than a declaration is not necessary. I believe if the compiler can find the definition of the function prior to encountering the use of the function it will generate the prototype itself. I don't currently use this feature, I explicitly create declarations for all functions in a header file. However, I...
7
2380
by: junky_fellow | last post by:
Can a function have two different prototypes ? If not , then how can main() have two different prototypes ? int main(void) and int main argc(int argc, char *argv) I mean to say, if I declare main in either of the above mentioned ways my compiler does not give any warning. How can it accept two different prototypes ?
6
5013
by: Robbie Hatley | last post by:
I'm maintaining a software project with 134 C++ files, some of them huge (as much as 10,000 lines each), and very few prototypes. The author's attitude towards prototypes was like this: Prototypes are only good for headers to be included in other files. For functions which call each other inside one file, such as A calls B which calls C and D, just define the functions in order D, C, B, A, and you'll never need prototypes.
9
1664
by: wizwx | last post by:
what does the following mean? int func2(); According to C++, it surely means func2 is a function that takes no argument and returns an integer. But what about in C? Does it have the same meaning or does it mean that func2 is a function that takes any number of arguments and returns an integer? Thanks for any clarification.
1
5561
by: Noah Roberts | last post by:
Trying to use boost::function in a C++/CLI program. Here is code: pragma once #include <boost/function.hpp> #include <boost/shared_ptr.hpp> #include <vector> using namespace System;
73
3468
by: Steph Barklay | last post by:
Hi, I'm currently taking a data structures course in C, and my teacher said that function prototypes are not allowed in any of our code. He also said that no professional programmers use function prototypes. This kind of bugged me, because from other people's code that I've seen in the past, almost all of them use function prototypes. The following also bugged me. Let's say you have a file called main.c with only the main function, and...
29
8088
by: Ravishankar S | last post by:
Dear C Experts, While prepating a content for a C course,I made section on function prototypes. Could you kindly provide me your comments on its correctness. Thank you ! Q12: What is the difference between a function prototype and a function declaration? If you get this right, you must have done fair amount of research on C
16
3289
by: Xiaoxiao | last post by:
Hi, I got a C library, is there a way to view the public function names in this library so that I can use in my C program? Thanks.
0
9518
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10433
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10000
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9035
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6777
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5560
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2919
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.