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 <iostream> 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)); |
undefined
Why the call returns Undefined?
Let us come to the basics of recursion and recall what we need in every recursive method:
- Base Case
- Recursive function call
- 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 <iostream> 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))<<endl; } // This code is contributed by Aman Kumar. |
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)); |
120
Time Complexity: O(n)
Auxiliary Space: O(1)
Related Articles:
Please Login to comment...