Information Science 1 - 立命館大学piumarta/is1/slides/IS1-12.pdf · College of Information...
-
Upload
nguyenlien -
Category
Documents
-
view
218 -
download
0
Transcript of Information Science 1 - 立命館大学piumarta/is1/slides/IS1-12.pdf · College of Information...
Information Science 1
Fundamental Programming Constructs (2)
College of Information Science and Engineering Ritsumeikan University
Week 12
2
Topics covered l Terms and concepts from Week 11 l Structural decomposition as a method to
manage program complexity l Functions and procedures (subroutines)
– concepts – parameter passing mechanism
l Basic ideas of object-oriented programming (OOP) – terminology – OOP advantages and disadvantages
3
Recall Week 11 l Flow of control, sequence l Selection statement, if…else, switch l Clause, Body l Indentation l Nested l Repetition statement, loop, while,
do…while, for l Counter, Sentinel l Infinite loop
4
Class objectives l Discuss how to manage program
complexity
l Introduce the concepts of functions and subroutines, and discuss their scope and parameter passing mechanism
l Learn basic concepts of object-oriented programming
5
Managing program complexity l When developing complex software, we usually
make programs “modular”. In a modular program, we can then work with one piece of code at once, step by step, rather than with the whole algorithm
l To create modules, there are several programming constructs: – Functions, Procedures (also called Subroutines), or – Classes and Objects – These constructs are used to decompose
complex problems into smaller and easier tasks. This is called algorithmic decomposition, or object-oriented decomposition
l Let’s say “structural decomposition” to mean either of these kinds of decomposition
6
Structural decomposition l Structural decomposition is often
“nicknamed” as the “divide and conquer” (or “divide and rule”) technique
l Various strategies, such as recursion, hierarchical construction, and logical breakdown, are used to decompose the program into smaller pieces
l These smaller pieces are usually easier (than the whole code) to understand, and often they can be reused in other (not necessarily similar) programs
7
Function concept l A function is a mini program that performs one
well-defined task. A function always has: – a set of inputs – one output – a body of statements
Side effects may occur while the function is executing, but before the function returns Examples of side effects: • changing an input variable • printing output to the screen • changing a global variable
8
Three steps in using functions 1. Declare the function:
l Known as function declaration l Done by writing a function prototype that specifies:
– the name of the function (i.e. its identifier) – the type of its return value – a list of its arguments and their types
2. Define the function: l Known as function definition or function implementation l Done by writing a block of statements of the function
(i.e. the function body) to define processes that should be done by the function
3. Call the function: l Known as function call or function invocation l Done by calling (e.g. in an assignment statement) the
name of the function to execute it
9
Examples of function prototypes
l float avrg(int n1, int n2, int n3); — Function avrg takes three integers as
parameters and returns a floating-point value
l void mix(double n4, int n5); — This function receives a double and an integer
value as parameters. However, it does not return any value (i.e. its type is void)
l void greeting(void); – This function does not receive any parameters
and it also does not return any value
10
Defining a function l In a high-level programming language, a typical
function definition is as follows: return_type function_name(formal_parameter_list) { statements; return return_expression;
}
function header
function body
– The function header is similar to the prototype but with no semicolon
– If the return_type is not void, the function must have a return expression that evaluates to a value compatible with the function’s type. If it is a void, the return expression (and the return statement itself) is optional.
11
Calling a function l The name of a function is called in order to
execute the function l A called function receives control from the
calling module l When the called function completes its task,
control is returned to the calling module l The called function may or may not return a
value to the calling module l When they return a value, functions are
usually part of an expression
12
Recursive functions l A function that calls itself is known as a
recursive function – for example: int factorial(int n) { if (n>1) return n*factorial(n-1); return 1; }
l Recursion is a powerful programming technique in which programs are defined in terms of themselves
13
Scope l Scope determines the area of the program in
which a variable is visible – the variable can only be used in that area
l Examples: – Scope of a local variable, i.e., a variable declared inside a function
(or module): only in the function body where it was declared – Scope of a global variable, i.e., a variable declared outside of any
function (module): everywhere in the program
l A block {} creates a new inner scope l Inner blocks can use identifiers that were declared
outside of it, but outer blocks cannot use identifiers that were declared in an inner block – any function can use any global variables – you cannot use any local variables from a function in
another function: scopes are hierarchical
14
Scope int a;// a is in scope (visible)int f(int b) { // a, f and b are in scope int c; // a, f, b and c are in scope { int d; // a, f, b, c, and d are in scope } // a, f, b, and c are in scope { int e; // a, f, b, c and e are in scope }}// a and f are in scopeint g(int z) { // a, f, g and z are in scope}
15
Parameter passing l To call a function, we write its name and pass it
some data which are called parameters. Passing data when calling a function is called parameter passing
l There are: – formal parameters which are parameters used in
function definitions – actual parameters (arguments) which are parameters
used in function calls int factorial(int n) {
if (n == 0) return 1; else return n * factorial(n - 1);
}
…
factorial(42);
actual parameters
formal parameter
16
Passing by value l When data is passed by value, a copy of the
data is created and placed in a local variable in the called function
l Passing by value does not cause any side effects to the original variable – after the function call is completed, the original
data remains unchanged – for example, this does not work: void swap(int a, int b) { int temp; temp = a; a = b; b = temp; }
17
Passing by reference l Passing by reference passes the address of
a variable (not its value) l Passing by reference may cause side
effects to the actual parameters – when the function changes a value, it actually
changes the original variable in the caller – C++ supports pass-by-ref; C can simulate it:
void swap(int &a, int &b) { int temp = a; a = b; b = temp; }
swap(x, y);
void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; }
swap(&x, &y);
C++ C
18
Procedure concept l Procedures (or subroutines) are similar to
functions l Procedures encapsulate a group of
statements into a single statement and are executed for their effect
l Procedures often return nothing, but can also return calculated values implicitly
l Procedures can call other procedures and can be nested
l Procedures cannot be used in an expression since they do not return a value
19
Functions vs. Procedures Functions Procedures
l Explicitly returns a value l Can implicitly return
more results l A function can be part of
a larger expression l To be used (called), a
function must be: – declared, and – defined
l Cannot return a value l Can implicitly return
results l A procedure is called as
a single statement l To be used (called), a
procedure must be: – declared, and – defined
20
Object-oriented programming (OOP) concepts: Objects
l An object is a software representation of something
l An object can represent: – a real-world object (e.g., a car), – a part of software (e.g., a GUI button), – a concept or idea (e.g., a plan), etc.
l Technically, an object can be seen as a data structure consisting of data fields and procedures (operations or methods) that can manipulate those fields. OOP can be implemented in any programming language
21
OOP concepts: Encapsulation
l An object encapsulates (combines): – data (attributes, variables) – behaviour (operations, methods or
procedures) l An object thus has data and “knows”
how to use it l In “traditional” (non object-oriented)
programming, data and procedures (operations, functions) are separated
22
OOP concepts: Information hiding
l An object’s data is internal to the object, and this data is either inaccessible from the outside or can be accessed only via methods
l Sometimes, an object will also have internal methods that are inaccessible from the outside
l Information that is unnecessary to the outside world is hidden and, thus, protected
l Information hiding makes programs: – easier to understand (we only need to understand the
interface but not the internal code), and – easier to maintain (changes to the internal code do not
force changes in other objects, and the internal code is protected from outside interferences)
23
OOP concepts: Methods (operations)
l Conceptually, methods are similar to functions, procedures, subroutines, and similar programming constructs
l Methods are invoked on an object l Sometimes, this is described as
“sending a message” to an object – the object that receives the message – the name of the method – the arguments of the message
24
OOP concepts: Classes
l A class describes a type of object, and it is a template for objects. For example: – Cars, – GUI_Buttons, – Calendars, etc.
l Each object is a (created) instance of exactly one class – e.g., a GUI button is an instance of the
GUI_Button class l A class describes the state and behaviour
that all its created objects share
25
Components of a class A class has: l Attributes (instance variables)
– all instances have the same attributes – each instance may have different values
for the attributes l Methods (operations)
– describe the behaviour of every instance l Constructors (in some languages)
– describe how to construct new instances
27
OOP in C typedef struct { float x, y; } Point;
Point *newPoint(float x, float y) { Point *p= malloc(sizeof(Point)); p->x= x; p->y= y; return p;}
float Point_x(Point *p) { return p->x; }float Point_y(Point *p) { return p->y; }float Point_r(Point *p) { return sqrt(p->x * p->x + p->y * p->y);}float Point_theta(Point *p) { return atan2(p->y, p->x);}
attributes (instance variables)
class name
constructor
methods (operations)
28
OOP in C Same interface, same behaviour, different representation and implementation:
typedef struct { float r, theta; } Point;
Point *newPoint(float x, float y) { Point *p= malloc(sizeof(Point)); p->r = sqrt(x * x + y * y); p->theta = atan2(y, x); return p;}
float Point_x(Point *p) { return p->r * cos(p->theta); }float Point_y(Point *p) { return p->r * sin(p->theta); }float Point_r(Point *p) { return p->r; }float Point_theta(Point *p) { return p->theta; }
int main(int argc, char *argv[]) { Point *p= newPoint(3, 4); printf("%f\n", Point_r(p)); return 0;}
instantiate (create) a Point ‘p’
invoke method ‘r’ on the object ‘p’
29
Summary l Managing program complexity is achieved
by structural decomposition l Functions and procedures (subroutines) are
“reusable” pieces of a program l Parameters can be passed to a function
(procedure, subroutine) by value or by reference
l Functions/procedures are algorithmic decompostions of the design process;
classes are data-oriented decompositions
30
Concepts you need to know after this class
l Structural decomposition l Functions, procedures l Parameter passing
— by value — by reference (address, C++)
l Objects, methods, information-hiding, classes
31
Homework l Read these slides again l Do the self-preparation
assignments l Learn the vocabulary l Consult, whenever necessary, the
textbook materials
32
l Arrays and strings – Concepts of arrays and strings – Memory management – Main operations and application
Next class