Thursday, May 21, 2020

Function Overloading in C++


  • Program :

#include <iostream>
using namespace std;
int c(int x, int y)
{
  return x + y;
}

double d(double x, double y)
{
  return x + y;
}

int main()
{
  cout <<"\n----------------FUNCTION OVERLOADING----------------\n";
  int a = c(8, 5);
  double b = d(4.3, 6.26);
  cout << "Int: " << a << "\n";
  cout << "Double: " << b;
  return 0;
}


Pass By Reference using functions in C++


  • Program :

#include <iostream>
using namespace std;
void swapNums(int &x, int &y)
{
  int z = x;
  x = y;
  y = z;
}


int main(){
  cout <<"\n----------------FUNCTION PARAMETERS----------------\n";
  cout <<"\n----------------Pass By Reference----------------\n";
  int a = 10, b = 5;
  cout << "Before swap: " << "\n";
  cout << a <<" and "<< b << "\n";

  swapNums(a, b);

  cout << "After swap: " << "\n";
  cout << a <<" and "<< b << "\n";

  return 0;
}


Return Values using functions in C++


  • Program :

#include <iostream>
using namespace std;
int myFunction(int x) {
  return 10 + x;
}

int main(){
  cout <<"\n----------------FUNCTION PARAMETERS----------------\n";
  cout <<"\n----------------Return Values----------------\n";
  cout << myFunction(8);
  return 0;
}


Multiple Parameters using functions in C++


  • Program :

#include <iostream>
#include <string>
using namespace std;
void myFunction(string fname, int age) {
  cout << fname << " Venkat" << age << " years old. \n";
}

int main(){
  cout <<"\n----------------FUNCTION PARAMETERS----------------\n";
  cout <<"\n----------------Multiple Parameters----------------\n";
  myFunction("Monisha", 3);
  myFunction("Janani", 14);
  myFunction("Ramya", 30);
  return 0;
}


Default Parameters using functions in C++


  • Program :

#include <iostream>
#include <string>
using namespace std;
void myFunction(string country = "Norway") {
  cout << country << "\n";
}

int main(){
  cout <<"\n----------------FUNCTION PARAMETERS----------------\n";
  cout <<"\n----------------Default Parameters----------------\n";
  myFunction("Sweden");
  myFunction("India");
  myFunction();
  myFunction("USA");
  return 0;
}

Parameters and Arguments using function in C++

  • Syntax :

     void functionName(parameter1parameter2parameter3
        {
      // code to be executed        }

  • Program :

#include <iostream>
using namespace std;
void myFunction(string name) {
  cout << name << " Venkat\n";
}

int main(){
  cout <<"\n----------------FUNCTION PARAMETERS----------------\n";
  cout <<"\n----------------Parameters and Arguments----------------\n";
  myFunction("Monisha");
  myFunction("Janani");
  myFunction("Ramya");
  return 0;
}


Call a function & function declaration,definition in C++


  • Syntax :

     void myFunction() 
        { // declaration
      // the body of the function (definition)        }
  • Program :

#include <iostream>
using namespace std;
// Function declaration
void myFunction();
int main(){
  cout <<"\n----------------FUNCTIONS----------------\n";
  cout <<"\n----------------Function Declaration & Definition,Create & Call a Function----------------\n";
    myFunction();  // call the function
  return 0;
}

// Function definition
void myFunction() {
  cout << "C++ Programming";
}


Function Overloading in C++

https://monitechvenkat1620.blogspot.com Program : #include <iostream> using namespace std; int c(int x, int y) {   return...