1.

How do we access a global variable in nested functions?(a) Simply seek the variable from the primary function(b) Make a copy of the global variables from the primary function(c) Declare the variable within the function(d) Declare the variable as globalI got this question in exam.Question is taken from Functions topic in portion MATLAB Basics of MATLAB

Answer»

Right choice is (d) DECLARE the VARIABLE as global

To ELABORATE: Any global variable, in the primary function, if required to be accessed by a nested function- the variable needs to be declared with the global keyword within the function which requires ACCESS to it. This allows sharing the variable amongst the primary and other functions. If the variable is declared without using the global keyword, the variable MIGHT bring an error to the function.



Discussion

No Comment Found

Related InterviewSolutions