Learn C++ - C++ Function






The following code shows how to create function.


#include <iostream> 
using namespace std; 
void my_function(int);    // function prototype for my_function() 
/*w ww  . jav a2 s .  co  m*/
int main() { 
    my_function(3);       // call the my_function() function 
    cout << "Pick an integer: "; 
    int count; 
    cin >> count; 
    my_function(count);   // call it again 
    cout << "Done!" << endl; 
    return 0; 
} 

void my_function(int n)   // define the my_function() function 
{ 
    using namespace std; 
    cout << "Hi:" << n << " ." << endl; 
    // void functions don't need return statements 
}

The main() function calls the my_function() function twice, once with an argument of 3 and once with a variable argument count.

The code above generates the following result.





Defining a Function

Functions without return values are called type void functions and have the following general form:

void functionName(parameterList) { 
     statement(s) 
     return;          // optional 
} 

parameterList sets the types and number of parameters passed to the function.

The optional return statement marks the end of the function.

Typically, you use a void function to perform some sort of action.

A function with a return value produces a value that it returns to the caller.

Such a function is declared as having the same type as the value it returns. Here is the general form:

typeName functionName(parameterList) { 
   statements 
   return value;   // value is type cast to type typeName 
} 




Function prototype

To use a C++ function, you must Provide a function definition, Provide a function prototype, and Call the function.


#include <iostream>
using namespace std;
void simple();    // function prototype
/*from   w w  w .ja  v  a 2  s . co m*/
int main()
{
    
    cout << "main() will call the simple() function:\n";
    simple();     // function call
  cout << "main() is finished with the simple() function.\n";
    return 0;
}
// function definition
void simple()
{
    cout << "a function.\n";
}

The code above generates the following result.

Function Headers

The my_function() function in Listing 2.5 has this header:

void my_function(int n)

The initial void means that my_function() has no return value.

So calling my_function() doesn't produce a number that you can assign to a variable in main().

Thus, the first function call looks like this:

my_function(3);            // ok for void functions 

Because my_function() lacks a return value, you can't use it this way:

simple = my_function(3);   // not allowed for void functions 

The int n within the parentheses means that you are expected to use my_function() with a single argument of type int.

The n is a new variable assigned the value passed during a function call.

The following function call assigns the value 3 to the n variable defined in the my_function() header:

my_function(3);

When the cout statement in the function body uses n, it uses the value passed in the function call.

User-Defined Function with a Return Value

The following code shows how to convert value.


#include <iostream> 
/*from   www  . j  a va  2  s .  c  om*/
int convert(int);     // function prototype 
int main() { 
     using namespace std; 
     int input; 
     cout << "Enter the weight: "; 
     cin >> input; 
     int pounds = convert(input); 
     cout << input << " input = "; 
     cout << pounds << " pounds." << endl; 
     return 0; 
} 

int convert(int sts) 
{ 
      return 14 * sts; 
} 

In main(), the program uses cin to provide a value for the integer variable input.

This value is passed to the convert() function as an argument.

The code above generates the following result.

Using Directive in Multifunction Programs

To make the std namespace available to both functions, to place the directive outside and above both functions:


#include <iostream> 
using namespace std; // affects all function definitions in this file 
void my_function(int); 
//from   w w w .j a va2s . c o m
int main() { 
     my_function(3); 
     cout << "Pick an integer: "; 
     int count; 
     cin >> count; 
     my_function(count); 
     cout << "Done!" << endl; 
     return 0; 
} 

void my_function(int n) 
{ 
     cout << "my_function says touch your toes " << n << " times." << endl; 
} 

The code above generates the following result.