I was wondering how I could call a function in an other function in C. For example, I cr开发者_开发技巧eated the function :
void speed (int argc, char** argv)
from a hyperterminal, I type 1 or 2 and I get something written on the hyperterminal. In another function called:
void menu (int argc, char ** argv)
I want to call the previous function: speed(...)
. I dont know what to fill in for ...
.
thanks jim
void menu (int argc, char ** argv)
{
speed(argc, argv); // this is how you call a function
}
For this to work either speed needs to be defined above menu, or a declaration of it needs to be before it or in a header. Declaration of speed looks like
void speed (int argc, char ** argv);
What Franco was talking about are called function prototypes. C parses (or compiles) your code from top down, so if it hits your call to speed before it hits your declaration of speed it complains. To workaround this, you need to create a function prototype as a forward reference to the function. Generally, it is good practice to write prototypes for all your functions at the top of your code, or in another file (aka the header file) and #include it.
/* helloworld.h */
void speed (int , char **);
void menu (int , char **);
/* helloworld.c */
#include "helloworld.h"
void menu (int argc, char **argv){
speed (argc, argc);
}
精彩评论