Chapter	
  IV   	
  
                               	
  
             	
  
Lecturer:	
  Hong	
  Mom	
  
             	
  


                                      1	
  
Objec9ve	
  
A=er	
  completely	
  this	
  chapter,	
  you	
  will	
  be	
  able	
  to:	
  
•  Define	
  func9on	
  
•  Define	
  Scope	
  role	
  
•  Construct	
  func9on	
  headers	
  and	
  prototypes	
  
•  Call	
  by	
  value	
  and	
  Call	
  by	
  reference	
  
•  Define	
  storage	
  class	
  specifiers	
  




                                    Lecturer:	
  Hong	
  Mom	
                   2	
  
Func9on	
  
	
  
•  What	
  is	
  Func9on?	
  
A	
  func&on	
  is	
  a	
  block	
  of	
  code	
  that	
  performs	
  a	
  
calcula9on	
  and	
  returns	
  a	
  value.	
  Once	
  a	
  func9on	
  
has	
  been	
  wriNen	
  to	
  play	
  a	
  par9cular	
  role	
  it	
  can	
  
be	
  called	
  upon	
  repeatedly	
  throughout	
  the	
  
program.	
  
•  Also	
  called	
  a	
  subrou&ne,	
  procedure,	
  or	
  
     method	
  


                                Lecturer:	
  Hong	
  Mom	
                       3	
  
Func9on	
  
•  Func9on	
  is	
  normally	
  consist	
  these	
  factors	
  as	
  
      below	
  
	
                            Name	
   Parameters	
   Statements	
  
            Type	
  
	
  
type	
  	
  	
  	
  name	
  (parameter1,	
  paramter2,…)	
  
{	
  
      	
  statements;	
  
}	
  

                              Lecturer:	
  Hong	
  Mom	
                4	
  
Func9on	
  
•  type	
  is	
  the	
  data	
  type	
  specifier	
  of	
  the	
  data	
  
   returned	
  by	
  the	
  func9on.	
  	
  
•  name	
  is	
  the	
  iden9fier	
  by	
  which	
  it	
  will	
  be	
  
   possible	
  to	
  call	
  the	
  func9on.	
  	
  
•  parameters	
  (as	
  many	
  as	
  needed):	
  Each	
  
   parameter	
  consists	
  of	
  a	
  data	
  type	
  specifier	
  
   followed	
  by	
  an	
  iden9fier,	
  like	
  any	
  regular	
  
   variable	
  declara9on	
  	
  

                              Lecturer:	
  Hong	
  Mom	
                    5	
  
Func9on	
  
(for	
  example:	
  int	
  x)	
  and	
  which	
  acts	
  within	
  the	
  
func9on	
  as	
  a	
  regular	
  local	
  variable.	
  They	
  allow	
  
to	
  pass	
  arguments	
  to	
  the	
  func9on	
  when	
  it	
  is	
  
called.	
  The	
  different	
  parameters	
  are	
  separated	
  
by	
  commas.	
  	
  
•  statements	
  is	
  the	
  func9on's	
  body.	
  It	
  is	
  a	
  block	
  
     of	
  statements	
  surrounded	
  by	
  braces	
  {	
  }.	
  	
  
	
  

                                 Lecturer:	
  Hong	
  Mom	
                      6	
  
Example1	
  
#include<iostream.h>	
  
int	
  	
  	
  sum2Num(int	
  a,	
  int	
  	
  b)	
  
{	
                                                                                The	
  result	
  is	
  13	
  
                 	
  int	
  	
  s;	
                                               	
  
                 	
  s	
  =	
  a	
  +	
  b;	
                                      	
  
                                                                                   	
  
                 	
  return	
  (s);	
                                              	
  
}	
                                                                                	
  
int	
  	
  	
  main(){	
                                                           	
  
                                                                                   	
  
                 	
  int	
  	
  r;	
                                               	
  
                 	
  r	
  	
  =	
  sum2Num(5,8);	
                                 	
  
                 	
  cout	
  <<	
  “The	
  result	
  is	
  	
  “	
  <<	
  r;	
     	
  
                 	
  return	
  0;	
  
}	
  
	
                                                  Lecturer:	
  Hong	
  Mom	
                                     7	
  
Example2	
  
•  Way1	
  
                                                                                                 Output:	
  
#include	
  <iostream.h>	
  	
  
                                                                                                 	
  
void	
  displayNum(int	
  num)	
  {	
  	
  
                                                                                                 num	
  =	
  0;	
  	
  
              	
  num	
  =	
  0;	
  	
  
                                                                                                 x	
  =	
  10;	
  	
  
              	
  cout	
  <<	
  "num	
  =	
  "	
  <<	
  num	
  <<	
  'n';	
  
                                                                                                 	
  
	
  }	
  	
                                                                                      	
  
int	
  main	
  (void)	
  {	
  	
                                                                 	
  
              	
  int	
  x	
  =	
  10;	
  	
                                                     	
  
                                                                                                 	
  
              	
  displayNum	
  (x);	
                                                           	
  
              	
  cout	
  <<	
  "x	
  =	
  "	
  <<	
  x	
  <<	
  'n';	
  return	
  0;	
  	
     	
  
}	
  	
  
	
  

                                                     Lecturer:	
  Hong	
  Mom	
                                           8	
  
Example2	
  
•  Way2	
  
#include	
  <iostream.h>	
  	
  
void	
  displayNum(int	
  num);	
  //Func9on	
  Declara9on	
  or	
  Prototype	
  
int	
  main	
  (void)	
  {	
  	
  
              	
  int	
  x	
  =	
  10;	
  	
  
              	
  displayNum	
  (x);	
  
              	
  cout	
  <<	
  "x	
  =	
  "	
  <<	
  x	
  <<	
  'n';	
  return	
  0;	
  	
  
}	
  	
  
	
  
void	
  displayNum(int	
  num)	
  {	
  	
  
              	
  num	
  =	
  0;	
  	
  
              	
  cout	
  <<	
  "num	
  =	
  "	
  <<	
  num	
  <<	
  'n';	
  
	
  }	
  	
  
	
                                                     Lecturer:	
  Hong	
  Mom	
              9	
  
Global	
  and	
  Local	
  Scope	
  
•  Global	
  Scope	
  
	
  	
  	
  	
  	
  	
  	
  -­‐	
  Everything	
  defined	
  at	
  the	
  program	
  scope	
  level	
  (i.e.,	
  
outside	
  func9ons	
  and	
  classes)	
  is	
  said	
  to	
  have	
  a	
  global	
  
scope	
  	
  
	
  	
  	
  	
  	
  	
  	
  -­‐	
  Can	
  be	
  accessed	
  by	
  any	
  func9on	
  defined	
  below	
  the	
  
declara9on	
  
•  Local	
  Scope	
  
	
  	
  	
  	
  	
  	
  	
  -­‐	
  Each	
  block	
  in	
  a	
  program	
  defines	
  a	
  local	
  scope	
  .	
  	
  
	
  	
  	
  	
  	
  	
  -­‐	
  	
  In	
  general	
  variables	
  declared	
  inside	
  a	
  block	
  are	
  
accessible	
  only	
  in	
  that	
  block	
  

                                             Lecturer:	
  Hong	
  Mom	
                                        10	
  
Scope	
  of	
  variables	
  
#include	
  <iosteam.h>	
  
	
  
int	
  	
  	
  x;	
  
char	
  	
  ch;	
                                                  Global	
  variables	
  
	
  
main()	
  
{	
  
        	
  int	
  age;	
  
                                                                          Local	
  variables	
  
        	
  float	
  number;	
  
	
  
        	
  cout	
  <<	
  “Enter	
  your	
  age:	
  “;	
  
        	
  cin	
  >>	
  age;	
  
        	
  …	
  
}	
                                 Lecturer:	
  Hong	
  Mom	
                                11	
  
Call	
  of	
  Func9on	
  
Func&on	
  Call	
  by	
  value	
                           Func&on	
  Call	
  by	
  Reference	
  

#include	
  <iostream.h>	
                                 #include	
  <iostream.h>	
  
#include<iostream.h>	
                                     #include<iostream.h>	
  
void	
  swap(int	
  x,	
  int	
  y){	
                     void	
  swap(int	
  &x,	
  int	
  &y){	
  
          	
  int	
  temp	
  =	
  x;	
                               	
  int	
  temp	
  =	
  x;	
  
          	
  x	
  	
  =	
  y;	
                                     	
  x	
  	
  =	
  y;	
  
          	
  y	
  =	
  temp;	
                                      	
  y	
  =	
  temp;	
  
}	
  	
                                                    }	
  	
  
	
                                                         	
  

                                           Lecturer:	
  Hong	
  Mom	
                                   12	
  
Call	
  of	
  Func9on	
  
void	
  main()	
  {	
  	
  
	
  	
  	
  	
  	
  	
  int	
  a	
  =	
  10;	
  	
  	
  
	
  	
  	
  	
  	
  	
  int	
  b	
  =	
  20;	
  	
  	
  	
  	
  
	
  	
  	
  	
  	
  	
  swap(a,	
  b);	
  	
  	
  	
  
	
  	
  	
  	
  	
  	
  cout<<”a:	
  ”	
  <<	
  a<<”	
  	
  	
  	
  and	
  b:	
  ”	
       	
  <<b;	
  	
  	
  
}	
  
Func&on	
  Call	
  by	
  value	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  a:10	
  	
  and	
  b:	
  20	
  
	
  
  Func&on	
  Call	
  by	
  Reference	
  	
  	
  a:20	
  	
  and	
  b:	
  10	
  
  	
  

                                                            Lecturer:	
  Hong	
  Mom	
                            13	
  
Storage	
  class	
  specifiers	
  
•  A	
  storage	
  class	
  specifier	
  is	
  used	
  to	
  refine	
  the	
  
   declara9on	
  of	
  a	
  variable	
  in	
  memory.	
  
•  The	
  storage	
  class	
  specifiers	
  in	
  	
  C++	
  are:	
  
    –  Auto	
  
    –  Register	
  
    –  Sta9c	
  
    –  Extern	
  




                                Lecturer:	
  Hong	
  Mom	
                     14	
  
Auto	
  Variables	
  
Because	
  the	
  life9me	
  of	
  a	
  local	
  variable	
  is	
  limited	
  and	
  is	
  
determined	
  automa9cally,	
  these	
  variables	
  are	
  also	
  
called	
  automa&c.	
  The	
  storage	
  class	
  specifier	
  auto	
  may	
  
be	
  used	
  to	
  explicitly	
  specify	
  a	
  local	
  variable	
  to	
  be	
  
automa9c.	
  For	
  example:	
  	
  
	
  
void	
  showMe(void)	
  {	
  	
  
     	
  auto	
  int	
  xyz;	
  //	
  same	
  as:	
  int	
  xyz;	
  	
  
//...	
  }	
  	
  
	
  
This	
  is	
  rarely	
  used	
  because	
  all	
  local	
  variables	
  are	
  by	
  
default	
  automa9c.	
  	
  
                                    Lecturer:	
  Hong	
  Mom	
                            15	
  
Register	
  variable	
  
•  Used	
  to	
  indicate	
  to	
  the	
  compiler	
  that	
  the	
  variable	
  
     should	
  be	
  stored	
  in	
  a	
  register	
  if	
  possible.	
  
•  The	
  scope	
  of	
  register	
  variables	
  is	
  local	
  to	
  the	
  
     block	
  in	
  which	
  they	
  are	
  declared.	
  
•  fast	
  
	
  For	
  example:	
  	
  
      	
  register	
  int	
  var;	
  	
  


                                 Lecturer:	
  Hong	
  Mom	
                    16	
  
Sta9c	
  variable	
  
•  Preserve	
  the	
  value	
  for	
  a	
  par9cular	
  variable	
  upon	
  
                re-­‐entry	
  into	
  the	
  same	
  func9on.	
  
For	
  example:	
  
void	
  sta9c_func9on_example()	
  
	
  	
  	
  	
  {	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  sta9c	
  int	
  x	
  =	
  0;	
  //variable	
  for	
  C++	
  tutorial	
  
example	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  x++;	
  
	
  	
  	
  	
  	
  	
  	
  	
  cout	
  <<	
  x	
  <<endl;	
  
	
  	
  	
  }	
  
	
  
                                            Lecturer:	
  Hong	
  Mom	
                                     17	
  
Extern	
  variable	
  
•  Used	
  to	
  specify	
  that	
  the	
  variable	
  is	
  declared	
  in	
  
   a	
  different	
  file.	
  
•  Used	
  to	
  declare	
  variables	
  of	
  global	
  scope	
  in	
  C+
   +	
  projects	
  
•  When	
  the	
  keyword	
  extern	
  is	
  used,	
  the	
  
   compiler	
  will	
  not	
  allocate	
  memory	
  for	
  the	
  
   variable.	
  


                                 Lecturer:	
  Hong	
  Mom	
                  18	
  

C++ Chapter IV

  • 1.
      Chapter  IV       Lecturer:  Hong  Mom     1  
  • 2.
    Objec9ve   A=er  completely  this  chapter,  you  will  be  able  to:   •  Define  func9on   •  Define  Scope  role   •  Construct  func9on  headers  and  prototypes   •  Call  by  value  and  Call  by  reference   •  Define  storage  class  specifiers   Lecturer:  Hong  Mom   2  
  • 3.
    Func9on     • What  is  Func9on?   A  func&on  is  a  block  of  code  that  performs  a   calcula9on  and  returns  a  value.  Once  a  func9on   has  been  wriNen  to  play  a  par9cular  role  it  can   be  called  upon  repeatedly  throughout  the   program.   •  Also  called  a  subrou&ne,  procedure,  or   method   Lecturer:  Hong  Mom   3  
  • 4.
    Func9on   •  Func9on  is  normally  consist  these  factors  as   below     Name   Parameters   Statements   Type     type        name  (parameter1,  paramter2,…)   {    statements;   }   Lecturer:  Hong  Mom   4  
  • 5.
    Func9on   •  type  is  the  data  type  specifier  of  the  data   returned  by  the  func9on.     •  name  is  the  iden9fier  by  which  it  will  be   possible  to  call  the  func9on.     •  parameters  (as  many  as  needed):  Each   parameter  consists  of  a  data  type  specifier   followed  by  an  iden9fier,  like  any  regular   variable  declara9on     Lecturer:  Hong  Mom   5  
  • 6.
    Func9on   (for  example:  int  x)  and  which  acts  within  the   func9on  as  a  regular  local  variable.  They  allow   to  pass  arguments  to  the  func9on  when  it  is   called.  The  different  parameters  are  separated   by  commas.     •  statements  is  the  func9on's  body.  It  is  a  block   of  statements  surrounded  by  braces  {  }.       Lecturer:  Hong  Mom   6  
  • 7.
    Example1   #include<iostream.h>   int      sum2Num(int  a,  int    b)   {   The  result  is  13    int    s;      s  =  a  +  b;        return  (s);     }     int      main(){        int    r;      r    =  sum2Num(5,8);      cout  <<  “The  result  is    “  <<  r;      return  0;   }     Lecturer:  Hong  Mom   7  
  • 8.
    Example2   •  Way1   Output:   #include  <iostream.h>       void  displayNum(int  num)  {     num  =  0;      num  =  0;     x  =  10;      cout  <<  "num  =  "  <<  num  <<  'n';      }       int  main  (void)  {        int  x  =  10;          displayNum  (x);      cout  <<  "x  =  "  <<  x  <<  'n';  return  0;       }       Lecturer:  Hong  Mom   8  
  • 9.
    Example2   •  Way2   #include  <iostream.h>     void  displayNum(int  num);  //Func9on  Declara9on  or  Prototype   int  main  (void)  {      int  x  =  10;      displayNum  (x);    cout  <<  "x  =  "  <<  x  <<  'n';  return  0;     }       void  displayNum(int  num)  {      num  =  0;      cout  <<  "num  =  "  <<  num  <<  'n';    }       Lecturer:  Hong  Mom   9  
  • 10.
    Global  and  Local  Scope   •  Global  Scope                -­‐  Everything  defined  at  the  program  scope  level  (i.e.,   outside  func9ons  and  classes)  is  said  to  have  a  global   scope                  -­‐  Can  be  accessed  by  any  func9on  defined  below  the   declara9on   •  Local  Scope                -­‐  Each  block  in  a  program  defines  a  local  scope  .                -­‐    In  general  variables  declared  inside  a  block  are   accessible  only  in  that  block   Lecturer:  Hong  Mom   10  
  • 11.
    Scope  of  variables   #include  <iosteam.h>     int      x;   char    ch;   Global  variables     main()   {    int  age;   Local  variables    float  number;      cout  <<  “Enter  your  age:  “;    cin  >>  age;    …   }   Lecturer:  Hong  Mom   11  
  • 12.
    Call  of  Func9on   Func&on  Call  by  value   Func&on  Call  by  Reference   #include  <iostream.h>   #include  <iostream.h>   #include<iostream.h>   #include<iostream.h>   void  swap(int  x,  int  y){   void  swap(int  &x,  int  &y){    int  temp  =  x;    int  temp  =  x;    x    =  y;    x    =  y;    y  =  temp;    y  =  temp;   }     }         Lecturer:  Hong  Mom   12  
  • 13.
    Call  of  Func9on   void  main()  {                int  a  =  10;                  int  b  =  20;                      swap(a,  b);                    cout<<”a:  ”  <<  a<<”        and  b:  ”    <<b;       }   Func&on  Call  by  value                      a:10    and  b:  20     Func&on  Call  by  Reference      a:20    and  b:  10     Lecturer:  Hong  Mom   13  
  • 14.
    Storage  class  specifiers   •  A  storage  class  specifier  is  used  to  refine  the   declara9on  of  a  variable  in  memory.   •  The  storage  class  specifiers  in    C++  are:   –  Auto   –  Register   –  Sta9c   –  Extern   Lecturer:  Hong  Mom   14  
  • 15.
    Auto  Variables   Because  the  life9me  of  a  local  variable  is  limited  and  is   determined  automa9cally,  these  variables  are  also   called  automa&c.  The  storage  class  specifier  auto  may   be  used  to  explicitly  specify  a  local  variable  to  be   automa9c.  For  example:       void  showMe(void)  {      auto  int  xyz;  //  same  as:  int  xyz;     //...  }       This  is  rarely  used  because  all  local  variables  are  by   default  automa9c.     Lecturer:  Hong  Mom   15  
  • 16.
    Register  variable   • Used  to  indicate  to  the  compiler  that  the  variable   should  be  stored  in  a  register  if  possible.   •  The  scope  of  register  variables  is  local  to  the   block  in  which  they  are  declared.   •  fast    For  example:      register  int  var;     Lecturer:  Hong  Mom   16  
  • 17.
    Sta9c  variable   • Preserve  the  value  for  a  par9cular  variable  upon   re-­‐entry  into  the  same  func9on.   For  example:   void  sta9c_func9on_example()          {                    sta9c  int  x  =  0;  //variable  for  C++  tutorial   example                    x++;                  cout  <<  x  <<endl;        }     Lecturer:  Hong  Mom   17  
  • 18.
    Extern  variable   • Used  to  specify  that  the  variable  is  declared  in   a  different  file.   •  Used  to  declare  variables  of  global  scope  in  C+ +  projects   •  When  the  keyword  extern  is  used,  the   compiler  will  not  allocate  memory  for  the   variable.   Lecturer:  Hong  Mom   18