# How to write recursive function in flow service

Hi, all
I was trying to find the factorial of a given number using recursion in flow but couldn’t figure it out.
Can anyone be so kind to tell me how to write recursive functions in developer, any help would be appreciated…

Hello,

Yemi Bedu

Thanks ybedu,
Have a nice day too.

I use a document for the recursive invoke and set the scope of the invoke step. Just be careful to clean up the scope document and you’ll be right.

e.g.
MAP (setup the document for invoke and map the inputs in)
INVOKE (same service, with scope set to the document)
MAP (get the result and cleanup the scope doc)

Just make sure your service is only returning what it should based on the input/output signature (but that’s standard practice)

regards,
Nath

What is scope??? Can you explain how to use this property with some examples Tq :roll:

Hi

Below is my flow service for recursion in developer. Kindly correct the mistakes,

``````              1. Map number and temp
2. Branch on number
2.1. Sequence if number>1
2.1.1 map subtract number by 1
2.1.2. call the same flow service
2.1.3. multiply temp with number
2.2. sequence for default
2.2.1. Exit from the flow
``````

When I run i ask input for each call and it doesn’t perform multiply operation at the end. Correct my error

Thank You

Share the algorithm of your flow service.

Back to college - try factorial of a number using recursion function.

Hi,

1. Map number to temp
2. map subtract number by 1
3. Repeat (count = number)
3.1. Sequence if number>1
3.1.1. multiply temp with number
3.1.2. map subtract number by 1
3.2. Sequence for default (number=1)
3.2.1. do nothing

The above code does not look like a recursion flow service call.

Have a look at the below screen shots: Just have a close look at the step level comments. Let me know if you have any questions

The above pseudo code will not work as expected. Have a look at the screen shot above.

Yes Thank you M@he\$h :lol: . It’s really helpful.

I know that my suggestion did not contain a recursive call.

I wanted to show the iterative alternative as recursion is not really a good idea in webMethods.

Depending on the depth of the recursion and the complexity of structures used as input recursion can lead to severe performance issues (i.e. OutOfThreads, OutOfMemory and/or StackOverflow).

Regards,
Holger

1 Like

Ok I see a valid point. Thanks for the detailed notes.

Does the loop has limit in webemthod flow language :?:

What do you mean? Can you be more clear?

I have list of document, I want to loop over the list to process. Is there any limit for that list in flow language?

Hello Preethi,

There is no limit for loop over the list of document.

The loop flow step is depending on input array property.

Thanks,
Prem

But please keep in mind as many as loops (documentList or recurssive) it could degrade the performance unless your logic for loops handling is done properly (dropping temp doclists and vars etc…)

HTH,
RMG

You can use loop to iterate over a document list, string list. While looping over large documents make sure to take care of performance. You may also right a java code.