Quick question

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
Energy
Posts: 133
Joined: Sat Mar 26, 2005 4:13 pm
Location: uk/beds/flitwick
Contact:

Quick question

Post by Energy »

when I try to call a function I get this error...
implicit declaration of function menu

Code: Select all

int main()
{
        pspDebugScreenInit();
	SetupCallbacks();

	int choice;

	choice = menu();

	sceKernelSleepThread();
	return 0;
}

int menu(void)
{
        // ...
        return 0;
}
What am I being thick over?
Energy
Dr. Vegetable
Posts: 171
Joined: Mon Nov 14, 2005 1:32 am
Location: Boston, Massachusetts
Contact:

Post by Dr. Vegetable »

You are calling menu() before/above the point where you define it. You either need to have a function prototype at the top of the file or else move the definition of menu() above your main() function.

Code: Select all

int menu&#40;void&#41;;   // <-- function prototype, tells the compiler how to call menu&#40;&#41;

int main&#40;&#41;
&#123;
   pspDebugScreenInit&#40;&#41;;
   SetupCallbacks&#40;&#41;;

   int choice;

   choice = menu&#40;&#41;;

   sceKernelSleepThread&#40;&#41;;
   return 0;
&#125;

int menu&#40;void&#41;
&#123;
   // ...
   return 0;
&#125;
The "implicit declaration" warning means that you have called a function without first defining its calling convention, so the C compiler has implicitly assumed that it knows how to call the function. In your case, you get lucky because the assumption is that the function takes no parameters (void) and returns an integer. In C++, implicit declarations are not allowed.
Last edited by Dr. Vegetable on Thu Feb 23, 2006 1:57 am, edited 1 time in total.
Energy
Posts: 133
Joined: Sat Mar 26, 2005 4:13 pm
Location: uk/beds/flitwick
Contact:

Post by Energy »

Dr. Vegetable wrote:You are calling menu() before/above the point where you define it. You either need to have a function prototype at the top of the file or else move the definition of menu() above your main() function.

Code: Select all

int menu&#40;void&#41;;   // <-- function prototype, tells the compiler how to call menu&#40;&#41;

int main&#40;&#41;
&#123;
   pspDebugScreenInit&#40;&#41;;
   SetupCallbacks&#40;&#41;;

   int choice;

   choice = menu&#40;&#41;;

   sceKernelSleepThread&#40;&#41;;
   return 0;
&#125;

int menu&#40;void&#41;
&#123;
   // ...
   return 0;
&#125;
thanks... I knew it was something stupid lol
LuMo
Posts: 410
Joined: Sun Aug 21, 2005 2:45 am
Location: Austria
Contact:

Post by LuMo »

alternative you could just move the function menue above the function main...
"Good artists copy, great artists steal."
Pablo Picasso
go2lumo.com
User avatar
dot_blank
Posts: 498
Joined: Wed Sep 28, 2005 8:47 am
Location: Brasil

Post by dot_blank »

move function int menu() above int main()
using function prototypes is not really common
nowadays ... its old K&R style but any modern
compiler should still compile your program
just fine ... and there is no need for prototypes
anymore ...unless you really is your style to
have functions on after main
10011011 00101010 11010111 10001001 10111010
User avatar
dot_blank
Posts: 498
Joined: Wed Sep 28, 2005 8:47 am
Location: Brasil

Post by dot_blank »

move function int menu() above int main()
using function prototypes is not really common
nowadays ... its old K&R style but any modern
compiler should still compile your program
just fine ... and there is no need for prototypes
anymore ...unless you really is your style to
have functions on after main
10011011 00101010 11010111 10001001 10111010
Dr. Vegetable
Posts: 171
Joined: Mon Nov 14, 2005 1:32 am
Location: Boston, Massachusetts
Contact:

Post by Dr. Vegetable »

You may notice that I suggested both approaches. Which coding style to use is largely a matter of personal preference, but in a larger project it can be very difficult (and counter-intuitive) to try to order all of your functions top-down so that no prototypes are ever needed. If you always define a prototype for every function, you never need to worry about this.

My personal preference is to avoid prototypes during development of static local functions, and to always use them for any global functions. But once the design of my local functions has stabilized, I usually extract prototypes to the top of the file anyway. I find this practice makes it easier to harvest the code for re-use in other projects, because you can scan a list of function prototypes more easily than you can scan an entire source file to see what functionality is available for the borrowing.

Sometimes there's a good reason to go Old School.
Post Reply