Call your own function - C++ Function

C++ examples for Function:Function Creation

Description

Call your own function

Demo Code

#include <iostream> 
#include <stdlib.h> 

using namespace std; 

int Squared(int numberToSquare); 

int main(int argc, char *argv []) 
{ 
  // Prompt the user for an integer. 
  cout << "Please ty pe an integer number and then press Enter: "; 

  // Delcare the integer variable. 
  int theNumber; 

  // Get the integer from the user. 
  cin >> theNumber; /*ww w  . j a va  2s.  co m*/

  // Declare another variable. 
  int theNumberSquared; 

  // Square the number. 
  theNumberSquared = Squared(theNumber); 

  // Print out the answer. 
  cout << theNumberSquared << endl; 

 // Prompt the user for an integer. 
 cout << "Please ty pe an integer number and then press Enter: "; 

 // Get the integer from the user. 
 cin >> theNumber; 

 // Square the number. 
 theNumberSquared = Squared(theNumber); 

 // Print out the answer. 
 cout << theNumberSquared << endl; 

 system("PAUSE"); 

 return (0); 
} 


int Squared(int numberToSquare) 
{ 
    int theNumberSquared = numberToSquare * numberToSquare; 
    
    return (theNumberSquared); 
}

Result


Related Tutorials