Why Recursive Function returns “Undefined”?
Last Updated :
17 Apr, 2023
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;
}
return fact(n - 1, n * res);
}
int main() {
cout << fact(5, 1) << endl;
return 0;
}
|
Java
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 ));
}
}
|
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));
}
}
|
Javascript
function fact(n, res = 1) {
if (n === 1) {
return res;
}
fact(n - 1, n * res);
}
console.log(fact(5));
|
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++
#include <iostream>
using namespace std;
int fact( int n, int res)
{
if (n == 1) {
return res;
}
return fact(n - 1, n * res);
}
int main()
{
int res = 1;
cout<<(fact(5, res))<<endl;
}
|
Java
import java.io.*;
class GFG {
static int fact( int n, int res)
{
if (n == 1 ) {
return res;
}
return fact(n - 1 , n * res);
}
public static void main(String[] args)
{
int res = 1 ;
System.out.print(fact( 5 , res));
}
}
|
Python3
def fact(n, res = 1 ):
if n = = 1 :
return res
return fact(n - 1 , n * res)
print (fact( 5 ))
|
C#
using System;
public class GFG {
public static int fact( int n, int res)
{
if (n == 1) {
return res;
}
return fact(n - 1, n * res);
}
static public void Main()
{
int res = 1;
Console.WriteLine(fact(5, res));
}
}
|
Javascript
function fact(n, res = 1) {
if (n === 1) {
return res;
}
return fact(n - 1, n * res);
}
console.log(fact(5));
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Related Articles:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...