How to solve "Not Enough Input Arguments" error in a recursive function?

2 views (last 30 days)
The goal of my program is to create Sierpinski Carpet. When i run the code i get the "Not Enough Input Arguments" error.
function out= MyFunction(x, y ,width, depth, max_depth)
clc
if depth > max_depth
rectangle('Position',[x, y, width, width]);
else
width=width/3;
MyFunction(x, y, width, depth+1);
MyFunction(x+width, y, width, depth+1);
MyFunction(x+width+width, y, width, depth+1);
MyFunction(x, y+width, width, depth+1);
%MyFunction(x, y+width, width, depth+1); %middle empty
MyFunction(x+width+width, y+width, width, depth+1);
MyFunction(x, y+width+width, width, depth+1);
MyFunction(x+width, y+width+width, width, depth+1);
MyFunction(x+width+width, y+width+width, width, depth+1);
end
end
When i call the function in console i get the error for example:
> MyFunction(0,0,9,0,3)

Accepted Answer

harsha001
harsha001 on 20 Mar 2019
Edited: madhan ravi on 21 Mar 2019
You defined the function to have 5 arguments (in the first line) yet in your recursive calls, you are only passing 4 arguments.
You are missing a 'max_depth' argument in all your recursive calls to MyFunction in the 'else part':
function out= MyFunction(x, y ,width, depth, max_depth)
clc
if depth > max_depth
rectangle('Position',[x, y, width, width]);
else
width=width/3;
MyFunction(x, y, width, depth+1, max_depth);
MyFunction(x+width, y, width, depth+1, max_depth);
MyFunction(x+width+width, y, width, depth+1, max_depth);
MyFunction(x, y+width, width, depth+1, max_depth);
%MyFunction(x, y+width, width, depth+1); %middle empty
MyFunction(x+width+width, y+width, width, depth+1, max_depth);
MyFunction(x, y+width+width, width, depth+1, max_depth);
MyFunction(x+width, y+width+width, width, depth+1, max_depth);
MyFunction(x+width+width, y+width+width, width, depth+1, max_depth);
end
end

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!