What does the following code print when it is invoked from the command line with
ID: 3860251 • Letter: W
Question
What does the following code print when it is invoked from the command line with funone. Be reasonably careful with the spacing. Note that blanks is a built-in function which generates a string with the specified number of spaces. function funone() a = 0: indent(a, 'hello'): funtwo(a + 3): indent (a, 'ciao'): funthree(a + 3): indent(a, 'done'): end function funtwo(b) indent(b, 'intwo'): funthree(b+3): indent(b, 'outtwo'): end function funthree(a) indent(a, 'inthree') end function indent(b, msg) fprintf ('%d%s %s ', b, blanks(b), msg): endExplanation / Answer
Executing statements in function funone():
1. Variable a is initialized to 0.
2. Function call indent(a, 'hello') => indent(0, 'hello')
2.1 With in indent function:
Prints b value followed by b spaces and then string. As b is 0
Output: 0 hello
3. Function call funtwo(a+3) => funtwo(0+3) => funtwo(3)
3.1 With in the funtwo function:
3.1.1 Calls indent function: indent(3, 'intwo')
3.1.1.a With in indent function:
Prints b value followed by b spaces and then string. As b is 3
Output: 3 intwo
3.1.2 Calls funthree function: funthree(3+3) => funthree(6)
3.1.2.a With in funthree function:
Calls indent function => indent(6, 'inthree')
Output: 6 inthree
3.1.3 Calls indent function: indent(3, 'outtwo')
3.1.3.a With in indent function:
Prints b value followed by b spaces and then string. As b is 3
Output: 3 outtwo
4. Function call indent(a, 'ciao') => indent(0, 'ciao')
4.1 With in indent function:
Prints b value followed by b spaces and then string. As b is 0
Output: 0 ciao
5. Function call funthree(a+3); => funthree(0+3) => funthree(3)
5.1 With in the funthree function:
5.1.1 Calls indent function: indent(3, 'inthree')
5.1.1.a With in indent function:
Prints b value followed by b spaces and then string. As b is 3
Output: 3 inthree
6. Function call indent(a, 'done') => indent(0, 'done')
4.1 With in indent function:
Prints b value followed by b spaces and then string. As b is 0
Output: 0 done
______________________________________________________________________________________________________________________________
Hence the final output will be :
0 hello
3 intwo
6 inthree
3 outtwo
0 ciao
3 inthree
0 done
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.