Global and Local Function Declaration – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Code Learning #CPP#2689 siteicon   siteicon   siteicon  

Global and Local Function Declaration

Understanding global and local function declaration using a function declaration above main and another inside the main routine.

Learning Objectives

  • Understanding how a global function declaration can be used in many functions while local declaration limits a function use within a given scope only.

Source Code

TC++ #2689

 

Source Code

Run Output

25
150

Code Understanding

int action1(int,int);
This function is declared outside main() routine so it can be used in any function and main().

int main() {
int action2(int,int);
This function is declared inside the main routine, so its usage is limited to this routine only.

int a=10,b=15; cout<<action2(a,b)<<endl;
Initial value of a and b are set and the function declared locally is applied. Although this function is declared locally but its code has to be written outside only.

cout<<action1(a,b)<<endl;
The function action1 is declared globally so it can be used in main() as well as in any other routine.

int action1(int x,int y){
//int n=action2(x,y);
This is not allowed and if uncommented will give error as this function action2() was meant to be used inside main only.
return x*y; //Returns multiplication of given parameters x and y.
}

int action2(int x,int y) {return x+y;}
This is function definition of a function declared inside the main routine. Its definition can not be given inside main but its call can only be made by main routine only.

 

 

Notes

  • concept of internal declaration within main or any other function itself provides restricted access to functions. This concept is well handled when the classes and oops concepts of c++ are used.


Suggested Filename(s): fn-global-local.cpp,fngloloc.cpp



Share

sunmitra| Created: 17-Jan-2018 | Updated: 17-Jan-2018|






×
Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues


Back