You are on page 1of 2

Shile Lleorah U.

Maslog

BSIT-1R9

Scope Rules

1. Local variables and Global Variables


- The scope under which local variables are declared, like a function, determines their
accessibility. They are destroyed after the block is exited and only exist momentarily
during the execution of that particular code block.

- Global variables, are defined outside of all functions and can be accessed from
anywhere in the code, even functions. For the duration of the program’s execution,
they remain valuable. However, due to their widespread accessibility, using global
variables excessively might make code more difficult to read and maintain.

2. Formal Parameters
- Formal parameters are limited to the function in which they are declared. They are
also referred to as function parameters or arguments. They serve as stand-ins for the
values that are supplied into the function during callback operations. The function
body where these arguments are specified is the only place they can be accessed.
After the function has finished running, its scope closes and it is no longer available.

Methods and Functions

Structure of a Function

- The structure of a function involves three main components: declaration, definition,


and invocation. Declaration outlines the function’s details, definition contains the
actual logic, and invocation is where the function is called within a program.
Functions are crucial for organizing code, facilitating reusability, and improving
readability.

Declaring Functions

- Declaring a function involves specifying its name, return type, and parameters (if
any). It’s like announcing the function’s existence and what it expects to receive and
return. In many programming languages, function declaration includes the function’s
name, the type of data it returns (if any), and the types of parameters it accepts (if
any). For example, in Python:
- Here, `add_numbers` is the function name, `(a, b)` are the parameters, and `return`
specifies what the function gives back.

Passing Arguments

- Passing arguments to a function means providing values for the parameters defined
in the function’s declaration. When you invoke or call a function, you pass specific
values (arguments) into the function’s parameters.

For instance, in Python, using the add_numbers function defined earlier:

Here, 5 and 3 are the arguments passed to the add_numbers function, which
correspond to the a and b parameters, respectively.

You might also like