MASIGNASUKAv102
6510051498749449419

C++ Functions - Learn With AVRK

Function in C++ Language

A function is a set of statements. In C++ programming language it is also known as procedure or subroutine in other programming languages. We can create a function to accomplish any task. A function can be called multiple times. It provides modularity and code reusability.

Advantage of Functions in C++

Functions have many advantages. Some basic advantages are written.

1) Code Reusability

You can call it multiple times by creating a function in C++. So we do not have to write the identical code again and again.

2) Code Optimization

It makes the code optimized, there is no need to write much code. Suppose, you have to check 3 numbers (331, 443 and 786) whether it is a prime number or not. Without using the function, you have to write the prime number argument 3 times. So, there is repetition of code. But if you use a function, you only have to write the argument once and you can reuse it multiple times.

Loading...

Types of Functions

There are two types of functions in C++ programming language - 

1. Library Functions

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

2. User-defined Functions

User-defined functions are those which created by the C++ programmer so that he can use them multiple times. It reduces the complexity of a large program and optimizes the code.

Declaration of a Function

The syntax for creating a function in C++ language is given below -
return_type function_name(list of parameter)
{
    /* Body of the function */
}

The C++ function definition consists of a function header and a function body. All the sections of a function are as follows -
  • Return TypeA function can return a value. The return type is the data type of any value that the function returns. Some functions perform desired operations without returning any value. In this case, the return type should always be void.

  • Function NameThis is the actual name of the function. The function name and parameter list together form the function signature.

  • Parameters - A parameter is like a placeholder. When the function is invoked, this time the user is passing a value to the parameter. These values ​​are referred to as the actual arguments. Parameter list refers to the order, type, and number of parameters of a function. Parameters are optional, which means that the function cannot have any parameters.

  • Function BodyThe function body contains a set of statements that define what the function is going to do.

Note : Function parameters are optional or sometimes they are not required, you can write it as per your requirement.


C++ Function Example -

Let's look at a simple example of a C++ function
#include <iostream>
using namespace std;
void function()
{
    static int i=0; /*static variable*/
    int j=0; /*local variable*/
    i++;
    j++;
    cout<<"i=" << i<<" and j=" <<j<<endl;
}
int main()
{
    function();
    function();
    function();
}

Output -

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

Also check out this : C++ Comments - Learn With AVRK
Learn With AVRK

Learn With AVRK