SlideShare a Scribd company logo
1 of 14
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 ,[object Object],[object Object],[object Object]
3 TYPES OF FUNCTION IN C++ ,[object Object],[object Object],[object Object]
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++ ,[object Object],[object Object],[object Object],[object Object]
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; }
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. }
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<< β€œ  β€˜ << 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

More Related Content

What's hot

Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programmingTejaswiB4
Β 
Storage Class Specifiers in C++
Storage Class Specifiers in C++Storage Class Specifiers in C++
Storage Class Specifiers in C++Reddhi Basu
Β 
Storage classes in C
Storage classes in C Storage classes in C
Storage classes in C Self employed
Β 
Header files in c
Header files in cHeader files in c
Header files in cHoneyChintal
Β 
Storage class in C Language
Storage class in C LanguageStorage class in C Language
Storage class in C LanguageNitesh Kumar Pandey
Β 
Function & Recursion
Function & RecursionFunction & Recursion
Function & RecursionMeghaj Mallick
Β 
More on Lex
More on LexMore on Lex
More on LexTech_MX
Β 
Storage classes in c++
Storage classes in c++Storage classes in c++
Storage classes in c++Jaspal Singh
Β 
Storage Classes and Functions
Storage Classes and FunctionsStorage Classes and Functions
Storage Classes and FunctionsJake Bond
Β 
File Handling and Command Line Arguments in C
File Handling and Command Line Arguments in CFile Handling and Command Line Arguments in C
File Handling and Command Line Arguments in CMahendra Yadav
Β 
user defined function
user defined functionuser defined function
user defined functionKing Kavin Patel
Β 
Command line arguments
Command line argumentsCommand line arguments
Command line argumentsAshok Raj
Β 
Amit user defined functions xi (2)
Amit  user defined functions xi (2)Amit  user defined functions xi (2)
Amit user defined functions xi (2)Arpit Meena
Β 
Recursive Function
Recursive FunctionRecursive Function
Recursive FunctionHarsh Pathak
Β 
11 lec 11 storage class
11 lec 11 storage class11 lec 11 storage class
11 lec 11 storage classkapil078
Β 
Storage classes in c language
Storage classes in c languageStorage classes in c language
Storage classes in c languagetanmaymodi4
Β 
Recursion in c
Recursion in cRecursion in c
Recursion in cSaket Pathak
Β 

What's hot (20)

Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programming
Β 
Storage Class Specifiers in C++
Storage Class Specifiers in C++Storage Class Specifiers in C++
Storage Class Specifiers in C++
Β 
Storage classes in C
Storage classes in C Storage classes in C
Storage classes in C
Β 
Header files in c
Header files in cHeader files in c
Header files in c
Β 
Storage class in C Language
Storage class in C LanguageStorage class in C Language
Storage class in C Language
Β 
Function & Recursion
Function & RecursionFunction & Recursion
Function & Recursion
Β 
Storage classes
Storage classesStorage classes
Storage classes
Β 
More on Lex
More on LexMore on Lex
More on Lex
Β 
Storage classes in c++
Storage classes in c++Storage classes in c++
Storage classes in c++
Β 
Storage Classes and Functions
Storage Classes and FunctionsStorage Classes and Functions
Storage Classes and Functions
Β 
File Handling and Command Line Arguments in C
File Handling and Command Line Arguments in CFile Handling and Command Line Arguments in C
File Handling and Command Line Arguments in C
Β 
user defined function
user defined functionuser defined function
user defined function
Β 
Command line arguments
Command line argumentsCommand line arguments
Command line arguments
Β 
C++ nots
C++ notsC++ nots
C++ nots
Β 
Functions in C
Functions in CFunctions in C
Functions in C
Β 
Amit user defined functions xi (2)
Amit  user defined functions xi (2)Amit  user defined functions xi (2)
Amit user defined functions xi (2)
Β 
Recursive Function
Recursive FunctionRecursive Function
Recursive Function
Β 
11 lec 11 storage class
11 lec 11 storage class11 lec 11 storage class
11 lec 11 storage class
Β 
Storage classes in c language
Storage classes in c languageStorage classes in c language
Storage classes in c language
Β 
Recursion in c
Recursion in cRecursion in c
Recursion in c
Β 

Viewers also liked

Viewers also liked (6)

Programming Methodology
Programming MethodologyProgramming Methodology
Programming Methodology
Β 
Structures
StructuresStructures
Structures
Β 
Wild Cards
Wild CardsWild Cards
Wild Cards
Β 
Cso Latest
Cso LatestCso Latest
Cso Latest
Β 
Computer Fundamentals
Computer FundamentalsComputer Fundamentals
Computer Fundamentals
Β 
Arrays
ArraysArrays
Arrays
Β 

Similar to RETURNING FROM FUNCTIONS

Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 FunctionDeepak Singh
Β 
What is storage class
What is storage classWhat is storage class
What is storage classIsha Aggarwal
Β 
Function in C++
Function in C++Function in C++
Function in C++Prof Ansari
Β 
Operator Overloading and Scope of Variable
Operator Overloading and Scope of VariableOperator Overloading and Scope of Variable
Operator Overloading and Scope of VariableMOHIT DADU
Β 
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxSangeetaBorde3
Β 
08 -functions
08  -functions08  -functions
08 -functionsHector Garzo
Β 
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM) FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)Mansi Tyagi
Β 
Latest C Interview Questions and Answers
Latest C Interview Questions and AnswersLatest C Interview Questions and Answers
Latest C Interview Questions and AnswersDaisyWatson5
Β 
Data structure scope of variables
Data structure scope of variablesData structure scope of variables
Data structure scope of variablesSaurav Kumar
Β 
C notes diploma-ee-3rd-sem
C notes diploma-ee-3rd-semC notes diploma-ee-3rd-sem
C notes diploma-ee-3rd-semKavita Dagar
Β 
Oop lect3.pptx
Oop lect3.pptxOop lect3.pptx
Oop lect3.pptxMrMudassir
Β 
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptxC-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptxSKUP1
Β 
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptxC-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptxLECO9
Β 
Chapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptChapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptAmanuelZewdie4
Β 
Basic construction of c
Basic construction of cBasic construction of c
Basic construction of ckinish kumar
Β 

Similar to RETURNING FROM FUNCTIONS (20)

Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 Function
Β 
What is storage class
What is storage classWhat is storage class
What is storage class
Β 
Function in C++
Function in C++Function in C++
Function in C++
Β 
Operator Overloading and Scope of Variable
Operator Overloading and Scope of VariableOperator Overloading and Scope of Variable
Operator Overloading and Scope of Variable
Β 
C functions list
C functions listC functions list
C functions list
Β 
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
Β 
08 -functions
08  -functions08  -functions
08 -functions
Β 
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM) FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
Β 
Lecture 13 - Storage Classes
Lecture 13 - Storage ClassesLecture 13 - Storage Classes
Lecture 13 - Storage Classes
Β 
Function
FunctionFunction
Function
Β 
Latest C Interview Questions and Answers
Latest C Interview Questions and AnswersLatest C Interview Questions and Answers
Latest C Interview Questions and Answers
Β 
Data structure scope of variables
Data structure scope of variablesData structure scope of variables
Data structure scope of variables
Β 
C notes diploma-ee-3rd-sem
C notes diploma-ee-3rd-semC notes diploma-ee-3rd-sem
C notes diploma-ee-3rd-sem
Β 
Oop lect3.pptx
Oop lect3.pptxOop lect3.pptx
Oop lect3.pptx
Β 
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptxC-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
Β 
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptxC-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
Β 
Chapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptChapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.ppt
Β 
User defined functions in matlab
User defined functions in  matlabUser defined functions in  matlab
User defined functions in matlab
Β 
Basic construction of c
Basic construction of cBasic construction of c
Basic construction of c
Β 
Unit iii
Unit iiiUnit iii
Unit iii
Β 

Recently uploaded

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
Β 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
Β 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
Β 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
Β 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
Β 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
Β 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
Β 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
Β 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
Β 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
Β 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
Β 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
Β 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
Β 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
Β 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
Β 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
Β 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
Β 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
Β 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
Β 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
Β 

Recently uploaded (20)

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Β 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
Β 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
Β 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
Β 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
Β 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
Β 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
Β 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
Β 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
Β 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Β 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
Β 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
Β 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Β 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
Β 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Β 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
Β 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
Β 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
Β 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
Β 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
Β 

RETURNING FROM FUNCTIONS

  • 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.
  • 3.
  • 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.
  • 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.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.