C++ Functions

C++ Functions: The C++ functions can be defined as a group of statements that perform a particular task. And in every C++ program, there will be at least one function called main( ). All these functions are used for structured programming which follows a top-down approach. The functions take the inputs and perform the task on those inputs and provide the output.

C++ Functions

While we create the programs, we use the functions which make the program easier to understand, edit and check the errors, etc; A function deceleration tells us the compiler about the function names, return type and parameters. And the function definition provides the actual body of the function.

And in C++ we are having no.of built-in functions that your program can call. We can also call the function with different names like sub-routine (or) method (or) a procedure etc.

Advantages of Function in C++

There are different advantages of functions C++. They are as follows:

1) Code Re-usability: If you create a function in the C++ program, we can call it many times. So, we no need to write the same code again and again.

2) Code Optimization: It is nothing but, optimizes the code. so, that we no need to write the same code again and again.

Defining  Functions in C++

If you want to define the functions in C++, we can define by using return_type, function name and parameters.

Syntax:

return_type function_name( parameter list ) {
body of the function
}

Let us in detail regarding the parameters, return type and all about defining a function.

  • Return type: The return_type is the data type of the value which the function returns. And a function may return a value. In some cases, functions will not return a value then return_type is the keyword void.
  • Function name: It is known as the actual name of the function. So, the function name and the parameter list together constitute the function signature.
  • Function body: It contains a collection of statements that define what the function is doing.
  • Parameters: When a function is invoked, we pass a value to the parameter. so it acts as like a placeholder. And this value is referred to as the actual parameter (or) argument. And the parameter list refers to type, order and no.of  the parameters of a function.

Example:

#include<iostream>
#include<conio.h>
using namespace std;
int iseven(int);//function prototype
int main()
{
    int n;
    cout<<"Enter a number: ";
    cin>>n;
    if (iseven(n))//function call by value
        cout<<n<<" is even";
    else
        cout<<n<<" is odd";
    getch();
    return 0;
}
int iseven(int x) //function definition
{
    int r;
    if (x%2 == 0)
        r=1;
    else
        r=0;
    return r;
}

Output:

Enter a number: 5

5 is odd

Example 2

#include <stdio.h> 
int max(int x, int y) 
{ 
 if(x > y) 
 return x; 
 else
  return y; 
} 
// main function that doesn't receive any parameter and 
// returns integer. 
int main(void) 
{ 
  int a = 10, b = 20; 
//Calling above function to find the max of 'a' and 'b' 
  int m = max(a, b); 
  printf("m is %d", m); 
  return 0; 
}

Output:

m is 20

Function declaration in C++

The function declaration in C++ tells the compiler about the number of parameters that function is taking and also the data types of parameters and return type of the function. And also assigning parameter names are optional in function deceleration. But it is compulsory in the function definition.

Syntax: return_type function_name( parameter list );

Example:

#include <iostream>
using namespace std; 
//function declaration
int max(int num1, int num2);
int main () {
//local variable declaration:
   int a = 100;
   int b = 200;
   int ret;
//calling a function to get max value.
   ret = max(a, b);
   cout << "Max value is : " << ret << endl;
 
   return 0;
}
 
//function returning the max between two numbers
int max(int num1, int num2) {
//local variable declaration
   int result;
   if (num1 > num2)
      result = num1;
   else
      result = num2;
   return result; 
}

Output: Max value is : 200