answersLogoWhite

0


Best Answer

If this is a homework assignment, please consider trying it yourself first, otherwise the value of the reinforcement to the lesson offered by the homework will be lost on you.

Since C and C++ pass parameters by value, local variables can be passed between functions (subroutines).

If you intend to pass them by reference, i.e. by passing their address, you must ensure that they do not go out of scope while the called function runs, otherwise the called function might reference memory that is no longer valid.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Can local variables be passed between subroutines?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Are C variables initialized to 0 by default?

Only global/static variables are, local variables aren't.


Local Variables will get stored in?

Stack.


Mention the importance of subroutines in programming?

A subroutine is typically defined as being a function or procedure that can be invoked at any point in a program, causing the procedural execution to branch to the subroutine. This is not unlike a goto or jump statement, however subroutines also make use of the program's call stack to allow values (arguments or parameters) to be passed to the subroutine, including the address of the caller so that execution can return to the point of the call when the subroutine falls from scope. A subroutine can also (optionally) return a single value to the caller, known as the return value. Return values are temporary and will fall from scope when the expression that invoked the subroutine falls from scope. However, by assigning a subroutine to a variable, the return value can be stored in that variable and can then used in subsequent expressions. Subroutines pop their arguments from the stack and construct local variables or references from the argument values. Arguments passed by reference can therefore be used to return values through the referenced values. Values returned in this manner are known as output arguments. Subroutines that use output arguments often use the subroutine's temporary return value to indicate any errors that occurred within the subroutine, with zero indicating success (no error).


When variables are declared in the body of a method they are know as what?

Local Variables There are two types of variables based on the location of declaration 1. Instance Variables- Declared inside a class, but outside of any method's body. 2. Local Variables- Declared inside a method's body inside a class.


How are coupling and cohesion related to modular design?

Ideally, modules will have low coupling and high cohesion. Coupling describes the strength of the connection between modules in a program. Loose (or low) coupling occurs when modules do not depend on other modules. One way to control this is by avoiding the use of global variables and reducing the number of variables that are passed between the modules. Another is to limit the depth of module calls (where a module calls another module, that then calls another module, and so on). Cohesion is a measure of how well a module accomplishes the module's purpose. High cohesion implies that all the module's internal statements serve to perform the module's (single) task. In order for modules to work together, there must be some connection between them. The nature of the connection is important because it determines the extent to which the modules are coupled. How are they connected? The best way to connect them is to pass the value of a local variable in one module to a second module through its parameter list. (A local variable is a variable that is defined within a module (not a parameter) is local to that module. The values of local variables are not available outside of the module in which they are declared unless they are passed. Local variables are reset to their default values once control leaves the module in which they are declared.) Another way to share information is through the use of global variables. (A variable that is defined outside of a module and that does not need to be passed to a module to be accessed by it is a global variable. Global variables retain their value once control leaves the module in which they are referenced. ) Because the value of a global variable can be changed by any module without passing, it increases the coupling between modules.

Related questions

What are the differences between global variables and local variables in C plus plus?

Global variables can be seen in all blocks of your program, when local variables are visible only within the block where it's declared.


What is difference between auto and local variables in c?

Nothing.


Why can't java compiler initialize local variables?

Its not that the compiler can't initialize local variables; its that the compiler does not initialize local variables.This is by design and language specification. If you want to initialize local variables, you must explicitly do so.


Where auto variables are stored?

Auto variables are stored on the stack alongside all other local variables.


Are C variables initialized to 0 by default?

Only global/static variables are, local variables aren't.


Where does Local Variables get stored in?

Stack.


Local Variables will get stored in?

Stack.


Mention the importance of subroutines in programming?

A subroutine is typically defined as being a function or procedure that can be invoked at any point in a program, causing the procedural execution to branch to the subroutine. This is not unlike a goto or jump statement, however subroutines also make use of the program's call stack to allow values (arguments or parameters) to be passed to the subroutine, including the address of the caller so that execution can return to the point of the call when the subroutine falls from scope. A subroutine can also (optionally) return a single value to the caller, known as the return value. Return values are temporary and will fall from scope when the expression that invoked the subroutine falls from scope. However, by assigning a subroutine to a variable, the return value can be stored in that variable and can then used in subsequent expressions. Subroutines pop their arguments from the stack and construct local variables or references from the argument values. Arguments passed by reference can therefore be used to return values through the referenced values. Values returned in this manner are known as output arguments. Subroutines that use output arguments often use the subroutine's temporary return value to indicate any errors that occurred within the subroutine, with zero indicating success (no error).


When variables are declared in the body of a method they are know as what?

Local Variables There are two types of variables based on the location of declaration 1. Instance Variables- Declared inside a class, but outside of any method's body. 2. Local Variables- Declared inside a method's body inside a class.


Where local variables are stored in c?

On the stack.


How are coupling and cohesion related to modular design?

Ideally, modules will have low coupling and high cohesion. Coupling describes the strength of the connection between modules in a program. Loose (or low) coupling occurs when modules do not depend on other modules. One way to control this is by avoiding the use of global variables and reducing the number of variables that are passed between the modules. Another is to limit the depth of module calls (where a module calls another module, that then calls another module, and so on). Cohesion is a measure of how well a module accomplishes the module's purpose. High cohesion implies that all the module's internal statements serve to perform the module's (single) task. In order for modules to work together, there must be some connection between them. The nature of the connection is important because it determines the extent to which the modules are coupled. How are they connected? The best way to connect them is to pass the value of a local variable in one module to a second module through its parameter list. (A local variable is a variable that is defined within a module (not a parameter) is local to that module. The values of local variables are not available outside of the module in which they are declared unless they are passed. Local variables are reset to their default values once control leaves the module in which they are declared.) Another way to share information is through the use of global variables. (A variable that is defined outside of a module and that does not need to be passed to a module to be accessed by it is a global variable. Global variables retain their value once control leaves the module in which they are referenced. ) Because the value of a global variable can be changed by any module without passing, it increases the coupling between modules.


What is the difference between a static variable a global variable and a local variable?

A static variable is a variable allocated in static storage. A local variable is a variable declared inside a function. A global variable is a variable declared outside of any class or function. Note that local variables and global variables can both be allocated in static storage.