C++ Functions

A function may be a group of statements that together perform a task.

Each C++ program has at least one function, which is main(), and each one the first unimportant program can characterize extra function.

You can split your code into independent functions. How you split your code among various functions is up to you, yet logically the division for the most part is such each functions performs a chose task.

The function in C++ language is additionally referred to as procedure or subroutine in other programming languages.

To perform any task, we can create function. A function can be called many times. It provides modularity and code reusability.

The function in C++ language is additionally referred to as procedure or subroutine in other programming languages.

Types of Functions



There are two sorts of functions in C programming:
1. Library Functions:

These are the functions which are declared within the C++ header files like ceil(x), cos(x), exp(x), etc.

2. User-defined functions: These are the functions which are created by the C++ programmer, in order that he/she can use it repeatedly . It reduces complexity of an enormous program and optimizes the code.


Declaration of Functions


A function declaration tells the compiler a few function name and the way to call the function. the particular body of the function are often defined separately.

Declaration of function should be done before main() funcrion.

Syntax: -

return_type function_name(data_type parameters)



Defining a Functions


A function declaration tells the compiler a few function's name, return type, and parameters. A function definition provides the particular body of the function.

Syntax: -

return_type function_name(data_type parameters)
{
//Functions Body
//Code going to be executed

}


Where,
  • Return Type − A function may return a worth . The return_type is that the data sort of the worth the function returns. Some functions perform the specified operations without returning a worth . during this case, the return_type is that the keyword void.

  • Function Name − this is often the particular name of the function. The function name and therefore the parameter list together constitute the function signature.

  • Parameters − A parameter is sort of a placeholder. At the point when function is invoked, you pass a worth to the parameter. This value is mentioned as actual parameter or argument. The parameter list refers to the sort , order, and number of the parameters of a function. Parameters are optional; that's , a function may contain no parameters.

  • Function Body − The function body contains a set of statements that outline what the function does.

Calling a Function


While creating a C++ function, you provides a definition of what the function has got to do. To use a function, you'll need to call or invoke that function.

At the point when a program calls a functions, program control is moved to the called function.

To call a function, you merely got to pass the specified parameters along side function name, and if function returns a worth , then you'll store returned value.

Syntax: -

function_name(data_type parameters);


When you write function name with it's parameter it will acts as call to the function.


Example: -

#include <iostream>
using namespace std;

// Function Declaration
int sum(int a,int b);

int main() {
int a = 10;
int b = 20;
int s;

//Calling Function
s = sum( a , b );
cout << i <<"Sum = "<< s;
}

// Function Defining
int sum(int a,int b);
{
return ( a + b );
}


When above program is executed, it will print sum of a and b. which is 30.

Output:
Sum = 30