3 Replies - 784 Views - Last Post: 09 April 2009 - 04:39 PM Rate Topic: -----

#1 scokah  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-April 09

problem understanding function prototypes

Posted 09 April 2009 - 01:57 PM

My program might be a little messy so sorry...
I am having a problem with prototypes. I don't really get why I need phototypes or how to use them (my book doesn't explain it well)

One of my problems is I need to use correctMessage() instead of
cout << "Great Work!!!" << "\n";
and I also need to use random good comments (switch between great, excellent, very good...). That also applys to incorrectMessage() and
 wrong++;
		   while ( z != multiplication(x,y) )
		   {
			   cout << "Wrong. Try again. \n";
			   cin >> z;
		   }

and bool needHelp( int, int ) and
 fail = (wrong/10)*100;
   	if ( fail <= 75)
	{
		cout << "\nPlease ask you instructor for extra help.\n";
	}



Thank you for your help and hopefully I will understand the prototypes more. My full program is below.

#include <iostream> 
using std::cin; 
using std::cout; 
using std::endl; 

#include <cstdlib>
using std::rand;
using std::srand;

#include <ctime>
using std::time;

void multiplication(); 	// function prototype for the function you are programming! 
void correctMessage(); 	// function prototype
void incorrectMessage();	 // function prototype
bool needHelp( int, int );	 // function prototype

int main()
{



   srand( time( 0 ) ); 		// seed random number generator
   int multiplication( int x = 0, int y = 0 );		// begin multiplication practice

  
		 int count =1;
		 int x =0;
		 int y =0;
		 int z =0;
		 int wrong =0;
		 double fail =0;


   while (count <= 10 && z != -1)
   {
	   count++;
	   x = rand() % 9 + 1;
	   y = rand() % 9 + 1;

		   cout << "How much is " << x << " times " << y << " (-1 to end)? ";
		   cin >> z;

		   	   if ( z != multiplication(x,y) && z != -1)
	   {
		   wrong++;
		   while ( z != multiplication(x,y) )
		   {
			   cout << "Wrong. Try again. \n";
			   cin >> z;
		   }
			
	   }

			 if ( z == multiplication(x,y))
			{
		
				  cout << "Great Work!!!" << "\n";
			  }		

	   
	   
   }
   fail = (wrong/10)*100;
   	if ( fail <= 75)
	{
		cout << "\nPlease ask you instructor for extra help.\n";
	}


   return 0; 		// indicate successful termination

} // end main

int multiplication(int x, int y)
{ 
	return x*y;
}



Is This A Good Question/Topic? 0
  • +

Replies To: problem understanding function prototypes

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: problem understanding function prototypes

Posted 09 April 2009 - 03:17 PM

A function prototype simply tells the compiler that the code that follows is going to refer to a function that we've not yet defined but will look like this prototype.

What this means is that the compiler will compile the code without those functions defined, expecting that the linker, during that part of the build process, will find a function matching that prototype defined somewhere the linker knows about, whether it's another object file (.o) created by the current build, or a previously-built library (.a or .lib).

If you do not define a function that you've previously prototyped, or you define the function differently than you've prototyped it -- even if it's just a typo, different capitalization, or misspelling -- when you get to the linking process the linker will give "Unresolved external symbol" of "Undefined reference" errors, depending on your OS. The prototype (otherwise known as the declaration) must match the implementation (otherwise known as the definition) EXACTLY.

For example, building this simple little code, without defining printHelloWorld():
#include <iostream>

// Function prototype
void printHelloWorld();

int main()
{
    printHelloWorld();
    return 0;
}


gives this error in Linux:
/hw.cpp:8: undefined reference to `printHelloWorld()'
collect2: ld returned 1 exit status


OK, so let's implement printHelloWorld, but do it wrong:
#include <iostream>

// Function prototype
void printHelloWorld();

int main()
{
    printHelloWorld();
    return 0;
}

// Note, I've said here that it will takes an integer argument
void printHelloWorld(int x)
{
    int p = x;
    std::cout << "Hello World!" << std::endl;
}


The error is the same:
hw.cpp:8: undefined reference to `printHelloWorld()'
collect2: ld returned 1 exit status


So I'll remove the argument (and remove the reference to it) and it will build fine.

Note, that prototypes are simply to let the compiler know what's coming, so that it's prepared for it. If the compiler already knows about the function, a prototype isn't necessary.

For example, let's add the user's name to our Hello statement. We will define a function that is not prototyped, but is called from our prototyped printHelloWorld function. In order to do that, the definition must precede its reference in the code:
#include <iostream>
#include <string>

// Function prototype
void printHelloWorld();

int main()
{
   printHelloWorld();
   return 0;
}

// No prototype
std::string getName()
{
   std::string name;
   std::cout << "Enter your first name: ";
   std::cin >> name;

   return name;
}

void printHelloWorld()
{
   // Call unprototyped function
   std::string name = getName();
   std::cout << "Hello " << name << "!" << std::endl;
}


Produces a satisfactory program that does the following:
jack@work:~/dic> ./hw
Enter your first name: Jack
Hello Jack!
jack@work:~/dic>

Was This Post Helpful? 1
  • +
  • -

#3 Zerobu  Icon User is offline

  • Black Hatter

Reputation: 13
  • View blog
  • Posts: 1,822
  • Joined: 14-January 08

Re: problem understanding function prototypes

Posted 09 April 2009 - 03:27 PM

Well think of a fuction prototype as a promise to compiler. You would declare the function prototype like this.

 void sumFunction();



When a function prototype is declared it is still required for you to write the code for the function anywhere in your program. With function prototypes you could call any of your functions anywhere in your program as long as the code definition is implemented. This is better method of design because you are able to call a function anywhere, this has the a major advantage over top down design where you would previously had to declare functions on top of each other. Hope that helps!

This post has been edited by Zerobu: 09 April 2009 - 03:28 PM

Was This Post Helpful? 0
  • +
  • -

#4 scokah  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-April 09

Re: problem understanding function prototypes

Posted 09 April 2009 - 04:39 PM

thank you all for your help, I understand it a lot better now and I sould be able to get my program done.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1