• C

implicit routine definition

i have 2 routines say,

void one() {
  ...
  two();
  ...
}

void two() {
  ...
  one();
  ...
}


when compiling, it gives me the warning of the implicit declaration of two() to return int.  
is there something i can do to avoid this warning??

rawdribAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

van_dyCommented:
just do

void two(void);   // add this declaration before void one()

void one() {
  ...
  two();
  ...
}

void two() {
  ...
  one();
  ...
}

it shall compile without warning
hope this helps,
van_dy
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PaulCaswellCommented:
van_dy,

That should move the problem to 'implicit ... one() ...'. Caller should also prototype:

void one(void);   // add this declaration too.

The reason for this is that if you allow the compiler to intuit the type of a function then it will assume the function returns an 'int'. Prototyping all functions is a good practice as it will avoid many potential issues.

Paul



0
van_dyCommented:
Hi Paul,

      I dont think it will be necessary to add void one(void)
if we keep
---------------------
void two(void);   // add this declaration before void one()

void one() {
  ...
  two();
  ...
}

void two() {
  ...
  one();
  ...
}
-------------------- in one single  file.
It is possible to declare and define the  functions (which call  each other)
at the same time,  with one exception, we need to break the loop somewhere
and a single declaration like 'void two(void)' should work  nicely.
like for an example.

varun@pc ~# cat > test.c
void two(void);

void one(void)
{
        two();
}

void two(void)
{
        one();
}
varun@pc ~# cc -c test.c   //compiles nicely without warnings.
varun@pc ~#

However your suggestion to prototype void one(void) at the
beginning isnt bad either since it lets the reader know all the
functions that are being defined later in the file.   But not giving
the prototype  'void one(void)' shouldnt cause warnings

0
kledbetterCommented:

You should always prototype ALL of your functions -- never let the compiler do implicit definitions.  It's just asking for trouble.
Bottom line, to fix your problem just do function prototypes in either a .h file or at the top of the .c file holding these routines:


void one(void);
void two(void);

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.