ADVERTISEMENT
ADVERTISEMENT

Does Go language supports recursion?

Learn about the recursion, does Go language supports recursion?
Submitted by IncludeHelp, on October 05, 2021

Let's understand, what is recursion?
In computer science, a recursion function is a function that calls itself during its execution. Recursion functions allow programmers to write efficient programs using a minimal amount of code.

Now coming to the question, does Go language supports recursion?

The answer is – Yes, Go supports recursive functions (recursion).

Consider the below example,

package main

import (
	"fmt"
)

// Recursion function
func fact(n int) int {
	if n == 0 {
		return 1
	}
	return n * fact(n-1)
}

func main() {
	x := 5
	factorial := fact(x)
	fmt.Println("Factorial of", x, "is", factorial)

	x = 15
	factorial = fact(x)
	fmt.Println("Factorial of", x, "is", factorial)
}

Output:

Factorial of 5 is 120
Factorial of 15 is 1307674368000

Golang FAQ »

ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.