When a call is made to a new function all the variables local to the calling routine need to be saved, otherwise the new function will overwrite the calling routine variables. Similarly the current location address in the routine must be saved so that the new function knows where to go after it is completed.

Recursive Function to Find Factorial
int fact(int n)
{
int S; if(n==1)
return(1); else
S = n * fact( n โ 1 );
return(S)
}
Balancing the Symbols
- Compilers check the programs for errors, a lack of one symbol will cause an error.
- A Program that checks whether everything is balanced.
- Every right parenthesis should have its left parenthesis.
- Check for balancing the parenthesis brackets braces and ignore any other character.
Algorithm for balancing the symbols
Read one character at a time until it encounters the delimiter `#’.
Step 1 : – If the character is an opening symbol, push it onto the stack.
Step 2 : – If the character is a closing symbol, and if the stack is empty report an error as missing opening symbol.
Step 3 : – If it is a closing symbol and if it has corresponding opening symbol in the stack, POP it from the stack. Otherwise, report an error as mismatched symbols.
Step 4 : – At the end of file, if the stack is not empty, report an error as Missing closing symbol. Otherwise, report as balanced symbols.
E.g. Let us consider the expression ((B*B)-{4*A*C}/[2*A]) #


Empty stack, hence the symbols the balanced in the given expression.
Example for unbalanced symbols:

Other Courses