Cpp - Function Function Creation

Introduction

General form of a function

[type] name([declaration_list]) // Function header 
{                               
    . 
    . 
    What will be done           // Function block 
    . 
    . 
}// End  
Item Meaning
typethe function type, that is, the type of the return value.
namethe function name, like a variable name and should indicate the purpose of the function.
declaration_list contains the names of the parameters and types. The list can be empty. void is equivalent to an empty list.

The parameters declared in a list are local variables.

They are created when the function is called and initialized by the values of the arguments.

When test( 10, -7.5); is called, the parameter arg1 is initialized with a value of 10 and arg2 with -7.5.

Prototype

The prototype is the declaration of the function and describes only the formal interface of that function.

You can omit parameter names from the prototype.

Demo

#include <iostream> 
using namespace std; 

void test( int, double );               // Prototype 

int main() /* w w  w . ja  v a 2s  .  co m*/
{ 
   cout << "\nNow function test() will be called.\n"; 
   test( 10, -7.5);                      // Call 
   cout << "\nAnd back again in main()." << endl; 

   return 0; 
} 

void test(int arg1, double arg2 )       // Definition 
{ 
    cout << "\nIn function test()." 
          << "\n  1. argument: " << arg1 
          << "\n  2. argument: " << arg2 << endl; 
}

Result

Related Topic