Pages

C++ Functions

What is C++ Functions?

Functions allow to C++ programs structure in parts of code to perform individual tasks. Modules in C++ are referred to as functions. Every C++ program has at least one function, which is main(), and all the most trivial programs can define additional functions.

Advantages of using Function in C++ Language

  • Divide-and-conquer
    • Function can an breaking up programs and algorithms into smaller, more manageable pieces and easier to break up the work for team development
  • Easy Debugging
    • Function  for easier writing, testing, and debugging
  • Reusability
    • Functions can be called to do their tasks anywhere in a program, as many times as needed that will Avoids repetition of code in a program of C++.

Types of Functions in C++ Language

C++ Language provide following types of function 

    1. User Defined Functions
C++ language allows programmer / developer  to define their own function.A user-defined function groups code to perform a specific task and user defined function have unique name.

Example:


#include <iostream>
// function declaration
float area(float length, float width);
using namespace std;
int main()
{
// String output with calling function with parameters
    cout<<"Area="<<area(31.21,55.4);
// new line
    cout<<endl;
    return 0;
}
 // function definition with two parameters 
 float area(float length, float width){
// return two parameters with multiply Length & Width
     return (length*width);

  }

   2. Build-in Functions / Library Functions
A type of function that is available as part of language is known as built-in function or library function.These function are ready made programs.These functions are stored in different header files.Built-in function make programming faster and easier. for example sqr(12); cmath header file.

Example:
//Example of Build-in Function in C++
// www.cpplanguage.com
#include <iostream>
// include header for using Built-in function
#include <cmath>
// namespace std
using namespace std;
// main 
int main()
{
// Initialization number value
float number=90;
// output string with run function sqrt
cout<<"Square Root of Number="<<sqrt(number);
// New Line
cout<<endl;
// output string with run math function cos value
cout<<"Cos of Number="<<cos(number);
// New Line
cout<<endl;
// Return function 
return 0;

}

Function Declaration / Function Prototype 

Function declaration is model of a function. it is also called function prototype. it provides information to computer about structure of the function to used in program.it end with semicolon.Function prototypes are usually placed at the beginning of the source file just before the main function. 

Function declaration consists of the following parts

Function name 
it indicates the name of function. the rule for function name is similar to variables. Each function should have a unique name.

Function return type
it indicate the type of value that will be returned by function. For Example int is used as return type if the function return integer value. if the function return no value , the keyword void is used.

Number and types of parameters
Parameters are the values that are provided to function when the function is called.parameters are given in parentheses. if there are many parameters, these are separated by commas. If there is no parameter,empty parentheses are used or keyword void is written in parentheses.

The Parameters are given in two ways:
Only data type of the parameters are written in prototype as following:
int add(int,int);
Both data types and names of parameters are written in prototype as follows:

int add(int a, int b);

Examples:
The following example shows that the function cube show accepts no parameter and return no value
void show(void);

The following example shows that the function line show accepts one integer parameter and return no value
void show(int);

The following example shows that the function line show accepts one integer parameter and return integer value
int show(int);
int show(int a);

The following example shows that the function line show accepts three integer parameter and return integer value
int show(int,int,int);

int show(int a, int b, int c);


Function Definition


A set of statement that explains what a function does is called function definition.
The Function defination can be written at the following places:
  • Before main function
  • After main function
  • In a separate file

Function declaration is not required if the function is written before main () function. Function declaration is compulsory if function definition is written after main() function. If function definition is written in a separate file then it can be used by including that file in the program using #include pre-processor directive.

The function definition consist of two parts:

Function Header
The first line of function definition is known as function header. it is also called function declaration. It is similar to function prototype. The Only Differences is that is not header and function prototype must be same.
Function Body
The set of statement which are executed inside the function is known as function body. The body of function appears after function declaration and the statements are written in curly braces {}.

Syntax
The Syntax of function definition is as follows

Return type Function_name(Parameters) // function header
{                      // Function Body
statement 1;
statement 2;
.......
statement N;

}

Function Call

The statement that activates a function is known as function call. A function is called with its name. Function name is followed by necessary parameters in parentheses. If there are many parameters. these are speared by commas. If there is no Parameter, empty parentheses are used.

The following steps take place when a function called
   1. The control moves to the function that is called
   2. All statements in the function body are executed.
   3. The control return back to the calling function.


C++ Function Call Process with diagram


<< C++ goto Statement        --        C++ Arrays >>