CSC305 CHAPTER 2b
CSC305 CHAPTER 2b
Advantage Disadvantage
Less costly Reduced
programmer
flexibility
Example:
Compiler will report an error if operator is applied to
incompatible operand.
•Example of languages:
APL, SNOBOL4, Early version of LISP
STATIC vs DYNAMIC SCOPE
Static scope: names are associated to variables based on
their textual layout (spatial).
1. Numeric Types
a) Integer – the most common primitive numeric data types
C++ : (int, long), Pascal: (Integer, Longint), Java: (int, long)
c) Decimal
- store a fixed number of decimal digits with decimal points
- the primary data type for COBOL
DATA TYPES :Primitive Data Types
2. Boolean Types
2. Subrange types
A subragge type is a contiguous subsequence of an
ordinal type
Example: 12..14
Subrange types were introduced by Pascal & are
included in Ada
EXPRESSIONS & ASSIGNMENT
STATEMENTS
EXPRESSIONS
Left
– leftmost occurrence is evaluated first
associativity
Right
– rightmost occurrence is evaluated first
associativity
ARITHMETIC EXPRESSIONS
Parentheses:
Used by programmers to alter the precedence &
associativity rules by placing parentheses in expressions
A parenthesized part of an expression has precedence
over its adjacent unparenthesized parts
TYPE CONVERSIONS
2 types:
1. Narrowing conversion
Converts a value to a type that cannot store even
approximations of all the values of the original type
Example: converting a real to int (from 3.5 to 3)
Disadvantage: the value will change
2. Widening conversion
Converts a value to a type that can include at least
approximations of all the values of the original type
Example: converting an int to real (from 3 to 3.0)
Advantage: safe as there is no value change
Disadvantage: possible to have information loss &
incorrect calculation
TYPE CONVERSIONS
Conversion Explanation Advantages
Implicit It takes place when Improve the flexibility of a
conversion operands are language
(coercion) automatically - Limit the type checking by
converted into values automatically converting
of the correct data between operands, which are
type for an actually not the correct type
expression
Assignments as expression
Example:
The value of xz =
is y = z to y, and then value of y
assigned
is assigned to x. Finally the value of x, y and z are
similar to each other.
MIXED-MODE ASSIGNMENT
Binary operators combine two expressions into a
single expression.
A subprogram header:
Specifies that the following syntactic unit is a
subprogram definition of some particular kind
Provides a name for the subprogram
May optionally specify a list of parameters
Example: void display (parameters)
FUNDAMENTALS OF
SUBPROGRAM
The parameter profile:
aka signature
Contains the number, order & types of its formal parameter
Actual parameters:
Are parameters in the subprogram call statements
Used to be bound to the formal parameters of the
subprogram
FUNDAMENTALS OF
SUBPROGRAM
Keyword parameters:
The name of the formal parameters to which an actual
parameter is to be bound is specified with the actual
parameter
FUNDAMENTALS OF
SUBPROGRAM
2 categories of subprograms:
Procedures :
Collections of statements that define parameterized
computations
These computations are enacted by single call statement
Functions:
Functions structurally resemble procedures but are
semantically modeled on mathematical functions
The value produced by a function’s execution is returned
to the calling code, effectively replacing the call itself
PARAMETER-PASSING
METHODS
Implementation models for parameter passing:
Pass-by-value
Pass-by-result
Pass-by-value-result
Pass-by-reference
Pass-by-name