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')

do

this

does

what

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
Give the formula for a rational function that has a hole at x=7 & vertical asmptote at x=-3/2

The variance is generally defined in terms of the arithmetic mean as: At times, though, the denominator is defined as n instead of n - 1. The default definition used by t

Using Functions with the Vectors and Matrices: As MATLAB is written to work with the vectors and matrices, the whole vector or matrix can be passed as an argument to the funct

Script a MATLAB program which meets the following speci cations: The program expects an input of a two-variable real-valued continuous function f : R^2--> R The program is to cal

Illustration of Minimum and Maximum Value Both of these functions also return the index of the minimum or maximum value; when there is more than one occurrence, it returns the

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

Illustration of Variance For illustration, for the vector [4, 6, 1, 5], there are n = 4 values therefore n - 1 is 3. The mean of this data set is also 4. The variance will be

Example of Minimum and Maximum Value For matrices, the functions min and max operate column wise by the default: >> mat = randint(2,4,[1 20]) mat =    9   10   1

Illustration of nested function: The output arguments are distinct from variables. The scope of an output argument is merely the nested function; and it cannot be used in the

Matrix of Plots: The other function which is very useful with any type of plot is subplot that creates a matrix of plots in the present Figure Window. The three arguments are