×

Ruby Tutorial

Ruby Basics

Ruby Control Statements

Ruby Methods

Ruby Classes and Methods

Ruby Arrays

Ruby Sets

Ruby Strings

Ruby Classes & Objects

Ruby Hash

Ruby Tools

Ruby Functions

Ruby Built-in Functions

Misc.

Ruby Programs

Ruby program to create a simple recursive function

Last Updated : December 15, 2025

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 »


Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

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