I want to use a function to call another function, like this:


Code:
void Ping(int x)
{
   switch(x)
   {
      case 1:
      case 2:
      case 3: while (x--)
      printf("\n%d",--x);
      break;
      case 25: pong (3);
      break;
      default: printf("\nSome text");
      pong(123);
   }
}
void Pong (int y)
{
   switch (x)
      { case 1:
      case 2: Ping (x);
      case 3: y=5;
      y++;
      return;
      default: printf("\nHello");
      return;
   }
}

main()
{
Pong (3);
Ping (-4);
}

It always gives me half a dozen of errors and warnings. I tried using an external function Ping_Pong (int x, int y); and declared the above functions inside it and for Pong (3), I wrote Ping_Pong(0,3), but it still doesn't work. It always says "Undefined reference to function [either Ping or Pong]

How do I mak this work?
The problem is that you need what are called function prototypes at the beginning of your program that tell the compiler the arguments, name, and return type of all of the functions you will have in your C program. That way, when it encounters a call to that function before the function itself, it'll know that function exists, and it'll know if you've passed the correct argument types and stored the output in a varible of the correct type. For your program, here's how the prototypes would look:
Code:
void Ping(int x)
void Pong (int y);
KermMartian wrote:
The problem is that you need what are called function prototypes at the beginning of your program that tell the compiler the arguments, name, and return type of all of the functions you will have in your C program. That way, when it encounters a call to that function before the function itself, it'll know that function exists, and it'll know if you've passed the correct argument types and stored the output in a varible of the correct type. For your program, here's how the prototypes would look:
Code:
void Ping(int x)
void Pong (int y);


Hello Kerm Very Happy

Tried; doesn't work neither.
Can you be more specific? In what way does it not work? What does your code look like (including those prototypes)?
This is my code now:

Code:
#include <stdio.h>

void Ping (int x);
void Pong (int x);

   void Ping(int x)
   {
   switch(x)
   {
      case 1:
      case 2:
      case 3: while (x--)
      printf("\n%d",--x);
      break;
      case 25: pong (3);
      break;
      default: printf("\nJa passei a C");
      pong(123);
   }
   }
   void Pong (int x)
   {
      switch (x)
      { case 1:
      case 2: Ping (x);
      case 3: x=5;
      x++;
      return;
      default: printf("\nHello");
      return;
      }}
      
main()
{
   Pong (3);
   Ping(-4);
   Ping(25);
   Pong (2);
   Pong(1);
}


the compiler says:
[Error] c:15: undefined reference to `pong'
[Error] c:18: undefined reference to `pong'
C is case-sensitive. The function "pong()" is not the same as the function "Pong()", just as the variable "SomeVar" is different from "Somevar" or "someVar" or "somevar" (or even "some_var").

Edit: Also, there's no space between the function name and the opening parenthesis with the arguments: Pong(..., not Pong (...
I feel so stupid right now 0.0

I can't believe that was what was causing the error

Thanks Kerm
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement