Function with Argument Examples in C++


Example Program : 1

This code prompts the user to input two integers, passes them to a function named add, and then displays the result of adding those two integers. The add function takes two integers as parameters, adds them together, and then displays the result. Overall, this code demonstrates how to define and call a simple function in C++.

Source Code

#include<iostream>
using namespace std;
void add(int,int);
int main()
{
    int a,b;
    cout<<"\nEnter The Two Values:";
    cin>>a>>b;
    add(a,b);
    return 0;
}
void add(int a,int b)
{
    int c;
    c=a+b;
    cout<<"\nAddition:"<<c;
}
To download raw file Click Here

Output

Enter The Two Values:45
67
Addition:112

Example Program : 2

This program defines a function called add which takes two integer inputs from the user, adds them together, and prints out the result. The add function is declared before main using the function prototype void add();, which tells the compiler that there is a function called add that takes no arguments and returns nothing.

In the main function, the add function is called using add();, which executes the code inside the function definition. Inside the add function, two integers are read in using cin, added together, and the result is printed out using cout. Overall, this program is a simple example of how to define and call a function in C++.

Source Code

#include<iostream>
using namespace std;
void add();//Function Declaration
int main()
{
  add();//Function Calling
  return 0;
}
void add()//Function Definition
{
    int a,b,c;
    cout<<"\nEnter The Two values:";
    cin>>a>>b;
    c=a+b;
    cout<<"Addition:"<<c;
}
To download raw file Click Here

Output

Enter The Two values:56
53
Addition:59

Example Program : 3

This program defines a function called myFunction that takes a string argument fname and outputs a message with the given fname concatenated with the string "Kumar". Then, the function is called three times with different names ("Arun", "Ram", and "Aswin") as arguments.

Source Code

#include<iostream>
using namespace std;
void myFunction(string fname)
{
    cout<<fname<<"Kumar\n";
}
int main()
{
    myFunction("Arun");
    myFunction("Ram");
    myFunction("Aswin");
    return 0;
}
To download raw file Click Here

Output

ArunKumar
RamKumar
AswinKumar

Example Program : 4

This program demonstrates the use of a function to perform addition of two integers. The function add takes two integer arguments a and b and returns their sum as an integer. In the main function, the user is prompted to input two integers a and b, which are passed as arguments to the function add. The result of the addition is then printed to the console.

Source Code

#include<iostream>
using namespace std;
int add(int,int);
int main()
{
    int a,b;
    cout<<"\nEnter The Two Values:";
    cin>>a>>b;
    cout<<"\nAddition:"<<add(a,b);
    return 0;
}
int add(int a,int b)
{
    int c;
    c=a+b;
    return c;
}

To download raw file Click Here

Output

Enter The Two Values:56
43
Addition:99

Example Program : 5

This program demonstrates the use of a function to perform addition of two numbers. The program defines a function called add() that takes two integer inputs from the user and returns their sum. In the main function, the add() function is called and its return value is printed to the console.

The program starts by including the iostream header file, which provides the input/output functionality. Then, the add() function is declared with a return type of int and no arguments. In the main function, the add() function is called using the cout statement, which prints the result of the function to the console. Finally, the main function returns 0, indicating successful execution of the program.

Source Code

#include<iostream>
using namespace std;
int add();
int main()
{
    cout<<"\nAddition:"<<add();
    return 0;
}
int add()
{
    int a,b,c;
    cout<<"\nEnter The Two Values:";
    cin>>a>>b;
    c=a+b;
    return c;
}
To download raw file Click Here

Output

Addition:
Enter The Two Values:56
65
121

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