Function with parameter examples in C++


Example Program

Here is the simple program for printing function with parameters in C++.

Source Code Example : 1

#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: $$$$$


Source Code Example : 2

#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


Source Code Example : 3

#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


Source Code Example : 4

#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

Source Code Example : 5

#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. 


Source Code Example : 6

#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

Basic Programs


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



Learn All in Tamil © Designed & Developed By Tutor Joes