RETURNING FROM A FUNCTION A FUNCTION TERMINATES WHEN EITHER A RETURN STATEMENT IS ENCOUNTERED OR THE LAST STATEMENT IN THE FUNCTION IS EXECUTED.
USE OF RETURN STATEMENT Control passes back to the main program. It is used to return a value to the calling code. Note:  A function can return only a single value.
3 TYPES OF FUNCTION IN C++ Computational Functions:  Functions that calculate or compute some value and return the computed value. E.g. sqrt( ), area ( ). Manipulative functions:  Functions that manipulate information and return a success or failure code. Generally, if  0 is returned - denotes success and any other number – denotes failure. E.g. strcmp() Procedural function :  Functions that perform an action and have no explicit return value. E.g. exit(), clrscr( )
RETURNING BY REFERENCE
SCOPE RULES The program part(s) in which a particular piece of code or a data value ( e.g. variable, function ) can be accessed is known as the piece-of-code’s or variable’s scope.
4 kinds of scope in C++ Local Scope : name declared in a block  ( { } ) is local to that block and can be used only in it and other blocks contained under it. Function scope:  can be accessed only in the fn. that declares them. File Scope / Global Scope:  name declared outside all blocks and functions – can be used in all the blocks and functions written inside the file in which the name declaration appears. Class Scope:  has class scope.
Variable Scope int g = 10; // global variable void main ( ) { clrscr( ); int m = 20;  // function scope void func ( void ); { int b = 30; // local scope cout << “ \n “ <<b; cout<< “ \n” << m; }
cout<< “ \n “ << g; cout<< “\n “<<m; cout<<“\n”<< b; // incorrect since var. of block cout<<”\n” << f; // incorrect var. of function func func ( ); getch (); } void func ( void ) { int f = 40; cout < < “ \n “ << f; cout << “ \n “<< g; cout << “ \n “<< m; // incorrect since var. of fn. main. }
OUTPUT 30 20 10 20 40 10
FUNCTION SCOPE void global-fn ( void ); // global / file scope void main ( ) { void internal-fn ( void );  // fn. Scope for main void  internal–fn-2 ( void ) // fn. Scope for main global-fn ( );  // can call global fn. internal-fn ( );  // fn. Of main internal-fn2( );  // fn. Of main sub-internal-fn ( );  // incorrect b/c outside the scope }
void global-fn( void ) { cout<<“ this is a global fn “; } void internal-fn ( void ) { void sub-internal-fn ( void ); // fn. Scope of internal-fn cout<< “ this is an internal fn. “; global-fn ( ); // can call global fn. sub-internal-fn( ); // can call b/c fn of internal-fn internal-fn-2 ( ); // incorrect b/c fn. of main } void sub-internal-fn ( void ) { cout<<“ sub internal fn”; }
void internal-fn-2 ( void ) { cout<< “ internal – fn –2 “; } OUTPUT This is global fn.  This is an internal fn. Global-fn  sub-internal fn  internal fn2
Example of same varibale name as Formal and Actual Parameters void main ( ) { clrscr( ); void same ( int ); int a = 50; same ( a ); cout<<“ \ n “ << a; getch(); } void same ( int a ) { a = 60; cout<< “ \n ‘ << a; }
Example of Local  and Global variable having the same name. int a = 20; void main ( ) { int a = 50; cout << “local var a : “<<a; cout<< “ global var a : “<< : : a; } OUTPUT:  50 20 NOTE:   You need to use : :  ( scope resolution operator to refer to Global variable

Functions

  • 1.
    RETURNING FROM AFUNCTION A FUNCTION TERMINATES WHEN EITHER A RETURN STATEMENT IS ENCOUNTERED OR THE LAST STATEMENT IN THE FUNCTION IS EXECUTED.
  • 2.
    USE OF RETURNSTATEMENT Control passes back to the main program. It is used to return a value to the calling code. Note: A function can return only a single value.
  • 3.
    3 TYPES OFFUNCTION IN C++ Computational Functions: Functions that calculate or compute some value and return the computed value. E.g. sqrt( ), area ( ). Manipulative functions: Functions that manipulate information and return a success or failure code. Generally, if 0 is returned - denotes success and any other number – denotes failure. E.g. strcmp() Procedural function : Functions that perform an action and have no explicit return value. E.g. exit(), clrscr( )
  • 4.
  • 5.
    SCOPE RULES Theprogram part(s) in which a particular piece of code or a data value ( e.g. variable, function ) can be accessed is known as the piece-of-code’s or variable’s scope.
  • 6.
    4 kinds ofscope in C++ Local Scope : name declared in a block ( { } ) is local to that block and can be used only in it and other blocks contained under it. Function scope: can be accessed only in the fn. that declares them. File Scope / Global Scope: name declared outside all blocks and functions – can be used in all the blocks and functions written inside the file in which the name declaration appears. Class Scope: has class scope.
  • 7.
    Variable Scope intg = 10; // global variable void main ( ) { clrscr( ); int m = 20; // function scope void func ( void ); { int b = 30; // local scope cout << “ \n “ <<b; cout<< “ \n” << m; }
  • 8.
    cout<< “ \n“ << g; cout<< “\n “<<m; cout<<“\n”<< b; // incorrect since var. of block cout<<”\n” << f; // incorrect var. of function func func ( ); getch (); } void func ( void ) { int f = 40; cout < < “ \n “ << f; cout << “ \n “<< g; cout << “ \n “<< m; // incorrect since var. of fn. main. }
  • 9.
    OUTPUT 30 2010 20 40 10
  • 10.
    FUNCTION SCOPE voidglobal-fn ( void ); // global / file scope void main ( ) { void internal-fn ( void ); // fn. Scope for main void internal–fn-2 ( void ) // fn. Scope for main global-fn ( ); // can call global fn. internal-fn ( ); // fn. Of main internal-fn2( ); // fn. Of main sub-internal-fn ( ); // incorrect b/c outside the scope }
  • 11.
    void global-fn( void) { cout<<“ this is a global fn “; } void internal-fn ( void ) { void sub-internal-fn ( void ); // fn. Scope of internal-fn cout<< “ this is an internal fn. “; global-fn ( ); // can call global fn. sub-internal-fn( ); // can call b/c fn of internal-fn internal-fn-2 ( ); // incorrect b/c fn. of main } void sub-internal-fn ( void ) { cout<<“ sub internal fn”; }
  • 12.
    void internal-fn-2 (void ) { cout<< “ internal – fn –2 “; } OUTPUT This is global fn. This is an internal fn. Global-fn sub-internal fn internal fn2
  • 13.
    Example of samevaribale name as Formal and Actual Parameters void main ( ) { clrscr( ); void same ( int ); int a = 50; same ( a ); cout<<“ \ n “ << a; getch(); } void same ( int a ) { a = 60; cout<< “ \n ‘ << a; }
  • 14.
    Example of Local and Global variable having the same name. int a = 20; void main ( ) { int a = 50; cout << “local var a : “<<a; cout<< “ global var a : “<< : : a; } OUTPUT: 50 20 NOTE: You need to use : : ( scope resolution operator to refer to Global variable