Function declaration and definition

Function declaration and definition

Function in C++ Programming: Declaration and Definition.

In C++ programming, a function program source code is a program block code that performs a specific programming function task defined by the programmer. If you want to solve any large programming problems in C++ programming, then you can divide these programs into smaller program function modules. This helps in dividing the complex programming task into smaller, manageable easy task blocks and improves the readability and reusability of the function program code. Whereas in C++ programming, functions are divided into two primary portions.

Function declaration and definition

C++ function declaration (or prototype/argument declaration).    

C++ function definition.

So let’s get to know the function default element in C++ better.

Function declaration (function prototype) in C++ program.

Function declaration in C++ programming is known as function prototype argument declaration process. Where C++ provides information to the compiler about the function’s prototype name, function return type and declared function parameters. But the realtime portion of the function is not added to it. The purpose of function declaration in C++ programming is to indicate the digital signature of the function to the compiler. So that when the actual and formal function is called at run time in the C++ program, it can test and debug the programming error and properly execute the function.

Function Syntax in C++ Programming.

return_type function_name(argument/parameter_list);

  • Function return_type – It indicates the data type of the function values ​​returned by the function in any C++ programming program. Such as, (int, void, float, etc.). If the current function does not return any value, the default function return type is zero.
  • function_name – This is the name of the function in the current C++ program. The function name is used to call the function in the program.
  • function_parameter_list – A list of argument parameters (if there is a function parameter list) in any C++ program that the function inputs as input variables in the program execution. You can separate multiple function parameter arguments in the program with a comma operator.

Function Declaration Syntax in C++ Example.

int total(int, int);  // here we Declare of a function named ‘total’ that returns an integer and takes two integer parameters.

Here, the function total function inputs two integer variables as parameter list, and returns an integer value as function program return.

Function Definition in C++ Programming.

The function definition in a C++ program is the location where C++ programmers create an executable block of code that the function executes in Realtime. It indicates the behaviour of the default function program, where the function return type, function name, and function parameter list should be properly similar to the function declaration. Where the programmer creates the function body in curly braces {} in the function definition.

Function Definition Syntax in C++ Programming.

return_type function_name(parameter_list) {

    // write function Code to execute

}

  • Where the function return type must be compatible with the declaration.
  • The function name must match the function name used in the declaration.
  • The function parameter list must match the function parameters used in the declaration.
  • The function type and the list of function parameters must be in reference to both.

Function Definition C++ Example.

int total(int p, int q)

{

    return p + q;  // it Returns the total of the two integers

}

Here, the function total is declared to take two integers (p and q) and return their total.

Function Declaration, Definition and Call Example in C++ Programming.

#include <iostream>

using namespace std;

// custom user define c++ Function declaration (prototype and argument)

int total(int, int);

int main()

{

    int output = total(5, 3);  // let Function call

    cout << “\n The total of p and q is – ” << output << endl;

    return 0;

}

// Function definition creation

int total(int p, int q)

{

    return p + q;  // it total two numbers and returns the output

}

Function Explanation in C++ Programming.

  • Function Declaration – Where the function total is declared before the main() function. It tells the compiler in the C++ program that there exists a function named total, which takes two integer values ​​as input, and returns an integer value.
  • Function definition – After the main() function in the function definition, the function total function is defined, which applies the total of two integer values ​​from the user.
  • Function call – In the function main(), we call the total function with arguments 1 and 2, and the function previews the result.