0
function return

I created a function that returns [A] with a for loop inside that modifies A, an array, there is no explicit return statement. It works. I added in a 2nd nested for loop under this code and did some simple disp to show the indexes. This code ran alone just fine. But now, with the extra code added into the original function, the added code never gets run. If I put the same code before the original for loop that makes A, the code runs for both for loops. So I am guessing that once a function's return is created, the function just returns from that point in the code. That is not what I would have expected in any other language like C or C++. Why did this act so strange the first time?


Scilab 25-06-18, 11:24 p.m. maxcy

Answers:

0

Please copy-paste your scilab code here.

26-06-18, 11:32 a.m. rupakrokade

function [A]=inc_array()
// added in nested for loop experiment
// if this code is cut and pasted below line 29, it will not execute
// the function returns at line 28!
// and will return even if line 29 is not there!
   n=3;
    for k=1:n-1    //1st row index
        disp(k,"k=");
        for i=k+1:n    //2nd row index
            disp(i,"i=");
            for j=k:n  // scan the row
                disp(j,"j=");
            end
        end 
    end    
    
    format("e",8);
    A=[1,3,5,7,9];
    val = 1.5;
    n=size(A,2);   //get the # cols, use 1 for rows
    for (i=1:n)
        //pause
        // to see the right side of the following  statement
        A(i)=A(i) + val*A(i);
        //the debugger can olny see the left side values of a variable
        disp(A(i), "A(i)=")
        pause
    end
    A=A'
endfunction


26-06-18, 8:35 p.m. maxcy

Login to add comment


0

Please copy-paste your scilab code here.

26-06-18, 11:32 a.m. rupakrokade


0

function [A]=inc_array()
// added in nested for loop experiment
// if this code is cut and pasted below line 29, it will not execute
// the function returns at line 28!
// and will return even if line 29 is not there!
   n=3;
    for k=1:n-1    //1st row index
        disp(k,"k=");
        for i=k+1:n    //2nd row index
            disp(i,"i=");
            for j=k:n  // scan the row
                disp(j,"j=");
            end
        end 
    end    
    
    format("e",8);
    A=[1,3,5,7,9];
    val = 1.5;
    n=size(A,2);   //get the # cols, use 1 for rows
    for (i=1:n)
        //pause
        // to see the right side of the following  statement
        A(i)=A(i) + val*A(i);
        //the debugger can olny see the left side values of a variable
        disp(A(i), "A(i)=")
        pause
    end
    A=A'
endfunction

26-06-18, 8:36 p.m. maxcy

Cut and paste the first 3 nested for loops to the end of the code, after the A=A' line and the for loop does not show up any more in the output! The code as it stands does work!


28-06-18, 7:25 p.m. maxcy

Login to add comment


0

Your question is difficult to understand looking at the code. Your code just runs fine. Please share a fail case. Mention what is the expected output.

28-06-18, 7:18 p.m. rupakrokade


0

I know the code runs fine. But did you cut and paste the code for the nested for loops, and put the code after the A=A' line? When you do that the for loops never show their index values anymore!

30-06-18, 7 a.m. maxcy


Log-in to answer to this question.