320 likes | 590 Views
Information Science 1 Fundamental Programming Constructs (2). College of Information Science and Engineering Ritsumeikan University. Week 12. Topics covered. Terms and concepts from Week 11 Structural decomposition as a method to manage program complexity
E N D
InformationScience 1Fundamental Programming Constructs (2) College of Information Science and EngineeringRitsumeikan University Week 12
Topics covered • Terms and concepts from Week 11 • Structural decomposition as a method to manage program complexity • Functions and procedures (subroutines) • concepts • parameter passing mechanism • Basic ideas of object-oriented programming (OOP) • terminology • OOP pros and cons • Quiz
Flow of control, sequence • Selection statement, if…else, switch • Clause, Body • Indentation • Nested • Repetition statement, loop, while, do…while, for • Counter, Sentinel • Infinite loop Recall Week 11
Class objectives • Discuss how to manage program complexity • Introduce the concepts of functions and subroutines, and discuss their scope and parameter passing mechanism • Learn basic concepts of object-oriented programming
Managing program complexity • When developing complex software, we usually want to make programs not just readable but “modular”. This allows us to reduce the cognitive load, because in a modular program, we can work with one piece of code at once, step by step, rather than with the whole algorithm • To achieve this, there are several programming constructs: • Functions • Procedures (also called Subroutines) • Classes and Objects • These constructs are used to decompose complex problems into smaller (and thus easier) tasks. The latter is called structural decomposition
Structural decomposition • Structural decomposition is often“nicknamed” as the “divide and conquer” (or “divide and rule”) technique • Various strategies, such as recursion, hierarchical construction, and logical breakdown, are used to decompose the program into smaller pieces • These smaller pieces are usually easier (than the whole code) to understand, and often they can be reused in other (not necessarily similar) programs
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 Function concept • 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
Three steps in using functions • Declare the function: • Known as function declaration • 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 • Define the function: • Known as function definition or function implementation • 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 • Call the function: • Known as function call or function invocation • Done by calling (e.g. in an assignment statement) the name of the function to execute it
Examples of function prototypes • float avrg(int n1, int n2, int n3); • Function avrg takes three integers as parameters and returns a floating-point value • 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) • void greeting( void ); • This function does not receive any parameters and it also does not return any value
function header function body Defining a function • In a high-level programming language, a typical function definition is as follows: return_type function_name (formal_parameter_list){ statements; return return_statement;} • The function header is similar to the prototype but with no semicolon • If the return_type is not a void, the function must have a return statement that evaluates to a value compatible with the function’s type. If it is a void, the return statement is optional
Calling a function • The name of a function is called in order to execute the function • A called function receives control from the calling module • When the called function completes its task, control is returned to the calling module • The called function may or may not return a value to the calling module • When they return a value, functions are usually used on the right side of an assignment statement
Recursive functions • 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; } • Recursion is a powerful programming technique in which programs are defined in terms of themselves • It must, however, be used with care, because recursive constructions are often not computationally efficient
Scope • Scope determines the area of the program in which an identifier is visible – the identifier can only be used (i.e. only exists) in that area • 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 • Scope that is enclosed in {} is called a block • Inner block can use identifiers that were declared outside of it, but outer block 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
formal parameter actual parameters Parameter passing • 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 • There are: • formal parameters which are parameters used in function definitions • actual parameters which are parameters used in function calls int factorial(int n) { if (n == 0) return 1; else return n * factorial(n - 1); } … factorial(42);
Passing by value • When data is passed by value, a copy of the data is created and placed in a local variable in the called function • 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: void swap(int a, int b) { int temp; temp = a; a = b; b = temp;}
Passing by reference • Passing by reference (or by address) is a mechanism that passes the address of a variable instead of its value • Passing by reference may cause side effects to the actual parameters • when the called function changes a value, it actually changes the original variable in the calling module • for example: void swap(int& a, int& b) { int temp; temp = a; a = b; b = temp;}
Procedure concept • Procedures (or subroutines) are similar to functions • Procedures encapsulate a group of statements into a single statement and are executed for their effect • Procedures often return nothing, but can also return calculated values implicitly • Procedures can call other procedures, but can also be nested • Procedures cannot be used on the right side of an assignment statement since they do not return a single value
Functions vs. procedures Functions Procedures • Can implicitly return more than one argument • A procedure is always a complete statement • To be used (called), a procedure must be: • declared, and • defined • Explicitly return only one argument • Usually, a function that returns a value is not a complete statement by itself • To be used (called), a function must be: • declared, and • defined
Object-oriented programming (OOP) concepts: Objects • An object is a software representation of something • 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. • Technically, an object can be seen as a data structure consisting of data fields and procedures (operations or methods) that can manipulate those fields. It can be implemented in any programming language
OOP concepts:Encapsulation • An object encapsulates (combines): • data (attributes, variables) • behaviour (operations, methods or procedures) • An object thus has data and “knows” how to use it • In “traditional” (non object-oriented) programming, data and procedures (operations, functions) are separated
OOP concepts:Information hiding • 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 • Sometimes, an object will also have internal methods that are inaccessible from the outside • Information unnecessary to the outside world is hidden and, thus, protected • Information hiding makes programs: • easierto 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)
OOP concepts:Methods (operations) • Conceptually, methods are similar to functions, procedures, subroutines, and similar programming constructs • Methods are called on an object • Sometimes, methods are described as sending a message to an object • A function can be seen as an object with a single method (such an object is usually called a functor)
OOP concepts:Classes • A class describes a type of object, and it is a template for objects. For example: • Cars, • GUI_Buttons, • Calendars, etc. • Each object is a (created) instance of exactly one class • e.g., the GUI button is an instance of the GUI_Buttons class • A class thus describes the state and behaviour that the created objects all share
Components of a class A class has: • Attributes (instance variables) • all instances have the same attributes • each instance may have different values for the attributes • Methods (operations) • describe the behaviour of every instance • Constructors (in some languages) • describe how to construct new instances
Summary • Managing program complexity is achieved via structural decomposition • Functions and procedures (subroutines) are “natural” pieces of a program • Parameters can be passed to a function (procedure, subroutine) by value or by reference • Objects (and classes) allow for further reducing program complexity in software design and development processes
Concepts you need to know after this class • Structural decomposition • Functions, procedures • Parameter passing • by value • by reference (address) • Objects, methods, information hiding, classes
Homework • Get the homework fromhttp://www.ritsumei.ac.jp/is/~rinaldo/InfoScience1/InfoScience1.html • Read these slides again and learn the new terms • Read the corresponding material in the textbook
Next class • Arrays and strings • Concepts of arrays and strings. Memory management. Main operations and application