How to resolve the algorithm Nested function step by step in the Fortran programming language
How to resolve the algorithm Nested function step by step in the Fortran programming language
Table of Contents
Problem Statement
In many languages, functions can be nested, resulting in outer functions and inner functions. The inner function can access variables from the outer function. In most languages, the inner function can also modify variables in the outer function.
Write a program consisting of two nested functions that prints the following text. The outer function (called MakeList or equivalent) is responsible for creating the list as a whole and is given the separator ". " as argument. It also defines a counter variable to keep track of the item number. This demonstrates how the inner function can influence the variables in the outer function. The inner function (called MakeItem or equivalent) is responsible for creating a list item. It accesses the separator from the outer function and modifies the counter.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Nested function step by step in the Fortran programming language
Source code in the fortran programming language
FUNCTION F(X)
REAL X
DIST(U,V,W) = X*SQRT(U**2 + V**2 + W**2) !The contained function.
T = EXP(X)
F = T + DIST(T,SIN(X),ATAN(X) + 7) !Invoked...
END
SUBROUTINE POOBAH(TEXT,L,SEP) !I've got a little list!
CHARACTER*(*) TEXT !The supplied scratchpad.
INTEGER L !Its length.
CHARACTER*(*) SEP !The separator to be used.
INTEGER N !A counter.
L = 0 !No text is in place.
N = 0 !No items added.
CALL ADDITEM("first") !Here we go.
CALL ADDITEM("second")
CALL ADDITEM("third")
CONTAINS !Madly, defined after usage.
SUBROUTINE ADDITEM(X) !A contained routine.
CHARACTER*(*) X !The text of the item.
N = N + 1 !Count another item in.
TEXT(L + 1:L + 1) = CHAR(ICHAR("0") + N) !Place the single-digit number.
L = L + 1 !Rather than mess with unknown-length numbers.
LX = LEN(SEP) !Now for the separator.
TEXT(L + 1:L + LX) = SEP !Placed.
L = L + LX !Advance the finger.
LX = LEN(X) !Trailing spaces will be included.
TEXT(L + 1:L + LX) = X !Placed.
L = L + LX !Advance the finger.
L = L + 1 !Finally,
TEXT(L:L) = CHAR(10) !Append an ASCII line feed. Starts a new line.
END SUBROUTINE ADDITEM !That was bitty.
END SUBROUTINE POOBAH !But only had to be written once.
PROGRAM POKE
CHARACTER*666 TEXT !Surely sufficient.
INTEGER L
CALL POOBAH(TEXT,L,". ")
WRITE (6,"(A)") TEXT(1:L)
END
SUBROUTINE POOBAH(TEXT,N,SEP) !I've got a little list!
CHARACTER*(*) TEXT(*) !The supplied scratchpad.
INTEGER N !Entry count.
CHARACTER*(*) SEP !The separator to be used.
N = 0 !No items added.
CALL ADDITEM("first") !Here we go.
CALL ADDITEM("second")
CALL ADDITEM("third")
CONTAINS !Madly, defined after usage.
SUBROUTINE ADDITEM(X) !A contained routine.
CHARACTER*(*) X !The text of the item to add.
N = N + 1 !Count another item in.
WRITE (TEXT(N),1) N,SEP,X !Place the N'th text, suitably decorated..
1 FORMAT (I1,2A) !Allowing only a single digit.
END SUBROUTINE ADDITEM !That was simple.
END SUBROUTINE POOBAH !Still worth a subroutine.
PROGRAM POKE
CHARACTER*28 TEXT(9) !Surely sufficient.
INTEGER N
CALL POOBAH(TEXT,N,". ")
WRITE (6,"(A)") (TEXT(I)(1:LEN_TRIM(TEXT(I))), I = 1,N)
END
You may also check:How to resolve the algorithm Sum digits of an integer step by step in the Fōrmulæ programming language
You may also check:How to resolve the algorithm Conway's Game of Life step by step in the Julia programming language
You may also check:How to resolve the algorithm Find the last Sunday of each month step by step in the Batch File programming language
You may also check:How to resolve the algorithm Function composition step by step in the Octave programming language
You may also check:How to resolve the algorithm Flatten a list step by step in the Trith programming language