Ruby program to create a simple recursive function

Ruby Example: Write a program to create a simple recursive function.
Submitted by Nidhi, on December 24, 2021

Problem Solution:

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

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.

Ruby User-defined Functions Programs »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.