C++ FUNCTIONS

A function is a block of statements that performs a specific tast.

Advantages of functions:

  • Using functions, we can  avoid rewriting same logic/code again and again in a program.
  • We can call a function any number of times in a program and from any place.
  • We can track a large C program easily when it is divided into multiple functions.
  • Reusability is the main achievement of C function. 

TYPES OF FUNCTION 

1. Library Functions: are the functions which are declared in the C++ header files such as ceil(x), cos(x), exp(x), etc.

2. User-defined functions: are the functions which are created by the C++ programmer, so that he/she can use it many times. It reduces complexity of a big program and optimizes the code.

return_type function_name(parameter_list)
{
    body of the function
}

Parts of function: 

return_type: A function may return a value. The return_type is the data type of the value that a function returns. 

Some functions perform the desired operations without returning a value.

In this case, the return_type is the keyword void.

function name: This is the actual name of the function. The function name and the parameter list together constitute the function signature.

Parameters: A parameter is like a placeholder, when a function is invoked, you pass a value to the parameter. This value is referred to an actual parameter or argument. This parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional, i.e. a function may contain no parameter.

function body: A function body contains a collection of statements that define what the function does.

Given below the example of function called max(). This function takes two parameter num1 and num2 and returns the maximum value among the two.

// Function to the max value mong two.
int max(int num1, int num2)
{
    //local variable declaration
    int result;
    if(num1>num2){
        result = num1
    }else{
        result = num2;
    }
    return result;
}

Function Declaration: A function declaration tells the compiler about a function name and how to call the function. The actual body of the function can be defined separately.

A function declaration has the following parts:

return_type function_name(parameter_list)

For the above defined function max(), the function declaration is as follows:

int max(int num1, int num2)

Parameter names are not important in function declaration only there type is required, so the following is also a valid declaration.

int max(int, int)

Function declaration is required when you define a function in one source file and you call that function in another file. In such case, you should declare the function at the top of the file calling the function.

Calling a function:

While creating a function, you gave a definition of what the function has to do. To use a function, you will have to call that function to perform the defined task.

When a program call a function, the program control is transferred to the called function. A called function performs a defined task and when its return statement is executed or when its function ending closing brace is reached, it returns the program control back to main program.

To call a function, you simply need to pass the required parameters along with the function name, and if the function returns a value, then you can store the returned value.

#include <iostream>
using namespace std;
void func() {
   static int i=0; //static variable
   int j=0; //local variable
   i++;
   j++;
   cout<<"i=" << i<<" and j=" <<j<<endl;
}
int main()
{
 func();
 func();
 func();
}  

OUTPUT

i= 1 and j= 1
i= 2 and j= 1
i= 3 and j= 1