+ All Categories
Home > Education > C++ Chapter IV

C++ Chapter IV

Date post: 22-May-2015
Category:
Upload: sorn-chanratha
View: 356 times
Download: 1 times
Share this document with a friend
Description:
មេរៀន C++/ C plus plus
Popular Tags:
18
1 Chapter IV Lecturer: Hong Mom
Transcript
Page 1: C++ Chapter IV

1  

 Chapter  IV  

     Lecturer:  Hong  Mom  

Page 2: C++ Chapter IV

Objec9ve  

Lecturer:  Hong  Mom   2  

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  

Page 3: C++ Chapter IV

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  

Page 4: C++ Chapter IV

Func9on  

•  Func9on  is  normally  consist  these  factors  as  below  

   type        name  (parameter1,  paramter2,…)  {    statements;  

}  

Lecturer:  Hong  Mom   4  

Type   Name   Parameters   Statements  

Page 5: C++ Chapter IV

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  

Page 6: C++ Chapter IV

(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  

Func9on  

Page 7: C++ Chapter IV

Example1  

Lecturer:  Hong  Mom   7  

#include<iostream.h>  int      sum2Num(int  a,  int    b)  {  

 int    s;    s  =  a  +  b;    return  (s);  

}  int      main(){  

 int    r;    r    =  sum2Num(5,8);    cout  <<  “The  result  is    “  <<  r;    return  0;  

}    

The  result  is  13                      

Page 8: C++ Chapter IV

Example2  

Lecturer:  Hong  Mom   8  

•  Way1  #include  <iostream.h>    void  displayNum(int  num)  {    

 num  =  0;      cout  <<  "num  =  "  <<  num  <<  '\n';  

 }    int  main  (void)  {    

 int  x  =  10;      displayNum  (x);    cout  <<  "x  =  "  <<  x  <<  '\n';  return  0;    

}      

Output:    num  =  0;    x  =  10;                  

Page 9: C++ Chapter IV

Example2  

Lecturer:  Hong  Mom   9  

•  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';  

 }        

Page 10: C++ Chapter IV

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  

Page 11: C++ Chapter IV

Scope  of  variables  

Lecturer:  Hong  Mom   11  

#include  <iosteam.h>    int      x;  char    ch;    main()  {  

 int  age;    float  number;  

   cout  <<  “Enter  your  age:  “;    cin  >>  age;    …  

}    

Global  variables  

Local  variables  

Page 12: C++ Chapter IV

Call  of  Func9on  

Func&on  Call  by  value  

#include  <iostream.h>  #include<iostream.h>  void  swap(int  x,  int  y){  

 int  temp  =  x;    x    =  y;    y  =  temp;  

}      

Func&on  Call  by  Reference  

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

 int  temp  =  x;    x    =  y;    y  =  temp;  

}      

Lecturer:  Hong  Mom   12  

Page 13: C++ Chapter IV

Call  of  Func9on  

Func&on  Call  by  value                      a:10    and  b:  20    

void  main()  {                int  a  =  10;                  int  b  =  20;                      swap(a,  b);                    cout<<”a:  ”  <<  a<<”        and  b:  ”    <<b;      }  

Func&on  Call  by  Reference      a:20    and  b:  10    

Lecturer:  Hong  Mom   13  

Page 14: C++ Chapter IV

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  

Page 15: C++ Chapter IV

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  

Page 16: C++ Chapter IV

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  

Page 17: C++ Chapter IV

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  

Page 18: C++ Chapter IV

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  


Recommended