Thursday, January 5, 2012

Local And Global Variables


Local And Global Variables
The variables in general may be classified as local or global variables.
 (a) Local variables  Identifiers declared as label, const, type, variables and functions in a block are said to a particular block are said to belong to a particular block or function and these identifiers are known as the local parameters or variables. Local variables are defined inside a function block or a compound statement.
   For example,
        void funct (int i, int j)
      {
           int k,m; / / local variables





                       / / body of the function
       }
  The integer variables k and m are defined within a function block of the funct (). All the variables to be used within a function block must be either defined at the beginning of the block itself or before using in a statement. Local variables are referred only the particular part of a block or a function. Same variable name may be given to different parts of a block or a function. Some variable will be treated as a different entity.
   For example,
          functionl (float a, float b)
        {
               float x,y; / / local variable
               x = 10.101;
               y = 20.20;



                / / body of the function 1
        }
        function2 (int i, int j)
        }
              float x,y; / / local to the function 2
              x = 1.111;
              y = 2.2222;


                              
              / / body of the function 2
        }
(b)  Global variables  Global variables are variables defined outside the main function block. These variables are referred by the same data type and by the same name through out the program and in the calling portion of a program and in the function block. Whenever some of the variables are treated as constants in both the main and the function block, it is advisable to use global variables.
    For example,
          int x,y = 4; / / global declaration
        void main (void)
        {
               void functionl ( );
               x = 10;


               
               functionl ( );
        }
        functionl ( );
        {
               int sum;
               sum = x+y;


                  
        }

PROGRAM
A program to find the  sum of the given two numbers using the global variable declaration.
    / / global variables declaration
   #includ <iostream.h>
   int x;
   int y = 5;
   void main (void);
      {
          x = 10;
          void sum (void);
          sum ( );
      }
void sum (void)
{
   int sum;
   sum = x+y;
   cout << “ x = “ << x << endl ;
   cout << “ y = “ << y << endl ;
   cout << “ sum = “ << sum << endl ;
}
Output of the above program
   x = 10
   y = 5
  sum = 15

Actual And Formal Arguments


Actual And Formal Arguments
The arguments may be classified under two groups, actual and formal arguments.

(a)  Actual arguments  An actual argument is a variable or an expression contained in a function call that replaces the formal parameter which is a part of the function declaration.
      Sometimes, a function may be called by a portion of a program with some parameters and these parameters are known as the actual arguments.
      For example,
             #include <iostream.h>
           void main ()
           {
                 int x,y;
                 void output (int x, int y) ; / / function declaration 
                                             
            


                  output (x,y) ; / / x and y are the actual arguments
           }
(b)  Formal arguments  Formal arguments are the parameters presents in a function definition which may also be called as dummy arguments or the parametric variables. When the function is invoked, the formal parameters are replaced by the actual parameters.
     For example,
          #include <iostream.h>
         void main ()
           {
                 int x,y;
                 void output (int x, int y) ;



                output (x,y) ;
           }
           void output (int a, int b)  / / formal or dummy arguments
           {
                   / /body of the function
           }
  Formal arguments may be declared by the same name or by different names in calling a portion of the program or in a called function but the data types should be the same in both blocks.
For example, the following function declaration is invalid.
       #include <iostream.h>
      void main ()
      {
            void funct (int x, int y, char s1, char s2) ;
            int x,y;
            char s1, s2;


                         
               funct (x,y,s1,s2) ;
      }
      funct (char c1, char c2, int a, int b) / / data type mismatch
      {
             / / body of a function
      }

Wednesday, January 4, 2012

Types Of Functions


Types Of Functions
The user defined functions may be classified in the following three ways based on the formal arguments passed and the usage of the return statement, and based on that, there are three types of user defined functions, Type 1 to Type 3.
     (a)   A function is invoked without passing any formal argument from the calling    portion of a program and also the function does not return back any value to the called function.
      (b)  A function is invoked with formal arguments from the calling portion of a program but the function does not return back any value to the calling portion.
      (c)  A function is invoked with formal arguments from the calling portion of a program which return back a value to the calling environment.
Type 1   It is the simplest way of writing a user defined function in C++. There is no data communication between the calling portion of a program and a called function block. The function is invoked by a calling environment by not passing any formal argument and the function also does not return back any value to the caller.
     For example,
          #include <iostream.h>
        void main (void)
        {
              void massage (void) / / function declaration
              massage ( ) ; function calling
        }
        void massage (void) / / function definition
        {

                                                                 
     

  / / body of the function
           }
Type 2   The second type of user defined function passes some formal arguments to a function but the function does not return back any value to the caller. It is an one way data communication between a calling portion of the program and the function block.
   For example,
   #include <iostream.h>
   void main (void)
  {
      void power  (int  ,int) ;  / / function declaration
      int x, y ;         
      power (x, y) ;    / / function calling
  }
  void power (int x, int y)
  {



 
              / / body of the function
      / / no value will be transferred back to the caller
  }

 PROGRAM
A program to find the square of its number using a function declaration without using the return statement.
   / /example 4.3
   / /  passing formal arguments and no return statement
   #include <iostream.h>
   void main (void)
   { 
       void square (int) ;  / / function declaration
       int max;
       cout << “enter a value for n ?  \n” ;
       cin >> max;
       for ( int I =0 ; i <= max-1; ++i)
             square (i) ;
    }
    void square (int n) / / function definition
    {
        float value;
        value =n*n;
        cout << “ I = “ << n << “ square = “ << value << endl ;
    }
Output of the above program
   enter a value for n ?
   4
   i = 0  square = 0
   i = 1  square = 1
    i = 2  square = 4
   i = 3  square = 9

Type 3 The third type of user defined function passes some formal arguments to a function from a calling portion of the program and the compound value, if any, is transferred back to the caller. Data are communicated between both the calling portion of a program and the function block.
    For example,
    #include <iostream.h>
   void main (void)
   {
      int output (int, int, char) ; / / function declaration
      int x, y, temp;
      char ch;


       
       temp = output (x, y, ch) ;
  }
  int output (int a, int b, char s)
  {
      int value;


       
       / / body of the function
      return (something) ;
   }
PROGRAM
A program to find the square of its number using a function declaration with the return statement.
      / / using return statement
     #include <iostream.h>
     void main (void)
    {
        float square (float) ;  / / function declaration
        float i, max, value ;
        max = 1.5 ;
        i = -1.5 ;
        while ( I <= max)  {
              value = square (i) ;
              cout << “ I = “ << I << “ square = “ << value <<endl ;
              I = I + 0.5 ;
     }
}
float square (float n) / / function definition
{
    float value ;
    value = n*n ;
    return (value) ;
}
PROGRAM
A program to find the sum of the given three numbers using function declaration with the return statement.
     / / demonstration of return statement
    #include <iostream.h>
void main (void)
{
    float sum (float, float, float) ;  / / function declaration
    float x, y, z;
    cout << “ enter any three numbers \n” ;
    cin >> x >> y >> z ;
    float total ;
    total = sum (x, y, z) ;
    cout  << “ sum of x, y and z = “ << total << endl ;
}
float sum (float a ,float b, float c)  / / function definition
{
    float temp;
    temp = a+b+c;
    return (temp) ;
}
Output of the above program
   enter any three numbers
   1  2  3 
   Sum of x, y and z = 6
PROGRAM
A program to find factorial of a given number using function declaration.
     / /factorial of a given number
    #include <iostream.h>
    void main (void)
    {
        long int fact (int) ;
        int x, n;
        cout << “Enter any integer number” << endl ;
        cin >>n;
        x = fact (n) ;
        cout << “value = “ << n << “and its factorial = “ ;
        cout << x << endl ;
   }
   long int fact (int n) / / function definition
  {
      int value = 1 ;
      if ( n == 1 )
           return (value  ) ;
     else
          {
               for (int I =1 ; i<=n ; ++i)
                     value = value*i ;
                     return (value) ;
           }
   }
Output of the above program
    enter any integer number
    value = 5 and its factorial = 120
   If the entered number is 1, then the function will return a factorial value 1, else, it will multiply the number n times by incrementing it by 1 in every time.
(1)                                                             (2)
Enter value 1                                  Enter value 2
value = 1                                       Value = 1*1
                                                     value = 1*2
                                                     value = 2
(3)
Enter value 3
= 1*1
= 1*2
= 2*3
Value = 6
And so on, it will calculate the factorial for any number.
PROGRAM
A program to find sum of the following series using a function declaration.
  Sum = x-(x3)/3! + (x5)/5! -… (xn)/n! Where x and n are entered from the keyboard.
 / /sum = x- (x3) /3 ! + (x5) / 5 ! -… (xn) / n !
#include <iostream.h>
{
    long int fact (int) ; / / function decleration
    float power (float, int) ;
    float sum, temp, x, pow;
    int sign, i, n;
    long int factyal ;
    cout << “Enter a value for n ? “<< endl ;
    cin >> n ;
    cout << “Enter a value for x ? “<< endl ;
    cin >> x ;
    i = 3 ;
    sum = x ;
    sign = 1 ;
    while (i <= n)  {
             factval = fact (i) ;
             pow = power (x, i) ;
             sign = (-1) *sign ;
             temp = sign*pow/ factval ;
             sum = sum +temp ;
             i = i+2 ;
    }
     cout << “sum of x-(x^3)/3! + (x^5)/5! -... = “ << sum ;
}
long int fact (int max)
{
    long int value ;
    value = 1 ;
    for (int i = 1; I <= max; ++i)  {
          value = value*i ;
    }
    return (value) ;
}
float power (float x, int n)
{
       float value2 ;
       value2 = 1 ;
       for ( int j = 1 ; j <= n; ++j)
             value2 = value2 * x;
             return (value2) ;
}
Output of the above program
      Enter a value for n ?
      7
      Enter value for x ?
      1
      Sum of x-(x^3)/3! + (x^5)/5! _... = 0.841468

PROGRAM
A program to find the sum of a given non-negative integer of n numbers where n is defined by the programmer using a function declaration.
    Sum= 1+2+3+4…n
     / / sum = 1+2+3…n using non-recursive function
    #include <iostream.h>
    vid main ( void)
 {
        int sum (int) ;
        int x,n;
        cout << “Enter any integer number” << endl;
        cin >> n;
        cout << “value = “ << n << “ and its sum = “ << sum(n) ;
  }
  int sum (int n)
  {
    int value = 0 ;
    if (n == 0)
         return (value) ;
     else
          {
          for (int I = 1; i <= n; ++i)
               value += i;
               return (value) ;
          }
  }
Output of the above program
    Enter any integer number
   10
    Value = 10 and its sum = 55

Monday, January 2, 2012

Defining A Function


DEFINING A FUNCTION
A function definition has a name, a parentheses pair containing zero or more parameters and a body. For each parameter, there should be a corresponding declaration that occurs before the body. Any parameter not declared is taken to be an int by default. It is good programming practice to declare all parameters.
  
     Recently compilers such as ANSIC may permit including the argument declaration within the parentheses :
     The general format of the function definition is given below.
                 funtiontype funtionname (datatype argumentl, datatype argument2...)
                {
                       body of function



                                                 
                      return something
                }
(a) Declaring the type of a function     The function refers to the type of value it would return to the calling portion of the program. Any of the basic data types such as int, float, char etc. may appear in the function declaration. In many compilers, when a function is not supposed to return any value, it may be declaration. In many compilers, when a function is not supposed to return any value, it may be declared as type void, which informs the compiler not to save any temporary space for a value to be sent back to the calling program.
For example,
      void function_name ( . . . )
      int function_name ( . . . )
      float function_name ( . . . )      
      char function_name ( . . . )      

(b)  Function name  The function name can be any name conforming to the syntax rules of the variables. Normally, a function name is made relevant to the function operation, as it will be easy to keep a track of it, whenever a transfer of similar function is used in the main program.
  For example,
        counter  ( ) ;
        square ( ) ;
        display_value ( ) ;
        output ( ) ;

(c)  Formal arguments   Any variable declared in the body of a function is said to be local to that function. Other variables which are not declared either as arguments or in the function body, are considered “global” to the function and must be defined externally. The storage classes or scope of variables are discussed subsequently in this chapter.
   For example,
          counter ( ) ;
        square ( ) ;
        display_value ( ) ;
        output ( ) ;
(c)  Formal arguments   Any variable declared in the body of a function is said to be local to that function. Other variables which are not declared either as arguments or in the function body, are considered “global” to the function and must be defined externally. The storage classes or scope of variables are discussed subsequently in this chapter.
     For example,
     (1)
              void square (int a, int b)
               / / a, b are the formal arguments
               {



                   }
      (2)
               int counter ( float xl, float x2, int y1, int y2)
               {               / / x1, x2, y1, and y2 are the formal arguments


                                               
                          return (int value) ;
               }
      (3) 
               float output (void) / / function without formal argument
            {
                   statement;



                        return (float value) ;

            }
             
(d)  Function body  After declaring the type of a function, function name and formal arguments, a statement or a block of statements is enclosed between the beginning and the end statements. In C++, each function is declared almost like a main( ) function.
    For example,
           void all_add (int a, int c, float x, float y)
         {
                int i, j, n ;     / / local variables , if any




                    
           }
PROGRAM
A program to demonstrate a function declaration, function calling and function definition in C++.
  #include <iostream.h>
  void main (void)
  {
      void display (void) ; / / function declaration
      display ( ) ; / / function definition
  }
  void display (void) / / function definition
  }
      cout << “ this is a test program \n”;
      cout << “ for demonstrating a function call\n”;
      cout << “ in C++ \n”;
  }
  Output of the above program
       this is a test program
      for demonstrating a function call
      in C++
      The main function invokes the display ( ) function. In C++, each function is almost like a separate program. So the formal declaration such as type, being and end must be used in the function also. The control will be transferred from the main function or from a function to the called function. After it has executed all the statements in the function the control switches back to the calling portion of the program.

Sunday, January 1, 2012

Return Statement


Return Statement
The keyword return is used to terminate function and return a value to its caller. The return statement may also be used to exit a function without returning a value. The return statement may or may not include an expression.
     The general syntax of the return statement is,
                     return;
                     return (expression );
     The return is a full-fledged C++ statement that can appear anywhere within a function body. A function can also have more then one return although it is good programming style for a function to have a single entrance and a single exit.
    Following are some valid return statement:
           return;
         return  (54) ;
           return  (x+y) ;
         return  (++i) ;
         return  ++j ;      /* correct , but not good style */
   The return statement terminates the execution of the function and pass on the control back to the calling environment.
    For example, a few valid function declarations are,
    (1)
          int sum (int x, int y)
        {
               Return (x+y) ;    / / return int value
        }
    (2)
           float maximum (float a, float b)
         {
              if ( a > b)
                   return (a) ;
              else
                   return (b) ;
         }   / / return floating point value
PROGRAM
A program to find the maximum of any three numbers using multiple return statement in a function definition.
/ /  using multiple return statements in a function
#include <iostream.h>
void main (void)
{
    float maximum (float, float, float) ;
    float x, y, z, max;
    cout << “ enter three numbers \n” ;
    cin >> x >> y >> z ;
    max = maximum (x, y, z) ;
    cout << “ maximum = “ << max;
}
float maximum (float a, float b, float c)
{
    if  (a > b)  {
         if  (a > c)
              return (a) ;
         else
              return (c) ;
    }
    else  {
            if (b > c)
                return (b) ;
           else
                return (c) ;
     }
}  / / end of function
Output of the above program
    enter three numbers
    4    5   6
    maximum = 6