Function with parameter examples in C++


Example Program : 1

This is a C++ program that demonstrates default arguments in functions. The display() function takes two parameters: a character c and an integer n. Both parameters have default values assigned to them, so they can be omitted when calling the function.

In the main() function, the display() function is called three times with different arguments. The first time, no arguments are passed, so the default values of '*' and 3 are used. The second time, only the first argument '#' is passed, so the default value of 3 for the second argument is used. The third time, both arguments '$' and n are passed, where n is set to 5.

The display() function uses a for loop to print the character c n times, followed by a newline character. The for loop runs n times, printing c on each iteration.

The output of the program shows the different strings that are printed by the display() function for each call. The first call prints *** (three asterisks), the second call prints ### (three pound signs), and the third call prints $$$$$ (five dollar signs).

Source Code

#include <iostream>
using namespace std;
void display(char = '*', int = 3);
int main()
{
    int n = 5;
    cout<<"No argument passed: ";
    display();
    cout<<"First argument passed: ";
    display('#');
    cout<<"Both arguments passed: ";
    display('$',n);
    return 0;
}
void display(char c, int n)
{
    for(int i=1;i<=n;++i)
    {
        cout<<c;
    }
    cout<<endl;
}
To download raw file Click Here

Output

No argument passed: ***
First argument passed: ###
Both arguments passed: $$$$$


Example Program : 2

This is a C++ program that demonstrates default function arguments. The program defines a function Func() that takes a single parameter State of type string. The default value of State is set to "Karnataka", so if no argument is passed when calling the function, it will use "Karnataka" as the default value.

In the main() function, the Func() function is called four times with different arguments. The first three calls pass a string argument to the function, which overrides the default value of State. The fourth call does not pass any argument, so the default value of "Karnataka" is used. Each time the Func() function is called, it simply prints the value of State followed by a newline character.

The output of the program shows the different strings that are printed by the Func() function for each call. The first three calls print the string argument passed to the function, while the fourth call prints the default value of "Karnataka".

Source Code

#include<iostream>
using namespace std;
void Func(string State = "Karnataka")
{
    cout<<State<<"\n";
}
int main()
{
    Func("Tamilnadu");
    Func("Kerala");
    Func();
    Func("Andhrapradesh");
    return 0;
}
To download raw file Click Here

Output

Tamilnadu
Kerala
Karnataka
Andhrapradesh


Example Program : 3

This is a C++ program that demonstrates default function arguments. The program defines a function Func() that takes a single parameter Roll of type string. The default value of Roll is set to "AE1003", so if no argument is passed when calling the function, it will use "AE1003" as the default value.

In the main() function, the Func() function is called four times with different arguments. The first three calls pass a string argument to the function, which overrides the default value of Roll. The fourth call does not pass any argument, so the default value of "AE1003" is used.

Each time the Func() function is called, it simply prints the value of Roll followed by a newline character. The output of the program shows the different strings that are printed by the Func() function for each call. The first three calls print the string argument passed to the function, while the fourth call prints the default value of "AE1003".

Source Code

#include<iostream>
using namespace std;
void Func(string Roll = "AE1003")
{
    cout<<Roll<<"\n";
}
int main()
{
    Func("AE1001");
    Func("AE1002");
    Func();
    Func("AE1004");
    return 0;
}
To download raw file Click Here

Output

AE1001
AE1002
AE1003
AE1004


Example Program : 4

The function sum has two parameters: a and b. Ifb is not provided, it defaults to 20. In the first call to sum (sum(a, b)) , both a and b are provided, so the function calculates the sum of a and b, which is 100 + 200 = 300.

In the second call to sum (sum(a)), only a is provided, so the function uses the default value of b, which is 20. Therefore, the function calculates the sum of a and 20, which is 100 + 20 = 120. Finally, the two results are printed using cout.

Source Code

#include <iostream>
using namespace std;
int sum(int a, int b = 20)
{
   int result;
   result=a+b;
   return (result);
}
int main ()
{
   int a=100;
   int b=200;
   int result;
   result=sum(a, b);
   cout<<"Total value is :"<<result<<endl;
   result = sum(a);
   cout<<"Total value is :"<<result<<endl;
   return 0;
}
To download raw file Click Here

Output

Total value is :300
Total value is :120

Example Program : 5

This program defines a function Func that takes two parameters, a string fname and an integer age, and prints a message to the console with the values of these parameters.

In the main function, the Func function is called three times with different arguments: "Arun" and 19, "Aswin" and 18, and "Kanish" and 17.

Source Code

#include<iostream>
using namespace std;
void Func(string fname, int age)
{
    cout<<fname<<" IT. "<<age<<" years old. \n";
}
int main()
{
    Func("Arun", 19);
    Func("Aswin", 18);
    Func("Kanish", 17);
    return 0;
}
To download raw file Click Here

Output

Arun IT. 19 years old. 
Aswin IT. 18 years old. 
Kanish IT. 17 years old. 


Example Program : 6

The program defines a function swapNums that takes two integer arguments x and y by reference and swaps their values using a temporary variablez. Themain function declares two integers n1 and n2 with initial values of 10 and 20 respectively. It then calls the swapNums function with n1 and n2 as arguments and displays the values of n1 and n2 before and after the swap using cout statements.

Source Code

#include<iostream>
using namespace std;
void swapNums(int &x, int &y)
{
    int z = x;
    x=y;
    y=z;
}
int main()
{
  int n1 = 10;
  int n2 = 20;
  cout<<"\nBefore swap: ";
  cout<<"\n"<<n1<<"\n"<<n2;
  swapNums(n1, n2);
  cout<<"\nAfter swap: ";
  cout<<"\n"<<n1<<"\n"<<n2;
  return 0;
}
To download raw file Click Here

Output

Before swap: 
10
20
After swap: 
20
10

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