Illustration of a recursive function, MATLAB in Mathematics

Assignment Help:

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.


Related Discussions:- Illustration of a recursive function

Algebra, 5 p2+8p+15, 3 p2-3p-18, 12 p...

5 p2+8p+15, 3 p2-3p-18, 12 p2-p-30

Counting in a while loop, Counting in a While Loop: Whenever it is not...

Counting in a While Loop: Whenever it is not known ahead of the time how many values will be entered into the script, it is often essential to count the number of values which

Using functions with the vectors and matrices, Using Functions with the Vec...

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

Changing dimensions, Changing Dimensions: In addition to transpose ope...

Changing Dimensions: In addition to transpose operator, the MATLAB has some built-in functions which change the dimensions or configuration of matrices, involving fliplr, resh

Nested if-else statements, Nested IF-ELSE statements: The if-else stat...

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

Strcat function - concatenation, Strcat function - Concatenation : The ...

Strcat function - Concatenation : The strcat function, though, will eliminate the trailing blanks from strings before concatenating. Note that in these illustrations, the trail

Illustration of empty vectors , Illustration of Empty vectors: The Emp...

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

User-defined functions - matlab, User-defined functions: Therefore, al...

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

Use of string variables, Use of string variables: The Characters invol...

Use of string variables: The Characters involve letters of the alphabet, punctuation marks, digits, white space, and control characters. The Control characters are characters

Recursive functions, Recursive Functions: The Recursion occurs wheneve...

Recursive Functions: The Recursion occurs whenever something is defined in terms of itself. In the programming, a recursive function is a function which calls itself. The Recu

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd