Function In C++ Programming


A C++ Function is a collection of statements that are grouped together to perform an operation. A function in C++ is a block of code that, When the function is invoked from any part of the program, it all executes the codes defined in the body of the function

You can insert values or parameters into Function, and they will only be executed when called. They are also referred to as functions. The primary uses of function in C++ are:

  • It allows code re-usability (define once and use multiple times)
  • You can break a complex program into smaller chunks of code
  • It increases code readability

 Two Types of Function :
        1. Pre-defined Function: These are built-in functions in C++ that are available to use.
        2. User-defined Function: We can create our own function based on our requirements.

 Pre-defined Function :
        Pre-defined Function are also known as library functions. We need not to declare and define these functions as they are already written in the C++ libraries such as iostream, cmath , string etc

User-defined Function :
        User-defined Function allows the programmer to define their own function.
        Syntax:
           Return_Type Function_Name ( Parameters )
           {
                 // body of Statement ;
           }

 Return Type :

  • Int : Int as the return type if the function returns value.
  • Void : Void as the return type if the function returns no value.

 Parameter list :

  • It is a list of arguments (data_type variable_name) that will be used in the function.

 Statement body :

  • This is the set of instructions enclosed within curly brackets.

Four Types of User-defined Function

  • No Return Type and Without Argument
  • No Return Type and With Argument
  • Return Type and Without Argument
  • Return Type and With Argument

The program defines a function named display() that adds two numbers entered by the user and displays their sum. The function display() is defined with a return type of void, meaning that it does not return any value. The function does not take any parameters, but it declares three integer variables a, b, and c to store user input and the result of the addition.

Within the display() function, the user is prompted to enter two integer values, which are stored in the variables a and b. The program then adds these two values together and stores the result in the variable c. Finally, the program displays the total by printing the value of c to the console.

In the main() function, the display() function is called twice, so the user is prompted to enter two pairs of numbers, and their sums are displayed to the console. Overall, the program defines a function that adds two numbers and displays their sum, and then calls this function twice within the main() function to allow the user to enter two pairs of numbers and view their sums.

Source Code

#include<iostream>
using namespace std;
//  Return Type   Function Name  (Parameters)
void display()
{
    int a,b,c;
    cout<<"\nEnter 2 Nos : ";
    cin>>a>>b;
    c=a+b;
    cout<<"Total : "<<c;
}
int main()
{
   display();
   display();
    return 0;
}
 

Output

Enter 2 Nos : 23
56
Total : 79
Enter 2 Nos : 12
23
Total : 35
To download raw file Click Here

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples