Illustration of a recursive function, MATLAB in Mathematics

Illustration of a recursive function:

illustration is of a recursive function which does not return anything, but simply prints. The below function prtwords receives a sentence, and prints words in the sentence in the reverse order. The algorithm for prtwords function is as shown below:

  •  Receive a sentence as input argument.
  •  Use strtok to split the sentence into the first word and the rest of the sentence.
  •  If the rest of the sentence is not blank (in another words, if there is more to it), recursively call the prtwords function and pass to it the rest of the sentence.
  •  Print the word.

The function definition is as shown:

1179_Illustration of a recursive function.png

An example of calling the function passes the sentence "what does this do":

>> prtwords('what does this do')





What occurs when the function is called here is outlined as shown.

1054_Illustration of a recursive function1.png

In this illustration, the base case is when the rest of the string is empty; in another words, the end of the original sentence has been reached. Each time the function is called, then the execution of the function is interrupted by a recursive call to the function, till the base case is reached. Whenever the base case is reached, the whole function can be executed, involving the printing of the word (in the base case, the word 'do'). The execution of the function is once completed, the program returns to the earlier version of the function in which the word was 'this', and finishes the execution by printing the word 'this'. This continues; the versions of the function are completed in the reverse order, therefore the program ends up printing the words from the sentence in the reverse order.

Posted Date: 10/22/2012 1:35:54 AM | Location : United States

Related Discussions:- Illustration of a recursive function, Assignment Help, Ask Question on Illustration of a recursive function, Get Answer, Expert's Help, Illustration of a recursive function Discussions

Write discussion on Illustration of a recursive function
Your posts are moderated
Related Questions
Print an imaginary number: To print an imaginary number, the function disp will show both parts automatically: >> disp(z1)    4.0000 + 2.0000i The function fprint

Illustration of Advanced file input and output: For illustration, to refer to the third number in the first element of the cell array: >> subjdata{1}(3) ans =

Refer the subset of a matrix: It is also possible to refer to the subset of a matrix. For illustration, this refers to the first & second rows, second & third columns: >> m

bar and barh functions: For a matrix, the bar and barh functions will group altogether the values in each and every row. The illustration is as shown: rm = randint(2,4,[1

Empty Vectors: An empty vector or in another words, a vector which stores no values, can be generated using the empty square brackets: >> evec = [] evec = [] >> lengt

Labels and Prompts: The script loads all the numbers from file into a row vector. It then splits the vector; it stores the initial element that is the experiment number in a v

Strings as Vectors: The Strings are considered as vectors of characters-or in another words, a vector in which each and every element is a single character-so numerous vector

Function isreal - complex numbers: The function isreal returns 1 for logical true when there is no imaginary part of the argument, or 0 for false when the argument does have a

Illustration of Empty vectors: The Empty vectors can also be used to delete elements from the arrays. For illustration, to remove the third element from array, an empty vector

Illustration of Spreadsheet Files: This reads the numbers in a double vector variable nums and the text in a cell array txt (the xlsread function forever returns the numbers f