1 CS 410 Mastery in Programming Chapter 4 Side Effects Herbert G. Mayer, PSU CS status 7/9/2011.

Slides:



Advertisements
Similar presentations
1) Scope a] Ada scope rules b] C scope rules 2) Parameter passing a] Ada parameter modes b) Parameter passing mechanisms COMP205 IMPERATIVE LANGUAGES 13.
Advertisements

Computational Models The exam. Models of computation. –The Turing machine. –The Von Neumann machine. –The calculus. –The predicate calculus. Turing.
1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha Control Flow-II: Execution Order.
1 CS 162 Introduction to Computer Science Chapter 7 Matrix Manipulation Herbert G. Mayer, PSU Status 9/21/2014.
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
PSUCS322 HM 1 Languages and Compiler Design II IR Code Generation I Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
Introduction to Programming Languages Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University.
Slide 1 Vitaly Shmatikov CS 345 Functions. slide 2 Reading Assignment uMitchell, Chapter 7 uC Reference Manual, Chapters 4 and 9.
Subprograms A subprogram allows process abstraction (as opposed to data abstraction). Characteristics –single entry point –caller suspended until control.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
ECE 103 Engineering Programming Chapter 11 One Minute Synopsis Herbert G. Mayer, PSU CS Status 7/1/2014.
1 CS 161 Introduction to Programming and Problem Solving Chapter 9 C++ Program Components Herbert G. Mayer, PSU Status 10/20/2014.
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
This set of slides is provided by the author of the textbook1 Introductory Topics l Computer Programming l Programming Life-Cycle Phases l Creating an.
1 Introduction to Computers and Programming Quick Review What is a Function? A module of code that performs a specific job.
PSU CS 106 Computing Fundamentals II VB Subprograms & Functions HM 4/29/2008.
1 CS 410 Mastery in Programming Chapter 2 Recursion Herbert G. Mayer, PSU CS status 7/3/2011.
ISBN Chapter 7 Expressions and Assignment Statements.
Overview Parameter passing modes.
Guide To UNIX Using Linux Third Edition
CSC321: Programming Languages1 Programming Languages Tucker and Noonan Chapter 9: Functions 9.1 Basic Terminology 9.2 Function Call and Return 9.3 Parameters.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Scope.
1 Chapter-01 Introduction to Computers and C++ Programming.
1 CS 162 Introduction to Computer Science Chapter 5 ASCII to Integer Conversion Herbert G. Mayer, PSU Status 11/9/2014.
Chapter TwelveModern Programming Languages1 Memory Locations For Variables.
Chapter 8 High-Level Programming Languages (modified by Erin Chambers)
1 CS 162 Introduction to Computer Science Chapter 4 Function Calls Herbert G. Mayer, PSU Status 11/9/2014.
CS 2104 Prog. Lang. Concepts Subprograms
LANGUAGE TRANSLATORS: WEEK 24 TRANSLATION TO ‘INTERMEDIATE’ CODE (overview) Labs this week: Tutorial Exercises on Code Generation.
Programming Languages –14 David Watt (Glasgow) Steven Wong (Singapore) Moodle : Computing Science → Level 3 → Programming Languages 3 © 2012 David.
Programming Languages and Design Lecture 7 Subroutines and Control Abstraction Instructor: Li Ma Department of Computer Science Texas Southern University,
CS 403 Programming Language Theory Class 2 - August 29, 2000.
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
1 CS Programming Languages Class 15 October 17, 2000.
1 Scope Scope describes the region where an identifier is known, and semantic rules for this.
Arithmetic Expressions
ECE 103 Engineering Programming Chapter 24 Sorting Herbert G. Mayer, PSU CS Status 6/2/2015 Initial content copied verbatim from ECE 103 material developed.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
Run-Time Storage Organization Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.
1 CS 201 Computer Systems Programming Chapter 18 “Parameter Passing” Herbert G. Mayer, PSU CS Status 1/23/2913.
1 CS 162 Introduction to Computer Science Chapter 6 C++ Reference Parameters Herbert G. Mayer, PSU Status 9/11/2014.
1 CS 163 Data Structures Chapter 12 Side Effects Herbert G. Mayer, PSU Status 5/11/2015.
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
Copyright © 2006 The McGraw-Hill Companies, Inc. Basic Terminology Value-returning functions: –known as “non-void functions/methods” in C/C++/Java –called.
Slide 1 Dr. Mohammad El-Ramly Fall 2010 Set 7- II - Functions Slides by Vitaly Shmatikov Cairo University Faculty of Computers and Information CS317 Concepts.
CS 161 Introduction to Programming and Problem Solving Chapter 18 Control Flow Through C++ Program Herbert G. Mayer, PSU Status 10/8/2014 Initial content.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Programming Language Descriptions. What drives PL Development? Computers are “in charge” of extremely important issues Execute a program literally. Exercise.
1 CS 163 Data Structures Chapter 6 Function Calls and Recursion Herbert G. Mayer, PSU Status 6/1/2015.
int k = Integer.MAX_VALUE; k++; int k = Integer.MAX_VALUE; k++; What happens when the following code executes? byte b = someFile.readByte(); b = (byte)(b.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
ISBN Chapter 10 Implementing Subprograms.
CS 161 Introduction to Programming and Problem Solving Chapter 12 C++ Statements Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
Programming Language Basics. What is a Programming Language? “A computer, human-created language used to write instructions for a computer.” “An artificial.
Functional Programming
CS 326 Programming Languages, Concepts and Implementation
CS 326 Programming Languages, Concepts and Implementation
Principles of programming languages 4: Parameter passing, Scope rules
User-Defined Functions
CS 201 System Programming Chapter 18 C++ Reference Parameters
Closure Closure binds a first-class function and a lexical environment together This is a complex topic, so we will build up our understanding of it we.
Programming Language Basics
Overview of Programming Paradigms
ECE 103 Engineering Programming Chapter 12 More C Statements
Programming Languages and Paradigms
Programming Languages, Preliminaries, History & Evolution
Presentation transcript:

1 CS 410 Mastery in Programming Chapter 4 Side Effects Herbert G. Mayer, PSU CS status 7/9/2011

2 Syllabus Side Effects Side Effects Language and Side Effect Language and Side Effect Sample Side Effect Sample Side Effect References References

3 Side Effects Side Effects in an executing program are: Actions performed by a function Changing the program state In a way that is generally surprising Due to their unnatural place, or strange time! As a consequence, side effects are hard to track, their impact difficult to account for, and programs generating them are hard to maintain What Side Effects are NOT! These changes are not caused by agents other than the programmer You/the programmer put them there for a reason Side effects are not bad per se!

4 Side Effects Why are they practiced? For “convenience” lumped together with the action proper of a function For efficiency, to avoid invoking another, separate function Fair to say: They are short-cuts How to avoid Side Effects? Assuming the impact of the Side Effect is needed Separate it out in an imperative language, thus simulating the behaviour/constraint of a functional language Why avoid Side Effects? A program written with Side Effects, no matter how well designed, is more difficult to read --harder to maintain– than a program without Side Effects But watch out for ‘snake-oil” propaganda : The work of the Side Effect has to be accomplished somehow

5 Language and Side Effect Imperative languages (e.g. C++) generally encourage side effects Underlying compute model is Turing Machine Side Effect means: a change in machine/program state Enabled by references to, and changes of, global objects Can change globals directly, or indirectly via reference parameters or pointers Can change files, state, condition codes, subtle state Other languages prone to side-effects: Fortran, PL/I, Pascal, Ada, C

6 Language and Side Effect Can be worse in languages other than C++ More complex languages with nested procedural scope offer even more chances for side effects E.g. PL/I, Ada, Pascal, Modula-2, Algol-60, Algol68, … Functional languages Underlying compute model generally is Lambda Calculus Eliminates/reduces state change of program/machine E.g. Haskell, ML, Prolog, …Caveat Watch out for PR! The language proponents generally have an agenda, maybe even a noble one. But watch out Functional language are not inherently better, just because they reduced the chances for side-effects Imperative languages with nested procedural scope are not inherently better, just because they allow better data sharing

7 Sample Side Effect #include #include #define MAX 10// arbitrary array bound int global_i = 5;// arbitrary number within array bounds int arr[ ] = { 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 }; int one()// bad function with side-effect { // one global_i++; return 1; } //end one void print() { // print for ( int i = 0; i < MAX; i++ ) { printf( "arr[%d] = %2d\n", i, arr[ i ] ); } //end for } //end print int main() { // main arr[ one() + global_i ] = arr[ one() + global_i ]; print(); } //end main

8 Sample Side Effect, Output arr[0] = 0 arr[1] = 1 arr[2] = 4 arr[3] = 9 arr[4] = 16 arr[5] = 25 arr[6] = 36 arr[7] = 64  element arr[ 7 ] overridden with arr[ 8 ] arr[8] = 64 arr[9] = 81

9 Sample Side Effect, Output arr[ one() + global_i ] = arr[ one() + global_i ] = arr[ one() + global_i ]; For the following assignment, what is the output:

10 Sample Side Effect, Output arr[0] = 0 arr[1] = 1 arr[2] = 4 arr[3] = 9 arr[4] = 16 arr[5] = 25 arr[6] = 36 arr[7] = 81  element arr[ 7 ] overridden with arr[ 9 ] arr[8] = 81  element arr[ 8 ] overridden with arr[ 9 ] arr[9] = 81

11 Sample Side Effect Was the correct element overridden? Was the right element referenced? Which language rule should apply? Must left-hand side of assignment operator = be evaluated first? Or the right-hand side? Should the function main() be allowed to change global_i or any other global? Did the programmer do something wrong?

12 References  Side effect: cience)  Functional language: nguages_by_category#Functional_languages  Turing Machine:  Functional vs Imperative: us/library/bb aspx