Return function in a recursive function code

When I create a recursive code and have the desired output, after following it with 'return' the next line - it continues running and outputting unwanted results - can someone help me avoid this please.

17 comentarios

Ameer Hamza
Ameer Hamza el 25 de Abr. de 2018
Can you show us your friends function code, along with data you are using to call that function?
Rdmn Raja
Rdmn Raja el 25 de Abr. de 2018
Editada: Rdmn Raja el 25 de Abr. de 2018
[C]=Function(A,P,K)
%A is adjacency matrix of a Graph say
%P, K are vectors
if (terminating condition)
   C=P;
   return
else 
    ......
    P=.....%redefined
    K=.....%redefined
    Function(A,P,K)
end
end

The code is of this type.

Ameer Hamza
Ameer Hamza el 25 de Abr. de 2018

I think in the first condition you meant K=P. The only reason it can be happening is C never becomes equal to P.

Rdmn Raja
Rdmn Raja el 25 de Abr. de 2018
No, the termination condition could be that all(P)==1 for instance and this is reached, I've debugged it and seen every step. I can use disp(P) but this doesn't output the result I want as a matrix.
Walter Roberson
Walter Roberson el 25 de Abr. de 2018
Editada: Walter Roberson el 25 de Abr. de 2018

The line

    Function(A,P,K)

does not change C, so there is no point running the call.

Rdmn Raja
Rdmn Raja el 25 de Abr. de 2018
true, C is only determined right at the end, can I display the output as a matrix using disp(..)

If you want to return P from the last termination call to recursive function than you will need to assign it to the output of Function in else condition like this

[C]=Function(A,P,K)
%A is adjacency matrix of a Graph say
%P, K are vectors
if (terminating condition)
   C=P;
   return
else 
    ......
    P=.....%redefined
    K=.....%redefined
    C = Function(A,P,K);
end
end

No point of using disp if you want the output in a matrix.

Rdmn Raja
Rdmn Raja el 25 de Abr. de 2018
i see my mistake, thanks
I'm actually facing a similar problem today again
[]=function(A,b,c,i,j,f,G)
if termination condition
disp(G) %as G is a matrix containing matrices, I'm sending it to my workspace as a variable at the moment
return
else
if some condition
G{i,j}= some matrix
else
....
end
end
if true
% code
end
The issue is this function runs even after I've sent that big G I'm building up I presume because of the recursive nature. I can't really use the idea above as I'm using my function to find an entry via several steps. Any help will be appreciated as after debugging I see the out of memory error is coming after when I would like the algo to stop - despite the use of return.
Ameer Hamza
Ameer Hamza el 26 de Abr. de 2018
There is no recursion in above code. It is hard to spot an error if we cannot see where the recursion is happening.
function[]=HELP(A,R,P,X,i,j,n,D,C)
%A is a fixed matrix, R is something I'm building up then assigning it as an try of C, P, X are matrices for the algo, i and j are indices for the entry in C, n and D are used for the algo
if i>length(A)
assignin('base','x',C); *<<<I just want C as output*
return
else
if isempty(P)==1
if isempty(X)==1
C{i,j}=R;
%R,P,X are changed
if isempty(P)==1
HELP(A,[],1:length(A),[],i+1,1,0,[],C);
else
HELP(A,R,P,X,i,j+1,n-1,D,C);
end
end
else
if n==0
some algo stuff
.
.
.
HELP(A,R,P,X,i,j,n+1,D,C);
else
algo stuff
.
.
.
.
.
.
HELP(A,R,P,X,i,j,n+1,D,C);
end
end
end
end
This is the framework of the code, I have debugged and I see it works, just some bad coding in the process.
Ameer Hamza
Ameer Hamza el 26 de Abr. de 2018
Are you sure that termination condition is met? Apparently, the above code is logically correct and it should fall out of recursion once termination condition is met.
Rdmn Raja
Rdmn Raja el 26 de Abr. de 2018
yes logically it's good, and yes the output falls out....but when I debugged it and ran it step by step it carries out further steps after it creates the variable 'x' in my workspace...
What do you mean by "further steps". The only statement after that is return. What exactly it is doing after
assignin('base','x',C);
Rdmn Raja
Rdmn Raja el 26 de Abr. de 2018
haha exactly, after return it goes to HELP(....) below and starts editing my matrices, it's not an issue as I have my output but I would love to know what I'm doing wrong from a programming point of view ( if any).
From the code you gave, all calls to HELP function are followed by end. There are no statements after that. How can the function start editing matrices? Or are there any other statements which you haven't included in the given code?
One possible solution can be adding a return after every call to HELP. For example
if isempty(P)==1
HELP(A,[],1:length(A),[],i+1,1,0,[],C);
return;
else
HELP(A,R,P,X,i,j+1,n-1,D,C);
return;
end
Similar for other two calls to HELP.
Rdmn Raja
Rdmn Raja el 27 de Abr. de 2018
thanks, added it in but didn't check in the debugger mode if it runs after as I get the desired output I need.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Operators and Elementary Operations en Centro de ayuda y File Exchange.

Preguntada:

el 25 de Abr. de 2018

Comentada:

el 27 de Abr. de 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by