Year 11 Exam  >  Year 11 Notes  >  Computer for GCSE/IGCSE  >  Local & Global Variables

Local & Global Variables | Computer for GCSE/IGCSE - Year 11 PDF Download

Local & Global Variables

  • A local variable is a variable declared within a subroutine and is accessible only from within that subroutine.
  • A global variable, on the other hand, is a variable declared outside of any subroutine and can be accessed from any part of the program.
  • Global variables facilitate passing data between different subroutines.
  • Excessive reliance on global variables can complicate program comprehension and debugging, making it a discouraged practice due to its negative impact on code maintainability and readability.

Pseudocode example:

global total_score
FUNCTION calculate_average(num1: INTEGER, num2: INTEGER)
    average ← (num1 + num2) / 2
    RETURN average
END FUNCTION
total_score ← 0
PROCEDURE add_score(score: INTEGER)
    total_score ← total_score + score
END PROCEDURE

Python example:

def calculate_average(num1, num2):
    average = (num1 + num2) / 2
    return average
total_score = 0
def add_score(score):
    global total_score
    total_score = total_score + score

Java example:

static int totalScore = 0;
public static void main(String args[]) {
    double average = calculateAverage(5, 7);
    System.out.println("Average: " + average);
    addScore(10);
    System.out.println("Total score: " + totalScore);
}
public static double calculateAverage(int num1, int num2) {
    double average = (num1 + num2) / 2.0;
    return average;
}
public static void addScore(int score) {
    totalScore = totalScore + score;
}

Visual Basic example:

Dim totalScore As Integer = 0
Dim average As Double = CalculateAverage(5, 7)
        Console.WriteLine("Average: " & average)
        AddScore(10)
      Console.WriteLine("Total score: " & totalScore)
Function CalculateAverage(num1 As Integer, num2 As Integer) As Double
        Dim average As Double = (num1 + num2) / 2.0
        Return average
End Function
Sub AddScore(score As Integer)
        totalScore = totalScore + score
End Sub

The document Local & Global Variables | Computer for GCSE/IGCSE - Year 11 is a part of the Year 11 Course Computer for GCSE/IGCSE.
All you need of Year 11 at this link: Year 11
92 docs|30 tests

Top Courses for Year 11

92 docs|30 tests
Download as PDF
Explore Courses for Year 11 exam

Top Courses for Year 11

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

study material

,

Important questions

,

Objective type Questions

,

MCQs

,

Sample Paper

,

past year papers

,

Semester Notes

,

Free

,

Local & Global Variables | Computer for GCSE/IGCSE - Year 11

,

video lectures

,

pdf

,

Local & Global Variables | Computer for GCSE/IGCSE - Year 11

,

ppt

,

Exam

,

Previous Year Questions with Solutions

,

mock tests for examination

,

practice quizzes

,

Extra Questions

,

Summary

,

Local & Global Variables | Computer for GCSE/IGCSE - Year 11

,

Viva Questions

,

shortcuts and tricks

;