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
Use of logical vector: Determine how many elements in the vector vec were greater than 5, the sum function can be used on the resulting vector isg: >> sum(isg) ans =

Nested IF-ELSE statements: The if-else statement is used to select between the two statements. In order to select from more than two statements, the if-else statements can als

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

Built in recursive function in MATLAB: We have seen that the built-in function in MATLAB to compute factorials, termed as the factorial and we know how to implement the iterat

Give the formula for a rational function that has a hole at x=7 & vertical asmptote at x=-3/2

Write a MATLAB function [d1, u1, l1, c1, r1] = NaiveGaussArrow(d, u, l, c, r) that takes as input the 5 vectors de ned above representing A. This function performs Naive Gauss redu

Logical Vectors: The relational operators can also be used with the vectors and matrices. For illustration, let's say that there is a vector, and we want to compare each eleme

Structure Consisting of the Fields A MATLAB can also solve the sets of equations. In this illustration, the solutions for x, y, & z are returned as a structure consisting of th

User-defined functions: Therefore, although many functions compute and return values, some do not. A few functions rather merely accomplish a task. Regardless of what type of

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