COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

Similar documents
COSC 2P91. Bringing it all together... Week 4b. Brock University. Brock University (Week 4b) Bringing it all together... 1 / 22

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

Pointers. A pointer is simply a reference to a variable/object. Compilers automatically generate code to store/retrieve variables from memory

Pointers in C/C++ 1 Memory Addresses 2

6. Pointers, Structs, and Arrays. 1. Juli 2011

QUIZ. What are 3 differences between C and C++ const variables?

Short Notes of CS201

In Java we have the keyword null, which is the value of an uninitialized reference type

CS201 - Introduction to Programming Glossary By

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Chapter 1 Getting Started

These are notes for the third lecture; if statements and loops.

Slide Set 6. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Lab 9: Pointers and arrays

QUIZ Friends class Y;

Object oriented programming C++

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

CS201 Some Important Definitions

D Programming Language

EL6483: Brief Overview of C Programming Language

Intro. Scheme Basics. scm> 5 5. scm>

Separate Compilation Model

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

The Dynamic Typing Interlude

Memory and Pointers written by Cathy Saxton

CSE 333 Midterm Exam July 24, Name UW ID#

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

QUIZ. What is wrong with this code that uses default arguments?

6.096 Introduction to C++

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

Operator overloading

Lecture 14: more class, C++ streams

This section provides some reminders and some terminology with which you might not be familiar.

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Functions and Recursion

QUIZ. Source:

Consider the above code. This code compiles and runs, but has an error. Can you tell what the error is?

Ch. 10: Name Control

Week - 01 Lecture - 04 Downloading and installing Python

Chapter 7 Functions. Now consider a more advanced example:

CMSC162 Intro to Algorithmic Design II Blaheta. Lab March 2019

Principles of Programming Pointers, Dynamic Memory Allocation, Character Arrays, and Buffer Overruns

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

C++ For Science and Engineering Lecture 15

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

COSC 2P95. Introduction. Week 1. Brock University. Brock University (Week 1) Introduction 1 / 18

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

Software Design and Analysis for Engineers

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Anatomy of a Standard Transcript

Name: Username: I. 20. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015

Functions, Pointers, and the Basics of C++ Classes

Review of the C Programming Language

CSCI 171 Chapter Outlines

Programming in C and C++

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

Fast Introduction to Object Oriented Programming and C++

Software Design and Analysis for Engineers

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Recursively Enumerable Languages, Turing Machines, and Decidability

Introduction to C: Pointers

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

Cpt S 122 Data Structures. Introduction to C++ Part II

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Review of the C Programming Language for Principles of Operating Systems

G52CPP C++ Programming Lecture 9

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

Software Design and Analysis for Engineers

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Lecture 10: building large projects, beginning C++, C++ and structs

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 12: Classes and Data Abstraction

Sixth lecture; classes, objects, reference operator.

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

7.1 Optional Parameters

Pointers II. Class 31

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

Object-Oriented Programming

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

CSE 12 Abstract Syntax Trees

CHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object

Lab 12: Lijnenspel revisited

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

PIC 10A Objects/Classes

C++ for Java Programmers

The C++ Language. Arizona State University 1

Pointers and References

Comp 11 Lectures. Mike Shah. June 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures June 26, / 57

Starting to Program in C++ (Basics & I/O)

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Chapter 2. Procedural Programming

A brief introduction to C programming for Java programmers

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Lecture Notes on Memory Layout

Transcription:

COSC 2P95 Procedural Abstraction Week 3 Brock University Brock University (Week 3) Procedural Abstraction 1 / 26

Procedural Abstraction We ve already discussed how to arrange complex sets of actions (e.g. loops), but we haven t learned anything that would help us to develop clean and readable code. Most algorithms include steps that only require a cursory understanding at that level of discourse Of course, eventually, everything must be completely and unambiguously defined We achieve this via procedural abstraction: defining single actions that actually represent more complicated sequences of actions In some languages, we might call them methods. We won t be using that term yet. Brock University (Week 3) Procedural Abstraction 2 / 26

Procedures, and functions, and methods, oh my! Are we familiar with these terms? Do we know the difference between them? A procedure is a sequence of imperative instructions that can be collectively aliased (i.e. invoked) via a call to a single term or phrase A function is comparable to a procedure, but, upon completion, returns a value to the context that invoked it A method could be a function method, or a procedure method (often simply called function or method, respectively). What matters is that it s an action that s directly tied to an object/class Once we get to OO, methods will sometimes also be known as member functions Some schools of thought will dictate that procedures tied to objects should change state, while functions should ask questions of the objects In C (and C++, outside of methods), it s not unheard of to simply refer to functions. Brock University (Week 3) Procedural Abstraction 3 / 26

How do we start? A procedure is pretty simple to write. All you need is: [ m o d i f i e r ] ( r e t u r n t y p e ) name ( [ p a r a m e t e r s ] ) { s t a t e m e n t l i s t } e.g. v o i d wokka ( ) { i n t d e a l i e =8; cout<< G r o b b l e g r o b b l e g r o b b l e! <<e n d l ; } Why void? Because that s how you say you re not returning anything! Brock University (Week 3) Procedural Abstraction 4 / 26

Parameters Parameters allow you to modify the behaviour of an action. Parameters declared within the procedure header are also known as formal parameters; when you invoke the procedure, the arguments you provide are the actual parameters The actual parameters must match the formal parameters Of course, casting/coercion can help with this Unlike with some languages, you can t omit any, or provide them out of sequence Brock University (Week 3) Procedural Abstraction 5 / 26

Invoking procedures from other procedures Can we call one procedure from within another? Well, obviously we can, but there s something special we need to worry about that may not be a concern for some other languages We re going to need to try an example. Brock University (Week 3) Procedural Abstraction 6 / 26

Whu... what?!? There are two different kinds of compilers: one-pass, and two-pass. A two-pass compiler first goes through the source files, collecting tokens, and building up a table of references. It then does a second pass to connect the dots By comparison, a one-pass compiler only goes through each source file once, building up its tables as it goes. If it hasn t seen a term by the time it s needed, it simply doesn t exist So... what s the solution? Brock University (Week 3) Procedural Abstraction 7 / 26

Forward declarations Of course, more often than not, you could simply ensure that you declared every procedure before you needed it. But what would happen if you had two procedures, a and b, with a calling b, and b calling a? Clearly, we simply need to cut the cake before we serve it! We don t actually need both definitions simultaneously; we simply need to know their signatures The solution is a forward declaration All we need to do is to write the procedure s header, with a semicolon. We can provide the body later Let s revisit the example. Brock University (Week 3) Procedural Abstraction 8 / 26

Headers The only problem is that, for larger source files, starting with a couple dozen prototypes can be a bit inelegant. More importantly, once we get to using multiple files and linking, we won t want to have to copy all of them over to every connected file. We can write a header file, which is just a minimal source file that (if used appropriately) doesn t define any behaviours Primarily, we use them for function prototypes, but we can also define constants and types (once we get to that) We use a preprocessor directive (#include "filename.h") to include it Let s wait until the next slide for an example. Brock University (Week 3) Procedural Abstraction 9 / 26

Functions If a procedure has a void return type, then a function has... well, pretty much anything else We ve already defined what a function is. All it takes is to change the return type, and include a return statement Quick example time? Brock University (Week 3) Procedural Abstraction 10 / 26

return statements There are two odd points worth knowing about the return statement: If you neglect to include the return statement, even with a function that promised a particular return type, it ll still compile (and run) However, there s no legitimate reason to do this, and the program won t be as readable You can include a return statement, even for a procedure A common use for this is if you might want to preemptively leave a procedure, but don t want to indent the rest of the procedure for the else (Yes, it s example time again) Brock University (Week 3) Procedural Abstraction 11 / 26

Overloading functions If you want to define the same behaviour more than once, with the only difference being the types, then you can overload the function names Just declare and define more than one copy of the same name, with only the signatures varying Be very careful with these. Coercion can already match with different signatures; ambiguous calls may not compile at all (Are we tired of examples yet? Because... example time) Brock University (Week 3) Procedural Abstraction 12 / 26

Speaking of returns... You might sometimes need a function that can return more than one value. Tough Brock University (Week 3) Procedural Abstraction 13 / 26

No multiple returns? Unfortunately, C++ actually doesn t directly provide a mechanism for multiple returns. That said, we have multiple alternatives available to us (of varying levels of usefulness): Once we learn how to create records (e.g. struct), you could have a single return that can contain multiple values inside C++ provides a tuple in its standard library for effectively the same use In theory, you could allocate an array, and provide it as a parameter to the function The function would then modify the contents of the array to match the data to be returned Generally, this isn t going to be practical However, it s immensely common as a mechanism for providing a buffer (e.g. for receiving multiple bytes from an input stream) What if we could simply use a parameter as a return? Brock University (Week 3) Procedural Abstraction 14 / 26

Types of parameters For programming languages in general, there are different potential types of parameters: In Parameters for receiving arguments Out Parameters allows a procedure to return values directly through the parameter In/Out Parameters can be used either way Ada, for example, is very good for labelling these explicitly. But, how does this help us in C++? Brock University (Week 3) Procedural Abstraction 15 / 26

Values, pointers, and references First, let s briefly discuss the difference between pass-by-value and pass-by-reference. This is where we re particularly glad we re using C++ (even over C). A pointer is a special variable that contains an address as its stored value Pointers can be dereferenced to access (or modify) the value referenced by the pointer Other than still technically being a number (meaning you can perform arithmetic on it), pointers are mostly analogous to Java references C++ also provides actual references A reference allows two names (variables/parameters) to share the same place in memory Both pointers and references require accessing the address of the corresponding variable The two different techniques are best understood by demonstrating the difference. Brock University (Week 3) Procedural Abstraction 16 / 26

Usage Suggestions There s a school of thought that functions should only request values, and procedures should change state. A side effect occurs when a function changes the state of the system Side effects are sometimes discouraged for functions, if it can make it harder to predict state just from reading the source An idempotent function is one that always produces the same result when it receives the same parameters Idempotent functions can have side effects, so long as those side effects don t change the behaviour of successive calls e.g. adding a value to an array at a specific position On the other hand, a push into a stack is not Brock University (Week 3) Procedural Abstraction 17 / 26

Variadic functions Sometimes you may not know exactly how many parameters a function will have. Common examples include formatted print statements, or functions that select a single element from an arbitrary number of options (e.g. max). We re actually not going to get into this yet, because C++ introduced a better approach, but it relies on templates (which is a few weeks away) However, if you find yourself needing to do it in the meantime, you can always use C s vararg feature Brock University (Week 3) Procedural Abstraction 18 / 26

Constants We ve already talked about symbolic constants (#define) and const, but there s still one more term: constexpr A constexpr defines a compile-time constant Technically you can define constant values, but there s little reason to choose that over a const constexpr can also be used to define simple functions, so long as they can be fully-defined at compile-time You may need to explicitly tell the compiler to use the C++11 standard (or higher) e.g. g++ -std=c++11 -o out file.cpp Example time? Example time. Brock University (Week 3) Procedural Abstraction 19 / 26

Inline functions Recall how function invocation works: it requires allocating memory for the call frame, jumping to a different place in the code, etc. It s slower than simply having the equivalent instructions in-place. Actually, you can do that. Prepending the inline keyword before a function acts as a hint to the compiler that it may explicitly expand the the function, as though its lines had been written directly Typically, it ll run slightly faster, and take up slightly more space Brock University (Week 3) Procedural Abstraction 20 / 26

Scope revisited Now that we ve looked at functions and pointers, let s revisit scope and extent a bit. Let s review a sample (which is technically different from an example). Brock University (Week 3) Procedural Abstraction 21 / 26

Recursion Thankfully, there are pretty much no special considerations for writing recursive solutions in C/C++. However, we do remember what a recursive function is, and how it works, right? Because we ll need that later. Brock University (Week 3) Procedural Abstraction 22 / 26

Arrays We ll get to dynamic allocation and ragged arrays later, but there s no reason not to start using arrays. Allocate an array as such: int name[size]; You can pre-initialize them: int name[3]={val1,val2,val3}; You don t need to specify the size when pre-initializing Be careful relying on the sizeof function for array sizes You can do interesting things by casting one array type into another When declaring a variable-length automatic array as a parameter, you can only leave the number of rows blank 2D (or more) arrays simply require additional square brackets Note: Because of how arrays work, you can also reference them with a pointer of the base type. Up to you if we want to look at this together. Brock University (Week 3) Procedural Abstraction 23 / 26

Strings There are two different major string options available: C used a char array (or char pointer) C++ defines a class Both have their own string libraries. C++ strings are pretty good, but, on occasion, you ll be forced to use the older C-style. Brock University (Week 3) Procedural Abstraction 24 / 26

Command-line parameters Recall from the other week that the main function accepts two parameters: the number of command-line parameters, and a (C-style) string array containing the parameters The first (index 0) entry in the array will be the program name, including path Since they re strings, you ll need to do convert them if you want to accept numbers You can use the atoi (or atof, or atol) function provided by the cstdlib library Brock University (Week 3) Procedural Abstraction 25 / 26

Questions? Have we had enough examples? Because we can totally come up with more. Brock University (Week 3) Procedural Abstraction 26 / 26