Cpp - Create a function to calculate area


Create a function to calculate area


#include <iostream> 
   /*from  ww  w  . j  a  va 2  s  .c o m*/
int findArea(int length, int width); // function prototype 
int main() 
    int length; 
    int width; 
    int area; 
    std::cout << "\nHow wide is your yard? "; 
    std::cin >> width; 
    std::cout << "\nHow long is your yard? "; 
    std::cin >> length; 
    area = findArea(length, width); 
    std::cout << "\nYour yard is "; 
    std::cout << area; 
    std::cout << " square feet\n\n"; 
    return 0; 
// function definition 
int findArea(int l, int w)  
     return l * w; 


The function prototype for the findArea() function is listed at the top.

Compare the prototype's name, return type and parameter types.

They are the same, but the names of the parameters are length and width in the prototype and l and w in the function.

This distinction does not matter because the parameter types match.

Related Example