Javatpoint Logo
Javatpoint Logo

Difference between Declaration of User Defined Function Inside main() and Outside of main()

The Declaration of functions inside and outside the main is the same as global and local variables. When we declare any function outside the main function, then it is globally defined, and it is in the global scope. When we define any function inside the main function, then it is in the local scope of the main function. So, any function outside of the main function cannot use the functions which are inside the main function.

The function which is defined outside of the main function can be accessed and used by any function, irrespective it is inside or outside of the main function.

Example 1:

When we declare any function outside the main() function.

C++ code:

Output:

The difference is 2

Explanation:

In the above code, we have two functions that are defined outside the main() function. One function is used to get the subtraction, and the other is a helper function. The helper function is called the diff function. Since both functions are in the global scope, there is no error. We gave two integer values in the function as 5 and 3, and we got the output as 2, which is equal to 5-3.

Example 2:

When we declare the function inside the main() function.

C++ code:

Output:

/tmp/stIpvIA8cL.cpp: In function 'void fxn(int, int)':
/tmp/stIpvIA8cL.cpp:14:9: error: 'diff' was not declared in this scope
   14 |         diff(a, b);
      |         ^~~~

Explanation:

In the above code, we declared the diff function inside the main and called it by the helper function, which is declared outside the main. Since the scope of the diff function is local to the main(), and it cannot be accessed outside the main() function, we got the error.

Note: When we have two functions with the same name, but one is declared outside the main, and one is declared inside the main with different function signatures. If we call the global function, then we can get the error due to ignorance of the local function.

Example 3:

C++ code:

Output:

The difference is 2

Explanation:

In the above code, we have the local declaration of the function, and we called it inside the main, so we got the result without any error. We gave two integer values in the function as 5 and 3, and we got the output as 2, which is equal to 5-3.

Example 4:

C++ code:

Output:

g++ /tmp/stIpvIA8cL.cpp
/tmp/stIpvIA8cL.cpp: In function 'int main()':
/tmp/stIpvIA8cL.cpp:9:19: error: too few arguments to function 'int diff(int, int)'
    9 |         cout<

Explanation:

In the above code, we have a diff() function of two types. One diff() function has arguments, and it returns the difference between two numbers. Another one is returning an integer without any calculation. So the diff() function with arguments is declared inside the main() function, and it is local to the main(). Another diff() function is declared outside the main() function. In the main() function, we are calling the diff() function without arguments since it is globally defined, so we can access it inside the main() function. We are expecting the answer as an integer value, but it gives the error because we have the same function name, which is declared locally, and we are calling it a global function.

So due to the ignorance of local functions, we got the error.


Next TopicIsprint() in C++





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA