CS 460 Programming Languages Fall 2010 Dr. Watts (Week 2) C Language Review How do your open a file? How do you dynamically allocate space? Single item Array of items How do you “pass by reference”? Input Output Swap function What is a “local function”? 1 – 6 assignment numbers C Review due Monday, 13 Sept, 11:59 pm – yourlastname.c 1 Useful C Books The C Programming Language Second Edition Brian W. Kernighan and Dennis M. Ritchie Prentice Hall, 1988 – ISBN 0-13-110362-8 C – A Reference Manual Fifth Edition Samuel P. Harbison III and Guy L. Steele Jr. Prentice Hall, 2002 – ISBN 0-13-089592-X History of Programming Languages 5+ generations of computing history Dates Signature Hardware People Companies Languages Milestones 2 Generation 0 Before 1940 Mechanical Abacus Slide Rule Analytic and Difference Engines Punch card Charles Babbage, Lady Ada Lovelace, Blaise Pascal, Herman Hollerith “Programming by Screwdriver” Dials, other mechanical devices Concepts of subprograms and loops Generation 1 1940’s Electro-mechanical Relays Vacuum tubes Alan Turing, Presper Eckert & John Mauchley, John V. Atanasoff, John VonNeuman, Konrad Zuse American Tabulating Company (later IBM) Machine Language Plankakül 3 Generation 1 (continued) Turing machine Enigma Code ENIAC Stored program design Generation 2 1950’s Transistors IBM, Univac Assembly Languages Short code Movement of computing from scientific laboratory to real world 4 Generation 3 1960’s – 1980’s Integrated Circuits Bill Gates, Steve Wozniak & Steve Jobs Microsoft, Wang, IBM, Apple High Level Language FORTRAN, COBOL, BASIC LISP, Smalltalk, Prolog Algol, Pascal Ada Generation 3 (continued) First Compiler First Interpreter Algol 60 Specification BNF – Bachus Naur Form (grammar spcification) Standardization Structured Programming Nested Control Structures Case Statement Single Pass Compilation Pascal 5 Generation 3 (continued) Data Structures Multi-processing Operating Systems Arrays, Lists Strings Records C / UNIX Multi-user Operating Systems (time sharing) Personal Computer Microprocessor BASIC Apple, IBM PC & Microsoft DOS Triumph of the Nerds: The Rise of Accidental Empires Generation 3 (continued) Dynamic Language APL, SNOBOL All-purpose Languages PL/I – all current data structures Ada – Department of Defense Software Engineering Before its time – slow compiler; slow development Object-Oriented Programming Smalltalk, Eiffel, C++, Java 6 Generation 4 1980’s – 2000’s VLSI – very large scale integrated circuits Distributed and Parallel Processing World Wide Web GUI Emergence – high powered graphics Special Purpose Languages Java, Visual BASIC, C# HTML, Java/Swing, Action Script Scripting Languages Java Script, Python, PHP, Ruby Generation 5 Still to come Post silicon Natural Languages AI 7 Implementation Importance of Standards Compilation vs interpretation vs hybrid Components of language translation Lexical analysis Syntactical analysis Semantic analysis Intermediate code generation Optimization Target code generation Portability Programming Environments Stand alone tools Integrated Development Environments IDEs Turbo Pascal (Borland) MS Visual C++ Code Warrior MS .net 8 Thoughts on Computer Programming Languages The use of COBOL cripples the mind; its teaching should, therefore, be regarded as a criminal offence. (Edsger Dijkstra) Consistently separating words by spaces became a general custom about the tenth century A.D., and lasted until about 1957, when FORTRAN abandoned the practice. (Sun FORTRAN Reference Manual) Cobol has almost no fervent enthusiasts. As a programming tool, it has roughly the sex appeal of a wrench. (Charles Petzold) C++ is the only current language making COBOL look good. (Bertrand Meyer) C++ has its place in the history of programming languages. Just as Caligula has his place in the history of the Roman Empire. (Robert Firth) Arguing that Java is better than C++ is like arguing that grasshoppers taste better than tree bark. (Thant Tessman) Java is, in many ways, C++--. (Michael Feldman) If Java had true garbage collection, most programs would delete themselves upon execution. (Robert Sewell) It is practically impossible to teach good programming style to students that have had prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration. (Edsger Dijkstra) In my egotistical opinion, most people's C programs should be indented six feet downward and covered with dirt. (Blair P. Houghton) C++ is history repeated as tragedy. Java is history repeated as farce. (Scott McKay) Unix and C are the ultimate computer viruses. (Richard P Gabriel) Subprograms – why? Why do we create subprograms? Reusability Modularity Readability Writeability Software Engineering Why are we studying subprograms (now)? Recursion – advanced sorting Scheme – a functional language 9 Subprogram components Subprogram definition Subprogram call Describes interface and actions of the subprogram Explicit request that subprogram be executed Complies to defined interface Subprogram header Indicates type of subprogram Indicates name of subprogram Optionally, specifies list of parameters Subprogram Header Examples FORTRAN Pascal Procedure adder (parameters); Function adder (parameters) : returnType; C++ SUBROUTINE ADDER (parameters) returnType adder (parameters); returnType adder (parameters) { } Python Def adder (parameters) : 10 Alternative Subprogram Definitions Function overloading Within #ifdef, #else, and #endif Nested subroutines Within Python if statement Other suprogram issues Prototypes vs definitions Subprogram vs method Method associated with object of a class Formal vs actual parameters Why prototypes? C, C++, Pascal Formal – in function definition Actual – in function call Parameter profile Number, order and types of formal parameters 11 More subprogram issues Procedure vs function Nested subprograms Procedure – no return value Function returns a value Scoping issues Local vs global variables Function names and return types Function names Return types Scalar Composite 12 Parameters Formal parameters in subprogram description Actual parameters in subprogram call Positional parameters Keyword parameters Variable number of parameters Default parameters Parameter Passing Types In mode, out mode, inout mode Pass-by-value Pass-by-result In mode then out mode – value passed then “returned” Pass-by-reference Out mode – value is “returned” Pass-by-value-result In mode – value is passed Inout mode – address is passed Pass-by-name Inout mode – actual name is substituted 13 Type checking parameters Compile time checking Type coalescing Passing arrays Passing base address Passing copy of array Passing multidimensional arrays 14 Parameter passing design considerations Efficiency One-way or two-way data transfer C++ vs C C++ or C : pass by value What will this do? void Swap (int a, int b) { int t = a; a = b; b = t; } int x, y; Swap (x, y); 15 C++ vs C C++ : pass by reference May “implicitly” pass and use address if two-way transfer is needed void Swap (int &a, int &b) { int t = a; a = b; b = t; } int x, y; Swap (x, y); C++ vs C C : only pass by value Must explicitly pass and use address if two-way transfer is needed void Swap (int * a, int * b) { int t = *a; *a = *b; *b = t; } int x, y; Swap (&x, &y); 16