Blog|Meta Topics|Advanced Topics

Software Technology: Recursion - function call

Assumed Knowledge:
Learning Outcomes:
  • Better understand the concept of parameter passing.
  • Familiarize yourself with standard terminology - formal paramters vs. actual parameters.
  • Understand the control flow and memory transactions during a function call.
  • Understand the concept of Call Stack.

Formal parameters vs. actual parameters

  • Formal parameter is the name used for the variable in the function definition.
  • Actual parameter is the value copied into the formal parameter during a function call.

Consider the following example:

1
2
3
4
5
6
7
8
9
10
11
12
13
public class FormalVsActual {
	public static int square(int val) {
		int result = val * val;
		return result;
	}
	
	public static void main(String[] args) {
		int c = 10;
		int d = square(c);
		int e = square(6);
		int f = square(d/20 + e/9);
	}
}

In the above example,

  • Formal parameter in function square is val.
  • Actual parameter in the function call square(c) is c (10).
  • Actual parameter in the function call square(6) is 6.
  • Actual parameter in the function call square(d/20 + e/9) is d/4 + e/9 (100/20 + 36/9 = 9).

What happens during a method call?

Before we can truly conquer recursion, it’s critical to understand what happens when a method is called. Consider the following example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public static void main(String[] args) {
	int ax = 1, ay = 3;
	int bx = 6, by = 5;
	double d = distance(ax, ay, bx, by);
	System.out.println("Distance: "+d);
}

public static double distance(int x1, int y1, int x2, int y2) {
	int s1 = square(x2-x1);
	int s2 = square(y2-y1);
	int sumSquares = s1 + s2;
	double result = Math.sqrt(sumSquares);
	return result;
}

public static int square(int num) {
	int answer = num * num;
	return answer;
}

STEP 1: main method is invoked by JVM

Method call is placed on the stack. Note that parameter is null because we typically do not pass any arguments to main, at least in this unit.

STEP 2: main method calls distance with parameters 1, 3, 6 and 5.

Another entry is made for the call to distance and placed on the call stack.

STEP 3: distance calls square with parameter 5

A third entry is made for the call to square and placed on the stack.

STEP 4: square returns 25 to distance

Entry for square is taken off the stack. distance becomes the active method.

STEP 5: distance calls square with parameter 2

A third entry is made for the call to square and placed on the stack.

STEP 6: square returns 4 to distance

Entry for square is taken off the stack. distance becomes the active method.

STEP 7: distance calls Math.sqrt with parameter 29

A third entry is made for the call to Math.sqrt and placed on the stack.

STEP 8: Math.sqrt returns 5.38516 to distance

Entry for square is taken off the stack. distance becomes the active method.

STEP 9: distance returns 5.38516 to main

Entry for distance is taken off the stack. main becomes the active method.

STEP 10: main terminates

Entry for main is taken off the stack. Call stack is now empty. Program has now finished execution.