Computer Science, asked by aleza1, 7 months ago

A program uses a function named convert() in addition to its main function. The function main° declares the variable x within its body and the function convert declares two variables y and z within its body, z is made static. A fourth variable m is declared ahead(i.e. at top) of both the functions. State the visibility and lifetime of each of these variables

Answers

Answered by shivamnautiyal06
1

Answer:

the declaration of variables would look like this:

//-- start of program --//

var m;

function main() {

   var x;

   ...

}

function convert() {

   var y;

   static var z;

   ....

}

//-- end of program  --//

Here, the variable "m" is accessible throughout the entire program (that is, outside and inside of any of the functions). This variable is called a global variable. It's created when the program starts and is destroyed when the program ends.

The variable "x" is only accessible inside the main() function. It's created when the program execution enters the main() function and is destroyed when the program execution exits the main() function.

The variable "y", much like "x", is only accessible inside the function it's declared. In this case, inside the convert() function. It's lifetime starts when the program execution enters the convert() function and ends when the program execution exits the convert() function.

The variable "z" is only accessible inside the convert() function as well. However, because it's declared static, it's initialized only ONCE, and that is when the program execution FIRST enters the convert() function and is destroyed ONLY when the program ends.

Similar questions