Q:

Ruby program to create a simple recursive function

belongs to collection: Ruby User-defined Functions Programs

0

In this program, we will create a simple recursive function print numbers from 5 to 1.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to create a simple recursive function is given below. The given program is compiled and executed successfully.

# Ruby program to create a 
# simple recursive function

def RecursiveFun(num)
	if num == 0
		return num;
	else 
		print num," ";
	end
	return RecursiveFun(num-1);
end

RecursiveFun(5);

Output:

5 4 3 2 1

Explanation:

In the above program, we created a recursive function RecursiveFun(). Here we called RecursiveFun() with argument value 5 and printed the numbers from 5 to 1.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Ruby User-defined Functions Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Ruby program to calculate the factorial of given n... >>
<< Ruby program to return multiple values from the us...