0% found this document useful (0 votes)
6 views10 pages

Wk09a Methods Scope

This document discusses the concepts of arguments and parameters in methods, highlighting that arguments are passed to methods while parameters are received. It explains variable scope, detailing the differences between global, local, and block scopes, and emphasizes the importance of declaring variables in the smallest scope possible to avoid name clashes and unintended mutations. The document also includes examples of variable accessibility within different scopes in programming.

Uploaded by

jairo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views10 pages

Wk09a Methods Scope

This document discusses the concepts of arguments and parameters in methods, highlighting that arguments are passed to methods while parameters are received. It explains variable scope, detailing the differences between global, local, and block scopes, and emphasizes the importance of declaring variables in the smallest scope possible to avoid name clashes and unintended mutations. The document also includes examples of variable accessibility within different scopes in programming.

Uploaded by

jairo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Methods – Scope

(Part 3 of 5)
Programming I
Narendra Pershad
Centennial College
Topics

 Method header
 Writing and calling methods Already covered
 Built-in methods

 Argument and parameter


 Variable scope
Argument and parameter

 When a method is called … the thing that is passed


to the method is called an argument or arguments.
 When you are calling methods, you talk about
argument

 In the method the thing is received is called a


parameter(s)
 When you are writing methods, you talk about
parameters
Scope

 This deals with visibility or access of variables.


 A method is like a mini-program.
 You may declare variable inside of them.
Class
Definition

Variable declarations (visible to all


members)
Also called global variables

Variable declarations (only visible in


method)
Also called local variables

Variable declarations (only visible in Method


method) Definition
Also called local variables

Variable declarations (only visible in


method)
Also called local variables
Scope
public class Program {
static int var_a; //class scope/ global variable

public static void Method_B() {


int var_b; //method scope/ local variable
}

public static void Method_C() {


int var_c; //method scope/ local variable
for(; ;) {
int var_d; //block scope
{
int var_e; //block scope
}
}
}
} Type of a variable
public class Program {
static int var_a;
public static void Method_B() {
int var_b;
//var_a and var_b acessible
}
public static void Method_C() {
int var_c;
//var_a and var_c acessible
for() {
int var_d;
//var_a, var_c and var_d acessible
{
int var_e;

Variables is scope
//var_a, var_c , var_d and var_e acessible
}
Scoping

 What happens when multiple variables with the


same name occurs in the same scope?
 The one within the closest scope prevails.
 It is good practice to avoid name clashes when
declaring local variables.
 The compiler enforces unique name declaration.

 It is impossible to access a variable that is declare


in another scope such as another method.
Scoping in action
for(int i = 0, sum = 0; i < 5; i++)
{
sum += i;
}
Console.WriteLine($"Total is: {sum}");"

int sum = 0;
for(int i = 0; i < 5; i++)
{
sum += i;
}
Console.WriteLine($"Total is: {sum}");"
Summary

 Argument is the thing that you send to a method.


 Parameter is the thing that the method gets.
 Variable may be declared in either of three scopes: global, local or
block
 You should declare variables in the smallest block allowable.
 Avoid global scope
 Although it is less complicated to use
 Any code with access can mutate the variable

You might also like