• Like
  • Save
Functions
Upcoming SlideShare
Loading in...5
×
Uploaded on

Functions, returning from a function, call by value, call by reference

Functions, returning from a function, call by value, call by reference

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
877
On Slideshare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
4
Comments
0
Likes
2

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. RETURNING FROM A FUNCTION A FUNCTION TERMINATES WHEN EITHER A RETURN STATEMENT IS ENCOUNTERED OR THE LAST STATEMENT IN THE FUNCTION IS EXECUTED.
  • 2. 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. 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( )
  • 4. RETURNING BY REFERENCE
  • 5. 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.
  • 6. 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.
  • 7. Variable Scope int g = 10; // global variable void main ( ) { clrscr( ); int m = 20; // function scope void func ( void ); { int b = 30; // local scope cout << “ “ <<b; cout<< “ ” << m; }
  • 8. cout<< “ “ << g; cout<< “ “<<m; cout<<“ ”<< b; // incorrect since var. of block cout<<” ” << f; // incorrect var. of function func func ( ); getch (); } void func ( void ) { int f = 40; cout < < “ “ << f; cout << “ “<< g; cout << “ “<< m; // incorrect since var. of fn. main. }
  • 9. OUTPUT 30 20 10 20 40 10
  • 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 }
  • 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 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<< “ ‘ << 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
  • 15. STORAGE CLASS SPECIFIERS
    • auto
    • register
    • static
    • extern
    • All four can be applied to variables
    • Function can only have static or extern
  • 16. STORAGE CLASS SPECIFIERS AND VARIABLES
    • Auto
    • Syntax: auto type variable-name
    • By default the variables are auto type
    • Auto var is automatically created when the function in which the var is declared is called. And automatically destroyed when fn terminates.
    • Thus lifetime of the var is till the fn is running
    • Scope is function scope.
  • 17.
    • register
    • Same characteristics as auto
    • Different because register var provide fast access as they are stored inside CPU registers.
    • auto and register are only for local variables
  • 18. Extern
    • Only for global variables
    • C++ program can be split into different files. Each file can contain some fns. These files are separately compiled and then later linked for program execution.
    • To use a global var of one file in another file we need to use extern.
    • Created and Initialized by the program when the file is first loaded
    • Given the value 0 if no value is given
    • Lifetime is throughout the program
    • Scope is file scope
  • 19. static
    • Can be for local and global variables both
    • Static local var is initialized when first call to fn is made
    • Not destroyed when fn terminates
    • Holds the value even after fn terminates
    • Lifetime of a global var
    • Scope is local to a function
  • 20. static global var
    • Useful when creating a global var only for the functions of a particular file
    • E.g. you want to create a global var for File A and do want it to be access in any other files linked with A. You need to make it static global var.
    • Lifetime is program run
    • Scope is global var of a file
  • 21. Storage Class Specifiers and Functions
    • Static function: appears before fn declaration
      • Fn can only be accessed in the file the declares it.
      • Another file can use the same name for a fn.
    • Extern fn: similar to extern var.
      • to indicate that the fn has been defined in
      • some other file.
      • Useful for programs spread across multiple files.