Q:

Write a function per2 that receives one number as an input argument. The function has a persistent variable that sums the values passed to it. Here are the first two times the function is called:

0

 Write a function per2 that receives one number as an input 

argument. The function has a persistent variable that sums the 

values passed to it. Here are the first two times the function is called:

>> per2(4)

ans =

 4

>> per2(6)

ans =

 10

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

per2.m

function outstat = per2(num)

% Persistent variable sums the numbers

% passed to this function

% Format of call: per2(input number)

% Returns the persistent sum of input arguments

persistent mysum

if isempty(mysum)

 mysum = 0;

end

mysum = mysum + num;

outstat = mysum;

end

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now