Compare and contrast the difference between return values and parameters in functions. How do they used? How do they work? Are they always needed? Provide and explain a code snippet example.
Function does not return a value.As before this function does not return a value hence it is declared as having type
void
. It now takes an integer parameter n
which indicates the number of lines to be skipped. The parameter list then consists of a type and a name for this formal parameter. Inside the body of the function (enclosed in {}
) a loop control variable i
is declared. This variable is a local variable to the function. A local variable defined within the body of the function has no meaning, or value, except within the body of the function. It can use an identifier name that is used elsewhere in the program without there being any confusion with that variable. Thus changing the value of the local variable i
in the function skip will not affect the value of any other variable i
used elsewhere in the program. Similarly changing the value of a variable i
used elsewhere in the program will not affect the value of the local variable i
in skip
.void skip(int n) // Function skips n lines on output { int i; // a local variable to this function // now loop n times for (i = 0; i < n; i++) cout << endl; }
No comments:
Post a Comment