Functions

1,180 views

Published on

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

Published in: Technology, Health & Medicine
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,180
On SlideShare
0
From Embeds
0
Number of Embeds
95
Actions
Shares
0
Downloads
4
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Functions

  1. 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. 2. USE OF RETURN STATEMENT <ul><li>Control passes back to the main program. </li></ul><ul><li>It is used to return a value to the calling code. </li></ul><ul><li>Note: A function can return only a single value. </li></ul>
  3. 3. 3 TYPES OF FUNCTION IN C++ <ul><li>Computational Functions: Functions that calculate or compute some value and return the computed value. E.g. sqrt( ), area ( ). </li></ul><ul><li>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() </li></ul><ul><li>Procedural function : Functions that perform an action and have no explicit return value. E.g. exit(), clrscr( ) </li></ul>
  4. 4. RETURNING BY REFERENCE
  5. 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. 6. 4 kinds of scope in C++ <ul><li>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. </li></ul><ul><li>Function scope: can be accessed only in the fn. that declares them. </li></ul><ul><li>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. </li></ul><ul><li>Class Scope: has class scope. </li></ul>
  7. 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. 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. 9. OUTPUT 30 20 10 20 40 10
  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. 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. 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. 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. 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. 15. STORAGE CLASS SPECIFIERS <ul><li>auto </li></ul><ul><li>register </li></ul><ul><li>static </li></ul><ul><li>extern </li></ul><ul><li>All four can be applied to variables </li></ul><ul><li>Function can only have static or extern </li></ul>
  16. 16. STORAGE CLASS SPECIFIERS AND VARIABLES <ul><li>Auto </li></ul><ul><li>Syntax: auto type variable-name </li></ul><ul><li>By default the variables are auto type </li></ul><ul><li>Auto var is automatically created when the function in which the var is declared is called. And automatically destroyed when fn terminates. </li></ul><ul><li>Thus lifetime of the var is till the fn is running </li></ul><ul><li>Scope is function scope. </li></ul>
  17. 17. <ul><li>register </li></ul><ul><li>Same characteristics as auto </li></ul><ul><li>Different because register var provide fast access as they are stored inside CPU registers. </li></ul><ul><li>auto and register are only for local variables </li></ul>
  18. 18. Extern <ul><li>Only for global variables </li></ul><ul><li>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. </li></ul><ul><li>To use a global var of one file in another file we need to use extern. </li></ul><ul><li>Created and Initialized by the program when the file is first loaded </li></ul><ul><li>Given the value 0 if no value is given </li></ul><ul><li>Lifetime is throughout the program </li></ul><ul><li>Scope is file scope </li></ul>
  19. 19. static <ul><li>Can be for local and global variables both </li></ul><ul><li>Static local var is initialized when first call to fn is made </li></ul><ul><li>Not destroyed when fn terminates </li></ul><ul><li>Holds the value even after fn terminates </li></ul><ul><li>Lifetime of a global var </li></ul><ul><li>Scope is local to a function </li></ul>
  20. 20. static global var <ul><li>Useful when creating a global var only for the functions of a particular file </li></ul><ul><li>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. </li></ul><ul><li>Lifetime is program run </li></ul><ul><li>Scope is global var of a file </li></ul>
  21. 21. Storage Class Specifiers and Functions <ul><li>Static function: appears before fn declaration </li></ul><ul><ul><li>Fn can only be accessed in the file the declares it. </li></ul></ul><ul><ul><li>Another file can use the same name for a fn. </li></ul></ul><ul><li>Extern fn: similar to extern var. </li></ul><ul><ul><li>to indicate that the fn has been defined in </li></ul></ul><ul><ul><li>some other file. </li></ul></ul><ul><ul><li>Useful for programs spread across multiple files. </li></ul></ul>

×