How to go to main stack?
Last Updated :
16 Nov, 2022
What is the main() function?
The function named “main” is a special function. It is the function called when a code is run. The execution of all programs begin with the main() function irrespective of where the function is actually located within the code.
How to go to the main() stack from a function?
At any stack or recursion of our program, we can simply call the main() function to go to the main stack easily. It can be demonstrated by a small program
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void fun();
int main()
{
cout << "Inside main" << endl;
fun();
return 0;
}
void fun()
{
static int key = 2;
if (key == 0)
return ;
key--;
cout << "Inside fun" << endl;
main();
}
|
Java
import java.io.*;
class GFG {
static int key = 2 ;
static void fun()
{
if (key == 0 )
return ;
System.out.println( "Inside fun" );
key--;
GFG.main( null );
}
public static void main(String[] args)
{
System.out.println( "Inside main" );
fun();
return ;
}
}
|
C#
using System;
public class GFG {
static int key = 2;
static void fun()
{
if (key == 0)
return ;
Console.WriteLine( "Inside fun" );
key--;
GFG.Main();
}
static public void Main()
{
Console.WriteLine( "Inside main" );
fun();
}
}
|
Output
Inside main
Inside fun
Inside main
Inside fun
Inside main
Whenever we are calling the main() function again it again starts executing the program
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...