Week 6 subject 1: Scope of variables
- Variables have scopes, i.e., parts of the program where they can be referenced.
- There are 2 types of variables based on their scopes:
- Global variables: those defined outside any function, and
- Local variables: those defined inside a function
- Global variables are valid inside functions, but local variables aren't valid outside functions.
- All variables must be first defined before referenced, within their respective scopes.
- Functions are like global variables, they must be defined before being called.
- Expand your knowledge by doing research of the concept Namespace.
- Traditionally programming languages are divided into 2 wide categories: procedural programming languages and object-oriented programming languages. Earlier languages are typically procedural languages, including BASIC, C, Fortran and many others. More recent languages are often OOPLs, among which there are C++, Java and Python.
- Programming in procedural languages is like writing a cook book, which includes a series of steps that are executed either sequentially or according to defined control flows.
- On the other hand OOPLs mimic the real world by defining 'objects' and how they interact with each other, so that we can stay reasonably far away from the tedious work of recreating all the 'steps' each time we need to describe the interactions, and let the 'objects' do it according to their definitions.
No comments:
Post a Comment