Friday, December 28, 2012

Learn Function...the hardest way

These program help to understand the concept of function.

Program_1.cpp - simple add program
Program_2.cpp - same add program but using 'function'
Program_3.cpp - program to understand why function (this is question)
Program_4.cpp - Program to understand why function (here is answer)
Program_5.cpp - same program via 'Function not returning a value' (same as Program_2.cpp)(invoke_1)
Program_6.cpp - simple add program via 'Function returning a value'
Program_7.cpp - same program via 'Function not returning a value' (same as Program_5.cpp)(invoke_2)
Program_8.cpp - same program via 'Function not returning a value' (same as Program_7.cpp)(invoke_3)
Program_9.cpp - simple add program via call by value 'function returning a value'
Program_10.cpp - simple add program via call by value 'function not returning a value'
Program_11.cpp - simple add program via call by reference 'function returning a value'
Program_12.cpp - simple add program via call by reference 'function not returing a value'

==================================================================
program_1.cpp
// simple program to add 2 number
#include <iostream>
using namespace std;

int main()
{
    int a=0;
    int b=0;
    int c=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
   
    c = a + b;
   
    cout << "\n Addition of 2 number is: " << c << endl;
}

===================================================================
program_2.cpp 
//same add program but using 'function'
#include <iostream>
using namespace std;

void add()
{
    int a=0;
    int b=0;
    int c=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
   
    c = a + b;
   
    cout << "\n Addition of 2 number is: " << c << endl;
}

int main()
{
    add();          //here we are calling (invoking) the function
}

====================================================================
Program_3.cpp
// program_3.cpp and Program_4.cpp is answer of question why function? 
#include <iostream>
using namespace std;

int main()
{
    int a=0;
    int b=0;
    int c=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
   
    c = a + b;
   
    cout << "\n Addition of 2 number is: " << c << endl;
   
    int a1=0;
    int b1=0;
    int c1=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a1 >> b1;
   
    c1 = a1 + b1;
   
    cout << "\n Addition of 2 number is: " << c1 << endl;
   
    int a2=0;
    int b2=0;
    int c2=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a2 >> b2;
   
    c2 = a2 + b2;
   
    cout << "\n Addition of 2 number is: " << c2 << endl;
}

==================================================================
Program_4.cpp
// program_3.cpp and Program_4.cpp is answer of question why function?
#include <iostream>
using namespace std;

void add()
{
    int a=0;
    int b=0;
    int c=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
   
    c = a + b;
   
    cout << "\n Addition of 2 number is: " << c << endl;
}

int main()
{
    add();
    add();           //here we are calling function 3 time
    add();
}

=========================================================================
 program_5.cpp
// simple program to add 2 number using 'Function not returning a value'
#include <iostream>
using namespace std;

void add()
{
    int a=0;
    int b=0;
    int c=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
   
    c = a + b;
   
    cout << "\n Addition of 2 number is: " << c << endl;
}

int main()
{
    add();
}
// This program is same as Program_2.cpp (in case you notice that)

====================================================================
Program_6.cpp 
// simple program to add 2 number using 'Function returning a value'
#include <iostream>
using namespace std;

int add()
{
    int a=0;
    int b=0;
    int c=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
   
    c = a + b;
   
    return (c);
}

int main()
{
    int c;              // Did you notice this
    c = add();         // understand what is going on here
    cout << "\n Addition of 2 number is: " << c << endl;
}

=======================================================================
Program_7.cpp
// This is another way to invoking (calling) a function
// and Program_7.cpp = Program_5.cpp  (please notice the differnce in programs, but both of them doing exact same work.) (but different calling methode)
#include <iostream>
using namespace std;

void add(int a, int b)
{
    int c = 0;
    c = a + b;
   
    cout << "\n Addition of 2 number is: " << c << endl;
}

int main()
{
    int a=0;
    int b=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
   
    add(a, b) ;
}

---------------------------------------------------------------------------------------------
Program_8.cpp
// Yet another ways to calling (invoke) a function
// This Program name is ; by Function not returning a value)
// and Program_8.cpp = Program_7.cpp   (both are not returning a value, but different calling methode)                      
#include <iostream>
using namespace std;

void add(int, int);         //declaration or prototype of function

int main()
{
    int a=0;
    int b=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
   
    add(a, b);
}

void add(int a, int b)         //definition of function
{
    int c = 0;
    c = a + b;
   
    cout << "\n Addition of 2 number is: " << c << endl;
}

==========================================================
Program_9.cpp
// simple add program via call by value 'function returning a value'
// this is equal to Program_6.cpp

#include <iostream>
using namespace std;

int add(int a, int b)    
{
    int c = 0;
    c = a + b;
  
    return (c);
}

int main()
{
    int a=0;
    int b=0;
    int c=0;
    int d=0;
  
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
  
    c = add(a, b);   
    cout << "\n Addition of 2 number is: " << c << endl;
}
*/

==========================================================
Program_10.cpp
// simple add program via call by value 'function not-returning a value'
// this is equal to Program_7.cpp

#include <iostream>
using namespace std;

void add(int a, int b)    
{
    int c = 0;
    c = a + b;
  
    cout << "\n Addition of 2 number is: " << c << endl;
}

int main()
{
    int a=0;
    int b=0;
    int c=0;
    int d=0;
  
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
  
    add(a, b);   
}
*/

=================================================================

// This is a extra program to understand 'actual' and 'formal' parameter
// which associated with the 'call by value' and 'call by reference'
#include <iostream>
using namespace std;

void add(int a, int b)      //here a, b are formal parameter
{
    int c = 0;
    c = a + b;
  
    cout << "\n Addition of 2 number is: " << c << endl;
}

int main()
{
    int a=0;
    int b=0;
    int c=0;
    int d=0;
  
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
  
    d = a-b+c;          //actual parameter
    cout << " \n substraction is(d): " << d << endl;
  
    add(a, b);     //actual parameter
}


To learn more about 'actual parameter & formal parameter' click here

========================================================================

Program_11.cpp
// simple add program via call by reference 'function returning a value'
#include <iostream>
using namespace std;

int add(int &a, int &b)   
{
    int c = 0;
    c = a + b;
  
    return (c);
}

int main()
{
    int a=0;
    int b=0;
    int c=0;
    int d=0;
  
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
  
    c = add(a, b);   
    cout << "\n Addition of 2 number is: " << c << endl;
}
 

===============================================================
Program_12.cpp
// simple add program via call by reference 'function not-returning a value'
#include <iostream>
using namespace std;

void add(int &a, int &b) 
  
{
    int c = 0;
    c = a + b;
  
    cout << "\n Addition of 2 number is: " << c << endl;
}

int main()
{
    int a=0;
    int b=0;
    int c=0;
    int d=0;
  
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
  
    add(a, b);   
}

=========================================================

// why use  'Function returning a value'
#include <iostream>
using namespace std;

int divide()
{
    int a1=0;
    int b1=0;
    int c1=0;
  
    cout << "\n Please Enter 2 number for divide: ";
    cin >> a1 >> b1;
    c1 = a1 / b1;
    return (c1);
}

int add()
{
    int a=0;
    int b=0;
    int c=0;
  
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
    c = a + b;
    return (c);
}

int main()
{
    int c1;
    c1 = divide();
  

    int c;             
    c = add(); 
  

    int d;
    d = c + c1;
    cout << "\n Addition of 2 number is: " << d << endl;
}

1 comment:

  1. notice program 2, 5, 10 are same
    and program 6 and 9 are axactlly same

    ReplyDelete