Robotics 2: Global and Local Variables PDF
Document Details
Uploaded by SwiftAcropolis8964
Liceo de Cagayan University
Tags
Summary
This document explains global and local variables in robotics and programming. It describes the characteristics and differences between these types of variables. The content is presented as a lesson plan for students in senior high school focused on Arduino.
Full Transcript
ROBOTICS 2 LESSON 1 : UNDERSTANDIN G GLOBAL AND LOCAL VARIABLES SENIOR HIGH SCHOOL - MAIN CAMPUS LICEO DE CAGAYAN UNIVERSITY TARGET OUTCOMES : At the end of the discussion, the students should be able to: Define what is Variables Identify the difference between Gl...
ROBOTICS 2 LESSON 1 : UNDERSTANDIN G GLOBAL AND LOCAL VARIABLES SENIOR HIGH SCHOOL - MAIN CAMPUS LICEO DE CAGAYAN UNIVERSITY TARGET OUTCOMES : At the end of the discussion, the students should be able to: Define what is Variables Identify the difference between Global and Local Variables. Create Projects that use Global and Local Variables in codes. SENIOR HIGH SCHOOL - MAIN CAMPUS LICEO DE CAGAYAN UNIVERSITY VARIABLE S A variable is a named storage location in memory that can hold a value. Variables in Arduino can be of different types (e.g., `int`, `float`, `char`, etc.). SENIOR HIGH SCHOOL - MAIN CAMPUS LICEO DE CAGAYAN UNIVERSITY GLOBAL VARIABLES Global variables are declared outside of any function and can be accessed by any function within the same file. From the point of declaration to the end of the program. SENIOR HIGH SCHOOL - MAIN CAMPUS LICEO DE CAGAYAN UNIVERSITY GLOBAL VARIABLES In this example, `int x=7` is a global variable, and it can be accessed and modified in both `setup()` and `loop()` functions. SENIOR HIGH SCHOOL - MAIN CAMPUS LICEO DE CAGAYAN UNIVERSITY LOCAL VARIABLES Local variables are declared within a function, and they can only be accessed within that function. From the point of declaration until the end of the function. SENIOR HIGH SCHOOL - MAIN CAMPUS LICEO DE CAGAYAN UNIVERSITY LOCAL VARIABLES In this example, `int x=7` is a local variable declared inside the `loop()` function. It will be reset every time the `loop()` restarts. SENIOR HIGH SCHOOL - MAIN CAMPUS LICEO DE CAGAYAN UNIVERSITY KEY DIFFENCES BETWEEN GLOBAL AND LOCAL VARIABLES SCOPES GLOBAL : Accessible from any function in the programs LOCAL : Accessible only within the function where they are defined. SENIOR HIGH SCHOOL - MAIN CAMPUS LICEO DE CAGAYAN UNIVERSITY KEY DIFFENCES BETWEEN GLOBAL AND LOCAL VARIABLES LIFETIME GLOBAL : Exist for lifetime of the program. LOCAL : Created when the function is called and destroyed when the function exits. SENIOR HIGH SCHOOL - MAIN CAMPUS LICEO DE CAGAYAN UNIVERSITY KEY DIFFENCES BETWEEN GLOBAL AND LOCAL VARIABLES MEMORY GLOBAL : Stored in a fixed memory location. LOCAL : Stored on stack; memory space is allocated when the function starts. SENIOR HIGH SCHOOL - MAIN CAMPUS LICEO DE CAGAYAN UNIVERSITY ACTIVITY 1 : UNDERSTANDING LOCAL AND GLOBAL VARIABLES SENIOR HIGH SCHOOL - MAIN CAMPUS LICEO DE CAGAYAN UNIVERSITY TARGET OUTCOMES : At the end of the discussion, the students should be able to: Build and key in the code based on the circuit diagram. SENIOR HIGH SCHOOL - MAIN CAMPUS LICEO DE CAGAYAN UNIVERSITY ACTIVITY 1 : UNDERSTANDING LOCAL AND GLOBAL VARIABLES MATERIALS SENIOR HIGH SCHOOL - MAIN CAMPUS LICEO DE CAGAYAN UNIVERSITY ACTIVITY 1 : UNDERSTANDING LOCAL AND GLOBAL VARIABLES