Professional Documents
Culture Documents
Maslog
BSIT-1R9
Scope Rules
- 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.
Structure of a Function
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.
Here, 5 and 3 are the arguments passed to the add_numbers function, which
correspond to the a and b parameters, respectively.