GeeksforGeeks App
Open App
Browser
Continue

# Why Recursive Function returns “Undefined”?

### What is Recursion?

In simple words, recursion is when a function is calling itself directly or indirectly.

It is used to break down the problem into subproblems which makes us easy to solve. It also reduces the length of the code. It has certain advantages over just iterating through that is discussed here.

The function of recursion looks simply like this:

recursion_function (parameters){
//  Code segments
recursion_function(parameters);
}

### Example of a recursive function returning “Undefined”:

Sometimes the recursive function returns undefined. Let’s understand it with an example of calculating the factorial of a number n.

Now if we call this recursive method using a value for num, say 5, it should give us 120. But instead, it returns “undefined” [“None” for Python, and error in Java].

## C++

 `#include ` `using` `namespace` `std;` `int` `fact(``int` `n, ``int` `res) {``  ``if` `(n == 1) {``    ``res;``  ``}``  ``// Calling function recursively``  ``return` `fact(n - 1, n * res);``}` `// Driver code``int` `main() {``  ``cout << fact(5, 1) << endl;``  ``return` `0;``}`

## Java

 `/*package whatever //do not write package name here */` `import` `java.io.*;` `class` `GFG {` `  ``public` `static` `int` `fact(``int` `n, ``int` `res)``  ``{``    ``if` `(n == ``1``)``      ``return` `res;``    ``fact(n - ``1``, n * res);``  ``}` `  ``public` `static` `void` `main(String[] args)``  ``{``    ``System.out.println(fact(``5``,``1``));``  ``}``}` `// This code is contributed by akashish__`

## Python3

 `def` `fact(n, res ``=` `1``):``    ``if` `n ``=``=` `1``:``        ``return` `res``    ``fact(n ``-` `1``, n ``*` `res);` `print``(fact(``5``));`

## C#

 `using` `System;` `public` `class` `GFG{``  ` `  ``public` `static` `int` `fact(``int` `n, ``int` `res = 1){``    ``if``( n == 1)``        ``return` `res;``    ``fact(n - 1, n * res);``  ``}` `    ``static` `public` `void` `Main (){` `        ``Console.WriteLine(fact(5));``    ``}``}``// This code is contributed by akashish__`

## Javascript

 `function` `fact(n, res = 1) {`` ``if` `(n === 1) {``   ``return` `res;`` ``}`` ``// Calling function`` ``fact(n - 1, n * res);``}` `// Driver code``console.log(fact(5));`

Output

`undefined`

### Why the call returns Undefined?

Let us come to the basics of recursion and recall what we need in every recursive method:

1. Base Case
2. Recursive function call
3. Return statement

Now let us compare our above code with the above three points. The code has:

Base case:

if (n == 1)
return res;

Recursive function call

fact( n – 1, n * res);

But the code doesn’t have a global return statement. As a result, the value is getting calculated every time but it is returning nothing, which leads to the output “undefined“.

### Solution:

Insert a global return statement in the code and it should work fine. This ensures from any path, we always return something.

## C++

 `// C++ Code``#include ``using` `namespace` `std;` `int` `fact(``int` `n, ``int` `res)``{``  ``if` `(n == 1) {``    ``return` `res;``  ``}` `  ``// Return whatever value we get after calling this``  ``// function``  ``return` `fact(n - 1, n * res);``}` `int` `main()``{``  ``int` `res = 1;``  ``cout<<(fact(5, res))<

## Java

 `// Java Code``import` `java.io.*;``class` `GFG {` `  ``static` `int` `fact(``int` `n, ``int` `res)``  ``{``    ``if` `(n == ``1``) {``      ``return` `res;``    ``}``    ` `    ``// Return whatever value we get after calling this``    ``// function``    ``return` `fact(n - ``1``, n * res);``  ``}` `  ``public` `static` `void` `main(String[] args)``  ``{``    ``int` `res = ``1``;``    ``System.out.print(fact(``5``, res));``  ``}``}` `// This code is contributed by lokesh.`

## Python3

 `def` `fact(n, res ``=` `1``):``    ``if` `n ``=``=` `1``:``        ``return` `res``    ``# Return whatever value we get after``    ``# calling this function``    ``return` `fact(n ``-` `1``, n ``*` `res)`  `# Driver code``print``(fact(``5``))`

## C#

 `using` `System;` `public` `class` `GFG {` `  ``public` `static` `int` `fact(``int` `n, ``int` `res)``  ``{``    ``if` `(n == 1) {``      ``return` `res;``    ``}` `    ``// Return whatever value we get after calling this``    ``// function``    ``return` `fact(n - 1, n * res);``  ``}` `  ``static` `public` `void` `Main()``  ``{` `    ``int` `res = 1;``    ``Console.WriteLine(fact(5, res));``  ``}``}``// This code is contributed by akashish__`

## Javascript

 `function` `fact(n, res = 1) {``  ``if` `(n === 1) {``    ``return` `res;``  ``}``  ``// Return whatever value we get after``  ``// calling this function``  ``return` `fact(n - 1, n * res);``}` `// Driver code``console.log(fact(5));`

Output

`120`

Time Complexity: O(n)
Auxiliary Space: O(1)

Related Articles:

My Personal Notes arrow_drop_up