Knowee
Questions
Features
Study Tools

Explain global, build-in variables with an example

Question

Explain global, build-in variables with an example

🧐 Not the exact question you are looking for?Go ask a question

Solution

Global variables in programming are those that are defined outside of functions. They can be accessed from any part of the code, even inside functions. However, it's generally considered best practice to avoid using global variables when possible, as they can make code harder to understand and debug.

Built-in variables, on the other hand, are those that are automatically defined by the programming language itself. These can include things like constants or functions that are always available for use.

Here's an example in Python:

# This is a global variable
global_var = "I'm a global variable"

def my_function():
    # This is a local variable
    local_var = "I'm a local variable"
    # We can still access the global variable from inside this function
    print(global_var)

# Call the function
my_function()

# This will print: I'm a global variable

# Now let's use a built-in variable. In Python, one example is the __name__ variable.
if __name__ == "__main__":
    print("This code is being run directly, not imported as a module.")

In this example, global_var is a global variable, local_var is a local variable, and __name__ is a built-in variable.

This problem has been solved

Similar Questions

define Local and Global variables and the difference between them

Where are variables with global scope accessible?

Provide the correct answer(s) in the blank(s) Global variables are declared outside of any function. A global variable is visible to any every function and can be used by any piece of code. Unlike local variable, global variables retain their values between function calls and throughout the program execution. Let us consider an example: #include < stdio.h > int a = 20; // Global declaration void test(); void main() { printf("In main() function a = %d\n", a); // Prints 20 test(); a = a + 15; // Uses global variable printf("In main() function a = %d\n", a); // Prints 55 } void test() { a = a + 20; // Uses global variable printf("In test() function a = %d\n", a); // Prints 40 } In the above code the global variable a is declared outside of all the functions. So, the variable a can be accessed in every function. Operating System calls the main() function at the time of execution. the variable a has no local declaration, so it access the global variable a. In test() function also there is no local declaration of variable a, the variable a gets access from the global. The global variables are destroyed only after completion of execution of entire program. What is the output for the following code #include <stdio.h> int a = 10; void test(); void main() { printf("In main() function a = %d\n", a); test(); a = a + 10; printf("In main() function a = %d\n", a); } void test() { a = a + 20; printf("In test() function a = %d\n", a); }

What is a variable in programming and why are they used?

Fill in the blanks. __________are the variables declared inside a function. a. Immediate variables b. Global variables c. Local variables d. None of these

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.