functions


 Function- is a subprogram that act on data and return a value.

Function Call by Value

Function Declaration – It is also called function prototype.  It tells the compiler the name, return type, and type of the parameters of the function.  The declaration of the function is called its prototype.

Function Definition – tells the compiler how the function works.

Syntax:           Return_Type    Function_Name(Argument Lists);

1.)    Function with void as return type and without argument list
Input
Process
Output
User-Defined Function
User-Defined Function
User-Defined Function

Example:         void highest();

2.)    Function with void as return type and with argument list
Input
Process
Output
main
Function
User-Defined Function
User-Defined Function

Example:         void highest(int num1, int num2);

3.)    Function with other data type as return type and without argument list
Input
Process
Output
User-Defined Function
User-Defined Function
main
 Function

Example:         int highest();

4.)    Function with other data type as return type and with argument list
Input
Process
Output
main
Function
User-Defined Function
main
 Function

Example:         int highest(int num1, int num2);



Sample Program: Create a C++ program that will input two numbers and output the highest number.  Implement this using function.


 























 















































 






















Function Call by Reference

Remember in the previous discussion that using function declaration void return type and with argument list that Input should be in main function, process and output should be in user defined function; Using Function Call by Reference Input is still in main, Process is in user defined function, but this time output is in the main function.

Note that in the following example, there is no Function declaration since the function definition is written before the main function.  In such case, no need to write the function declaration.

Example Number1:

#include

void byReference(int &num1,int &num2)
{
   num1 = num1 + 1;
   cout<< "num1 call by reference"<<
   num2 = num2 + 1;
   cout<<"num2 call by reference"<<
}

int main()
{ int num1, num2;

  num1 = 5;
  num2 = 6;

  cout<< "num1 before function call =  "<<
  cout<<"num2 before function =  "<<
 
  byReference(num1, num2);
 
  cout<< "num1 after function call =  "<<
  cout<<"num2 after function call =  "<<
  return 0;
}











Example Number2:


/*This program will also prompt the user to input two numbers and output he highest number. Notice that it uses function declaration 2 but the Output is in the main function.
*/
#include

void byReference(int num1,int num2, int &high)
{
  if(num1 > num2)
              high=num1;
  else
              high=num2;
}

int main()
{ int num1, num2, high=0;
 
  cout<<"Input two numbers: ";
  cin>>num1>>num2;
 
  byReference(num1, num2, high);
 
  cout<<"highest number = "<<
 
  return 0;
}














/* This is a program that will prompt the user to input ten numbers and output how many odd and even number were entered.  It uses function declaration with void as return type, and with three argument lists, first argument is the number entered, second argument is passed by reference, third argument is also passed by reference*/

#include

void oddeven(int num,int &ctrodd, int &ctreven)
{
   if (num % 2 == 0)
               ctreven++;
   else
               ctrodd++;
}

int main()
{ int num, ctr, ctreven=0, ctrodd=0;

  cout<<"Input ten numbers: "<
  for(ctr=1;ctr<=10;ctr++)
  { cin>>num;
    oddeven(num, ctrodd, ctreven);
  }
 
  cout<<"ctrodd =  "<<
  cout<<"ctreven =  "<<
  return 0;
}


















/* This program will input values for rows and columns to be able to produce a multiplication table.  Use function declaration with void as return type and two parameters row and column which is passed by value.*/


#include

void multtable(int row, int column)

{ int ans;

   for(int c=1;c<=column;c++)
               cout<<”\t”<
   cout<

   for(int r=1;r<=row;r++)
   {  cout<
               for(int c=1;c<=column;c++)
                        {  ans=r*c;
                                    cout<<”\t”<
                        }
   cout<
   }
}

int main()
{ int row, column;

  cout<<"Input rows and columns: "<
  cin>>row>>column;
  
    multtable(row, column);
  return 0;
}