Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Computer program
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Programming paradigms and languages== [[Programming language]] features exist to provide building blocks to be combined to express programming ideals.<ref name="stroustrup-ch1-10">{{cite book | last = Stroustrup | first = Bjarne | title = The C++ Programming Language, Fourth Edition | publisher = Addison-Wesley | year = 2013 | page = 10 | isbn = 978-0-321-56384-2 }}</ref> Ideally, a programming language should:<ref name="stroustrup-ch1-10"/> * express ideas directly in the code. * express independent ideas independently. * express relationships among ideas directly in the code. * combine ideas freely. * combine ideas only where combinations make sense. * express simple ideas simply. The [[programming style]] of a programming language to provide these building blocks may be categorized into [[programming paradigm]]s.<ref name="stroustrup-ch1-11">{{cite book | last = Stroustrup | first = Bjarne | title = The C++ Programming Language, Fourth Edition | publisher = Addison-Wesley | year = 2013 | page = 11 | isbn = 978-0-321-56384-2 }}</ref> For example, different paradigms may differentiate:<ref name="stroustrup-ch1-11"/> * [[Procedural programming|procedural languages]], [[functional language]]s, and [[Logic programming|logical languages]]. * different levels of [[data abstraction]]. * different levels of [[class hierarchy]]. * different levels of input [[datatypes]], as in [[Container (abstract data type)|container types]] and [[generic programming]]. Each of these programming styles has contributed to the synthesis of different ''programming languages''.<ref name="stroustrup-ch1-11"/> A ''programming language'' is a set of [[Reserved word|keywords]], [[Character (computing)|symbols]], [[Identifier (computer languages)|identifiers]], and rules by which programmers can communicate instructions to the computer.<ref name="pis-ch4-p159">{{cite book | last = Stair | first = Ralph M. | title = Principles of Information Systems, Sixth Edition | publisher = Thomson | year = 2003 | page = 159 | isbn = 0-619-06489-7 }}</ref> They follow a set of rules called a [[Syntax (programming languages)|syntax]].<ref name="pis-ch4-p159"/> * ''Keywords'' are reserved words to form [[Declaration (computer programming)|declarations]] and [[Statement (computer science)|statements]]. * ''Symbols'' are characters to form [[Operation (mathematics)|operations]], [[Assignment (computer science)|assignments]], [[control flow]], and [[delimiter]]s. * ''Identifiers'' are words created by programmers to form [[Constant (computer programming)|constants]], [[Variable (computer science)|variable names]], [[Record (computer science)|structure names]], and [[Function (computer programming)|function names]]. * ''Syntax Rules'' are defined in the [[Backus–Naur form]]. ''Programming languages'' get their basis from [[formal language]]s.<ref name="fla-ch1-p2">{{cite book | last = Linz | first = Peter | title = An Introduction to Formal Languages and Automata | publisher = D. C. Heath and Company | year = 1990 | page = 2 | isbn = 978-0-669-17342-0 }}</ref> The purpose of defining a solution in terms of its ''formal language'' is to generate an [[algorithm]] to solve the underlining problem.<ref name="fla-ch1-p2"/> An ''algorithm'' is a sequence of simple instructions that solve a problem.<ref name="dsa-ch2-p29">{{cite book | last = Weiss | first = Mark Allen | title = Data Structures and Algorithm Analysis in C++ | publisher = Benjamin/Cummings Publishing Company, Inc. | year = 1994 | page = 29 | isbn = 0-8053-5443-3 }}</ref> ===Generations of programming language=== {{main|Programming language generations}} [[File:W65C816S Machine Code Monitor.jpeg|thumb|[[Machine language]] monitor on a [[W65C816S]] [[microprocessor]]]] The evolution of programming languages began when the [[EDSAC]] (1949) used the first [[Stored-program computer|stored computer program]] in its [[von Neumann architecture]].<ref name="sco-ch1-p17">{{cite book | last = Tanenbaum | first = Andrew S. | title = Structured Computer Organization, Third Edition | publisher = Prentice Hall | year = 1990 | page = [https://archive.org/details/structuredcomput00tane/page/17 17] | isbn = 978-0-13-854662-5 | url = https://archive.org/details/structuredcomput00tane/page/17 }}</ref> Programming the EDSAC was in the first [[Programming language generations|generation of programming language]].<ref>{{Citation |last1=Wilkes |first1=M. V. |title=The EDSAC |date=1982 |work=The Origins of Digital Computers: Selected Papers |pages=417–421 |editor-last=Randell |editor-first=Brian |url=https://link.springer.com/chapter/10.1007/978-3-642-61812-3_34 |access-date=2025-04-25 |place=Berlin, Heidelberg |publisher=Springer |language=en |doi=10.1007/978-3-642-61812-3_34 |isbn=978-3-642-61812-3 |last2=Renwick |first2=W.|url-access=subscription }}</ref> * The [[First-generation programming language|first generation of programming language]] is [[machine language]].<ref name="pis-ch4-p160">{{cite book | last = Stair | first = Ralph M. | title = Principles of Information Systems, Sixth Edition | publisher = Thomson | year = 2003 | page = 160 | isbn = 0-619-06489-7 }}</ref> ''Machine language'' requires the programmer to enter instructions using ''instruction numbers'' called [[machine code]]. For example, the ADD operation on the [[PDP-11]] has instruction number 24576.<ref name="sco-ch7-p399">{{cite book | last = Tanenbaum | first = Andrew S. | title = Structured Computer Organization, Third Edition | publisher = Prentice Hall | year = 1990 | page = [https://archive.org/details/structuredcomput00tane/page/399 399] | isbn = 978-0-13-854662-5 | url = https://archive.org/details/structuredcomput00tane/page/399 }}</ref> * The [[Second-generation programming language|second generation of programming language]] is [[assembly language]].<ref name="pis-ch4-p160"/> ''Assembly language'' allows the programmer to use [[Assembly language#Mnemonics|mnemonic]] [[Instruction_set_architecture#Instructions|instructions]] instead of remembering instruction numbers. An [[Assembler (computing)|assembler]] translates each assembly language mnemonic into its machine language number. For example, on the PDP-11, the operation 24576 can be referenced as ADD in the source code.<ref name="sco-ch7-p399"/> The four basic arithmetic operations have assembly instructions like ADD, SUB, MUL, and DIV.<ref name="sco-ch7-p399"/> Computers also have instructions like DW (Define [[Word (computer architecture)|Word]]) to reserve [[Random-access memory|memory]] cells. Then the MOV instruction can copy [[integer]]s between [[Processor register|registers]] and memory. :* The basic structure of an assembly language statement is a label, [[Operation (mathematics)|operation]], [[operand]], and comment.<ref name="sco-ch7-p400">{{cite book | last = Tanenbaum | first = Andrew S. | title = Structured Computer Organization, Third Edition | publisher = Prentice Hall | year = 1990 | page = [https://archive.org/details/structuredcomput00tane/page/400 400] | isbn = 978-0-13-854662-5 | url = https://archive.org/details/structuredcomput00tane/page/400 }}</ref> ::* ''Labels'' allow the programmer to work with [[Variable (computer science)|variable names]]. The assembler will later translate labels into physical [[memory address]]es. ::* ''Operations'' allow the programmer to work with mnemonics. The assembler will later translate mnemonics into instruction numbers. ::* ''Operands'' tell the assembler which data the operation will process. ::* ''Comments'' allow the programmer to articulate a narrative because the instructions alone are vague. :: The key characteristic of an assembly language program is it forms a one-to-one mapping to its corresponding machine language target.<ref name="sco-ch7-p398">{{cite book | last = Tanenbaum | first = Andrew S. | title = Structured Computer Organization, Third Edition | publisher = Prentice Hall | year = 1990 | page = [https://archive.org/details/structuredcomput00tane/page/398 398] | isbn = 978-0-13-854662-5 | url = https://archive.org/details/structuredcomput00tane/page/398 }}</ref> * The [[Third-generation programming language|third generation of programming language]] uses [[compiler]]s and [[Interpreter (computing)|interpreters]] to execute computer programs. The distinguishing feature of a ''third generation'' language is its independence from particular hardware.<ref name="cpl_3rd-ch2-26">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 26 | isbn = 0-201-71012-9 }}</ref> Early languages include [[Fortran]] (1958), [[COBOL]] (1959), [[ALGOL]] (1960), and [[BASIC]] (1964).<ref name="pis-ch4-p160"/> In 1973, the [[C programming language]] emerged as a [[high-level language]] that produced efficient machine language instructions.<ref name="cpl_3rd-ch2-37">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 37 | isbn = 0-201-71012-9 }}</ref> Whereas ''third-generation'' languages historically generated many machine instructions for each statement,<ref name="pis-ch4-p160_quote1">{{cite book | last = Stair | first = Ralph M. | title = Principles of Information Systems, Sixth Edition | publisher = Thomson | year = 2003 | page = 160 | isbn = 0-619-06489-7 | quote = With third-generation and higher-level programming languages, each statement in the language translates into several instructions in machine language. }}</ref> C has statements that may generate a single machine instruction.{{efn|[[Operators in C and C++|Operators]] like <code>x++</code> will usually compile to a single instruction.}} Moreover, an [[optimizing compiler]] might overrule the programmer and produce fewer machine instructions than statements. Today, an entire [[programming paradigm|paradigm]] of languages fill the [[imperative programming|imperative]], ''third generation'' spectrum. * The [[Fourth-generation programming language|fourth generation of programming language]] emphasizes what output results are desired, rather than how programming statements should be constructed.<ref name="pis-ch4-p160"/> [[Declarative language]]s attempt to limit [[Side effect (computer science)|side effects]] and allow programmers to write code with relatively few errors.<ref name="pis-ch4-p160"/> One popular ''fourth generation'' language is called [[Structured Query Language]] (SQL).<ref name="pis-ch4-p160"/> [[Database]] developers no longer need to process each database record one at a time. Also, a simple [[Select (SQL)|select statement]] can generate output records without having to understand how they are retrieved. ===Imperative languages=== {{main|Imperative programming}} [[File:Object-Oriented-Programming-Methods-And-Classes-with-Inheritance.png|thumb|A computer program written in an imperative language]] ''Imperative languages'' specify a sequential [[algorithm#Computer algorithm|algorithm]] using [[Declaration (computer programming)|declarations]], [[Expression (computer science)|expressions]], and [[Statement (computer science)|statements]]:<ref name="cpl-ch4-75">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Second Edition | publisher = Addison-Wesley | year = 1993 | page = 75 | isbn = 978-0-201-56885-1 }}</ref> * A ''declaration'' introduces a [[variable (programming)|variable]] name to the ''computer program'' and assigns it to a [[datatype]]<ref name="stroustrup-ch2-40">{{cite book | last = Stroustrup | first = Bjarne | title = The C++ Programming Language, Fourth Edition | publisher = Addison-Wesley | year = 2013 | page = 40 | isbn = 978-0-321-56384-2 }}</ref> – for example: <code>var x: integer;</code> * An ''expression'' yields a value – for example: <code>2 + 2</code> yields 4 * A ''statement'' might [[Assignment (computer science)|assign]] an expression to a variable or use the value of a variable to alter the program's [[control flow]] – for example: <code>x := 2 + 2; [[Conditional_(computer_programming)#If–then(–else)|if]] x = 4 then do_something();</code> ====Fortran==== [[FORTRAN]] (1958) was unveiled as "The IBM Mathematical FORmula TRANslating system". It was designed for scientific calculations, without [[String (computer science)|string]] handling facilities. Along with [[Declaration (computer programming)|declarations]], [[Expression (computer science)|expressions]], and [[Statement (computer science)|statements]], it supported: * [[Array data structure|arrays]]. * [[Function (computer programming)#Jump to subroutine|subroutines]]. * [[For loop#1957: FORTRAN|"do" loops]]. It succeeded because: * programming and debugging costs were below computer running costs. * it was supported by IBM. * applications at the time were scientific.<ref name="cpl_3rd-ch2-16">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 16 | isbn = 0-201-71012-9 }}</ref> However, non-IBM vendors also wrote Fortran compilers, but with a syntax that would likely fail IBM's compiler.<ref name="cpl_3rd-ch2-16"/> The [[American National Standards Institute]] (ANSI) developed the first Fortran standard in 1966. In 1978, Fortran 77 became the standard until 1991. Fortran 90 supports: * [[Record (computer science)|records]]. * [[Pointer (computer programming)|pointers]] to arrays. ====COBOL==== [[COBOL]] (1959) stands for "COmmon Business Oriented Language". Fortran manipulated symbols. It was soon realized that symbols did not need to be numbers, so [[String (computer science)|strings]] were introduced.<ref name="cpl_3rd-ch2-24">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 24 | isbn = 0-201-71012-9 }}</ref> The [[US Department of Defense]] influenced COBOL's development, with [[Grace Hopper]] being a major contributor. The statements were English-like and verbose. The goal was to design a language so managers could read the programs. However, the lack of structured statements hindered this goal.<ref name="cpl_3rd-ch2-25">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 25 | isbn = 0-201-71012-9 }}</ref> COBOL's development was tightly controlled, so dialects did not emerge to require ANSI standards. As a consequence, it was not changed for 15 years until 1974. The 1990s version did make consequential changes, like [[object-oriented programming]].<ref name="cpl_3rd-ch2-25"/> ====Algol==== [[ALGOL]] (1960) stands for "ALGOrithmic Language". It had a profound influence on programming language design.<ref name="cpl_3rd-ch2-19">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 19 | isbn = 0-201-71012-9 }}</ref> Emerging from a committee of European and American programming language experts, it used standard mathematical notation and had a readable, structured design. Algol was first to define its [[Syntax (programming languages)|syntax]] using the [[Backus–Naur form]].<ref name="cpl_3rd-ch2-19"/> This led to [[Syntax-directed translation|syntax-directed]] compilers. It added features like: * [[Block (programming)|block structure]], where variables were local to their block. * arrays with variable bounds. * [[For loop|"for" loops]]. * [[Function (computer programming)|functions]]. * [[Recursion (computer science)|recursion]].<ref name="cpl_3rd-ch2-19"/> Algol's direct descendants include [[Pascal (programming language)|Pascal]], [[Modula-2]], [[Ada (programming language)|Ada]], [[Delphi (software)|Delphi]] and [[Oberon (programming language)|Oberon]] on one branch. On another branch the descendants include [[C (programming language)|C]], [[C++]] and [[Java (programming language)|Java]].<ref name="cpl_3rd-ch2-19"/> ====Basic==== [[BASIC]] (1964) stands for "Beginner's All-Purpose Symbolic Instruction Code". It was developed at [[Dartmouth College]] for all of their students to learn.<ref name="cpl_3rd-ch2-30"/> If a student did not go on to a more powerful language, the student would still remember Basic.<ref name="cpl_3rd-ch2-30"/> A Basic interpreter was installed in the [[microcomputers]] manufactured in the late 1970s. As the microcomputer industry grew, so did the language.<ref name="cpl_3rd-ch2-30"/> Basic pioneered the [[Read–eval–print loop|interactive session]].<ref name="cpl_3rd-ch2-30"/> It offered [[operating system]] commands within its environment: * The 'new' command created an empty slate. * Statements evaluated immediately. * Statements could be programmed by preceding them with line numbers.{{efn|The line numbers were typically incremented by 10 to leave room if additional statements were added later.}} * The 'list' command displayed the program. * The 'run' command executed the program. However, the Basic syntax was too simple for large programs.<ref name="cpl_3rd-ch2-30"/> Recent dialects added structure and object-oriented extensions. [[Microsoft]]'s [[Visual Basic]] is still widely used and produces a [[graphical user interface]].<ref name="cpl_3rd-ch2-31"/> ====C==== [[C programming language]] (1973) got its name because the language [[BCPL]] was replaced with [[B (programming language)|B]], and [[AT&T Bell Labs]] called the next version "C". Its purpose was to write the [[UNIX]] [[operating system]].<ref name="cpl_3rd-ch2-37"/> C is a relatively small language, making it easy to write compilers. Its growth mirrored the hardware growth in the 1980s.<ref name="cpl_3rd-ch2-37"/> Its growth also was because it has the facilities of [[assembly language]], but uses a [[High-level programming language|high-level syntax]]. It added advanced features like: * [[inline assembler]]. * arithmetic on pointers. * pointers to functions. * bit operations. * freely combining complex [[Operators in C and C++|operators]].<ref name="cpl_3rd-ch2-37"/> [[File:Computer-memory-map.png|thumb|right|Computer memory map]] ''C'' allows the programmer to control which region of memory data is to be stored. [[Global variable]]s and [[static variable]]s require the fewest [[clock cycle]]s to store. The [[call stack|stack]] is automatically used for the standard variable [[Declaration (computer programming)|declarations]]. [[Manual memory management|Heap]] memory is returned to a [[pointer variable]] from the [[C dynamic memory allocation|<code>malloc()</code>]] function. * The ''global and static data'' region is located just above the ''program'' region. (The program region is technically called the ''text'' region. It is where machine instructions are stored.) :* The global and static data region is technically two regions.<ref name="geeksforgeeks">{{cite web | url = https://www.geeksforgeeks.org/memory-layout-of-c-program/ | title = Memory Layout of C Programs | date = 12 September 2011 | access-date = 6 November 2021 | archive-date = 6 November 2021 | archive-url = https://web.archive.org/web/20211106175644/https://www.geeksforgeeks.org/memory-layout-of-c-program/ | url-status = live }}</ref> One region is called the ''initialized [[data segment]]'', where variables declared with default values are stored. The other region is called the ''[[.bss|block started by segment]]'', where variables declared without default values are stored. :* Variables stored in the ''global and static data'' region have their [[Memory address|addresses]] set at compile time. They retain their values throughout the life of the process. :* The global and static region stores the ''global variables'' that are declared on top of (outside) the <code>main()</code> function.<ref name="cpl-ch1-p31">{{cite book |title=The C Programming Language Second Edition |last1=Kernighan |first1=Brian W. |last2=Ritchie |first2=Dennis M. |publisher=Prentice Hall |year=1988 |isbn=0-13-110362-8 |page=31}}</ref> Global variables are visible to <code>main()</code> and every other function in the source code. : On the other hand, variable declarations inside of <code>main()</code>, other functions, or within <code>{</code> <code>}</code> [[Block (programming)|block delimiters]] are ''local variables''. Local variables also include ''[[formal parameter]] variables''. Parameter variables are enclosed within the parenthesis of a function definition.<ref name="cpl_3rd-ch6-128">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 128 | isbn = 0-201-71012-9 }}</ref> Parameters provide an [[Interface (computing)|interface]] to the function. :* ''Local variables'' declared using the <code>static</code> prefix are also stored in the ''global and static data'' region.<ref name="geeksforgeeks"/> Unlike global variables, static variables are only visible within the function or block. Static variables always retain their value. An example usage would be the function <code>int increment_counter(){static int counter = 0; counter++; return counter;}</code>{{efn|This function could be written more concisely as <code>int increment_counter(){ static int counter; return ++counter;}</code>. 1) Static variables are automatically initialized to zero. 2) <code>++counter</code> is a prefix [[increment operator]].}} * The [[call stack|stack]] region is a contiguous block of memory located near the top memory address.<ref name="lpi-ch6-p121">{{cite book |title=The Linux Programming Interface |last=Kerrisk |first=Michael |publisher=No Starch Press |year=2010 |isbn=978-1-59327-220-3 |page=121}}</ref> Variables placed in the stack are populated from top to bottom.{{efn|This is despite the metaphor of a ''stack,'' which normally grows from bottom to top.}}<ref name="lpi-ch6-p121"/> A [[Call stack#STACK-POINTER|stack pointer]] is a special-purpose [[processor register|register]] that keeps track of the last memory address populated.<ref name="lpi-ch6-p121"/> Variables are placed into the stack via the ''assembly language'' PUSH instruction. Therefore, the addresses of these variables are set during [[Runtime (program lifecycle phase)|runtime]]. The method for stack variables to lose their [[Scope (computer science)|scope]] is via the POP instruction. :* ''Local variables'' declared without the <code>static</code> prefix, including formal parameter variables,<ref name="lpi-ch6-p122">{{cite book |title=The Linux Programming Interface |last=Kerrisk |first=Michael |publisher=No Starch Press |year=2010 |isbn=978-1-59327-220-3 |page=122}}</ref> are called ''automatic variables''<ref name="cpl-ch1-p31"/> and are stored in the stack.<ref name="geeksforgeeks"/> They are visible inside the function or block and lose their scope upon exiting the function or block. * The [[Manual memory management|heap]] region is located below the stack.<ref name="geeksforgeeks"/> It is populated from the bottom to the top. The [[operating system]] manages the heap using a ''heap pointer'' and a list of allocated memory blocks.<ref name="cpl-ch1-p185">{{cite book |title=The C Programming Language Second Edition |last1=Kernighan |first1=Brian W. |last2=Ritchie |first2=Dennis M. |publisher=Prentice Hall |year=1988 |isbn=0-13-110362-8 |page=185}}</ref> Like the stack, the addresses of heap variables are set during runtime. An [[out of memory]] error occurs when the heap pointer and the stack pointer meet. :* ''C'' provides the <code>malloc()</code> library function to [[C dynamic memory allocation|allocate]] heap memory.{{efn|''C'' also provides the <code>calloc()</code> function to allocate heap memory. It provides two additional services: 1) It allows the programmer to create an [[Array (data structure)|array]] of arbitrary size. 2) It sets each [[Memory cell (computing)|memory cell]] to zero.}}<ref name="cpl-ch8-p187">{{cite book |title=The C Programming Language Second Edition |last1=Kernighan |first1=Brian W. |last2=Ritchie |first2=Dennis M. |publisher=Prentice Hall |year=1988 |isbn=0-13-110362-8 |page=187}}</ref> Populating the heap with data is an additional copy function.{{efn|For [[String (computer science)|string]] variables, ''C'' provides the <code>strdup()</code> function. It executes both the allocation function and the copy function.}} Variables stored in the heap are economically passed to functions using pointers. Without pointers, the entire block of data would have to be passed to the function via the stack. ====C++==== In the 1970s, [[software engineers]] needed language support to break large projects down into [[Modular programming|modules]].<ref name="cpl_3rd-ch2-38">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 38 | isbn = 0-201-71012-9 }}</ref> One obvious feature was to decompose large projects ''physically'' into separate [[computer file|files]]. A less obvious feature was to decompose large projects ''logically'' into [[abstract data type]]s.<ref name="cpl_3rd-ch2-38"/> At the time, languages supported [[Type system|concrete (scalar)]] datatypes like [[integer]] numbers, [[floating-point]] numbers, and [[String (computer science)|strings]] of [[Character (computing)|characters]]. Abstract datatypes are [[Record (computer science)|structures]] of concrete datatypes, with a new name assigned. For example, a [[List (abstract data type)|list]] of integers could be called <code>integer_list</code>. In object-oriented jargon, abstract datatypes are called [[Class (computer programming)|classes]]. However, a ''class'' is only a definition; no memory is allocated. When memory is allocated to a class and [[Name binding|bound]] to an [[identifier]], it is called an [[Object (computer science)|object]].<ref name="cpl_3rd-ch8-193">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 193 | isbn = 0-201-71012-9 }}</ref> [[Object-oriented programming|Object-oriented imperative languages]] developed by combining the need for classes and the need for safe [[functional programming]].<ref name="cpl_3rd-ch2-39">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 39 | isbn = 0-201-71012-9 }}</ref> A function, in an object-oriented language, is assigned to a class. An assigned function is then referred to as a [[Method (computer programming)|method]], [[member function]], or [[Operation (mathematics)|operation]]. ''Object-oriented programming'' is executing ''operations'' on ''objects''.<ref name="cpl_3rd-ch2-35">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 35 | isbn = 0-201-71012-9 }}</ref> ''Object-oriented languages'' support a syntax to model [[subset|subset/superset]] relationships. In [[set theory]], an [[Element (mathematics)|element]] of a subset inherits all the attributes contained in the superset. For example, a student is a person. Therefore, the set of students is a subset of the set of persons. As a result, students inherit all the attributes common to all persons. Additionally, students have unique attributes that other people do not have. ''Object-oriented languages'' model ''subset/superset'' relationships using [[Inheritance (object-oriented programming)|inheritance]].<ref name="cpl_3rd-ch8-192">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 192 | isbn = 0-201-71012-9 }}</ref> ''Object-oriented programming'' became the dominant language paradigm by the late 1990s.<ref name="cpl_3rd-ch2-38"/> [[C++]] (1985) was originally called "C with Classes".<ref name="stroustrup-notes-22">{{cite book | last = Stroustrup | first = Bjarne | title = The C++ Programming Language, Fourth Edition | publisher = Addison-Wesley | year = 2013 | page = 22 | isbn = 978-0-321-56384-2 }}</ref> It was designed to expand [[C (programming language)|C's]] capabilities by adding the object-oriented facilities of the language [[Simula]].<ref name="stroustrup-notes-21">{{cite book | last = Stroustrup | first = Bjarne | title = The C++ Programming Language, Fourth Edition | publisher = Addison-Wesley | year = 2013 | page = 21 | isbn = 978-0-321-56384-2 }}</ref> An object-oriented module is composed of two files. The definitions file is called the [[header file]]. Here is a C++ ''header file'' for the ''GRADE class'' in a simple school application: <syntaxhighlight lang="cpp"> // grade.h // ------- // Used to allow multiple source files to include // this header file without duplication errors. // ---------------------------------------------- #ifndef GRADE_H #define GRADE_H class GRADE { public: // This is the constructor operation. // ---------------------------------- GRADE ( const char letter ); // This is a class variable. // ------------------------- char letter; // This is a member operation. // --------------------------- int grade_numeric( const char letter ); // This is a class variable. // ------------------------- int numeric; }; #endif </syntaxhighlight> A [[Constructor (object-oriented programming)|constructor]] operation is a function with the same name as the class name.<ref name="stroustrup-ch2-49">{{cite book | last = Stroustrup | first = Bjarne | title = The C++ Programming Language, Fourth Edition | publisher = Addison-Wesley | year = 2013 | page = 49 | isbn = 978-0-321-56384-2 }}</ref> It is executed when the calling operation executes the <code>[[new and delete (C++)|new]]</code> statement. A module's other file is the [[source file]]. Here is a C++ source file for the ''GRADE class'' in a simple school application: <syntaxhighlight lang="cpp"> // grade.cpp // --------- #include "grade.h" GRADE::GRADE( const char letter ) { // Reference the object using the keyword 'this'. // ---------------------------------------------- this->letter = letter; // This is Temporal Cohesion // ------------------------- this->numeric = grade_numeric( letter ); } int GRADE::grade_numeric( const char letter ) { if ( ( letter == 'A' || letter == 'a' ) ) return 4; else if ( ( letter == 'B' || letter == 'b' ) ) return 3; else if ( ( letter == 'C' || letter == 'c' ) ) return 2; else if ( ( letter == 'D' || letter == 'd' ) ) return 1; else if ( ( letter == 'F' || letter == 'f' ) ) return 0; else return -1; } </syntaxhighlight> Here is a C++ ''header file'' for the ''PERSON class'' in a simple school application: <syntaxhighlight lang="cpp"> // person.h // -------- #ifndef PERSON_H #define PERSON_H class PERSON { public: PERSON ( const char *name ); const char *name; }; #endif </syntaxhighlight> Here is a C++ ''source file'' for the ''PERSON class'' in a simple school application: <syntaxhighlight lang="cpp"> // person.cpp // ---------- #include "person.h" PERSON::PERSON ( const char *name ) { this->name = name; } </syntaxhighlight> Here is a C++ ''header file'' for the ''STUDENT class'' in a simple school application: <syntaxhighlight lang="cpp"> // student.h // --------- #ifndef STUDENT_H #define STUDENT_H #include "person.h" #include "grade.h" // A STUDENT is a subset of PERSON. // -------------------------------- class STUDENT : public PERSON{ public: STUDENT ( const char *name ); GRADE *grade; }; #endif </syntaxhighlight> Here is a C++ ''source file'' for the ''STUDENT class'' in a simple school application: <syntaxhighlight lang="cpp"> // student.cpp // ----------- #include "student.h" #include "person.h" STUDENT::STUDENT ( const char *name ): // Execute the constructor of the PERSON superclass. // ------------------------------------------------- PERSON( name ) { // Nothing else to do. // ------------------- } </syntaxhighlight> Here is a driver program for demonstration: <syntaxhighlight lang="cpp"> // student_dvr.cpp // --------------- #include <iostream> #include "student.h" int main( void ) { STUDENT *student = new STUDENT( "The Student" ); student->grade = new GRADE( 'a' ); std::cout // Notice student inherits PERSON's name << student->name << ": Numeric grade = " << student->grade->numeric << "\n"; return 0; } </syntaxhighlight> Here is a [[makefile]] to compile everything: <syntaxhighlight lang="make"> # makefile # -------- all: student_dvr clean: rm student_dvr *.o student_dvr: student_dvr.cpp grade.o student.o person.o c++ student_dvr.cpp grade.o student.o person.o -o student_dvr grade.o: grade.cpp grade.h c++ -c grade.cpp student.o: student.cpp student.h c++ -c student.cpp person.o: person.cpp person.h c++ -c person.cpp </syntaxhighlight> ===Declarative languages=== {{main|Declarative programming}} ''Imperative languages'' have one major criticism: assigning an expression to a ''non-local'' variable may produce an unintended [[Side effect (computer science)|side effect]].<ref name="cpl_3rd-ch9-218">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 218 | isbn = 0-201-71012-9 }}</ref> [[Declarative language]]s generally omit the assignment statement and the control flow. They describe ''what'' computation should be performed and not ''how'' to compute it. Two broad categories of declarative languages are [[functional language]]s and [[Logic programming|logical languages]]. The principle behind a ''functional language'' is to use [[lambda calculus]] as a guide for a well defined [[Semantics (computer science)|semantic]].<ref name="cpl_3rd-ch9-217">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 217 | isbn = 0-201-71012-9 }}</ref> In mathematics, a function is a rule that maps elements from an ''expression'' to a range of ''values''. Consider the function: <code>times_10(x) = 10 * x</code> The ''expression'' <code>10 * x</code> is mapped by the function <code>times_10()</code> to a range of ''values''. One ''value'' happens to be 20. This occurs when x is 2. So, the application of the function is mathematically written as: <code>times_10(2) = 20</code> A ''functional language'' compiler will not store this value in a variable. Instead, it will ''push'' the value onto the computer's [[Call stack|stack]] before setting the [[program counter]] back to the calling function. The calling function will then ''pop'' the value from the stack.<ref name="dsa-ch3-p103">{{cite book | last = Weiss | first = Mark Allen | title = Data Structures and Algorithm Analysis in C++ | publisher = Benjamin/Cummings Publishing Company, Inc. | year = 1994 | page = 103 | isbn = 0-8053-5443-3 | quote = When there is a function call, all the important information needs to be saved, such as register values (corresponding to variable names) and the return address (which can be obtained from the program counter)[.] ... When the function wants to return, it ... restores all the registers. It then makes the return jump. Clearly, all of this work can be done using a stack, and that is exactly what happens in virtually every programming language that implements recursion. }}</ref> ''Imperative languages'' do support functions. Therefore, ''functional programming'' can be achieved in an imperative language, if the programmer uses discipline. However, a ''functional language'' will force this discipline onto the programmer through its syntax. Functional languages have a syntax tailored to emphasize the ''what''.<ref name="cpl_3rd-ch9-230">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 230 | isbn = 0-201-71012-9 }}</ref> A functional program is developed with a set of primitive functions followed by a single driver function.<ref name="cpl_3rd-ch9-218"/> Consider the [[Snippet (programming)|snippet]]: <code>function max( a, b ){/* code omitted */}</code> <code>function min( a, b ){/* code omitted */}</code> <code>function range( a, b, c ) {</code> :<code>return max( a, max( b, c ) ) - min( a, min( b, c ) );</code> <code>}</code> The primitives are <code>max()</code> and <code>min()</code>. The driver function is <code>range()</code>. Executing: <code>put( range( 10, 4, 7) );</code> will output 6. ''Functional languages'' are used in [[computer science]] research to explore new language features.<ref name="cpl_3rd-ch9-240">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 240 | isbn = 0-201-71012-9 }}</ref> Moreover, their lack of side-effects have made them popular in [[parallel programming]] and [[concurrent programming]].<ref name="cpl_3rd-ch9-241">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 241 | isbn = 0-201-71012-9 }}</ref> However, application developers prefer the [[object-oriented programming|object-oriented features]] of ''imperative languages''.<ref name="cpl_3rd-ch9-241"/> ====Lisp==== [[Lisp (programming language)|Lisp]] (1958) stands for "LISt Processor".<ref name="ArtOfLisp">{{cite book | last1=Jones | first1=Robin | last2=Maynard | first2=Clive | last3=Stewart | first3=Ian | title=The Art of Lisp Programming | date=December 6, 2012 | publisher=Springer Science & Business Media | isbn=9781447117193 | page=2}}</ref> It is tailored to process [[List (abstract data type)|lists]]. A full structure of the data is formed by building lists of lists. In memory, a [[tree data structure]] is built. Internally, the tree structure lends nicely for [[Recursion (computer science)|recursive]] functions.<ref name="cpl_3rd-ch9-220">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 220 | isbn = 0-201-71012-9 }}</ref> The syntax to build a tree is to enclose the space-separated [[Element (mathematics)|elements]] within parenthesis. The following is a [[list]] of three elements. The first two elements are themselves lists of two elements: <code>((A B) (HELLO WORLD) 94)</code> Lisp has functions to extract and reconstruct elements.<ref name="cpl_3rd-ch9-221">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 221 | isbn = 0-201-71012-9 }}</ref> The function <code>head()</code> returns a list containing the first element in the list. The function <code>tail()</code> returns a list containing everything but the first element. The function <code>cons()</code> returns a list that is the concatenation of other lists. Therefore, the following expression will return the list <code>x</code>: <code>cons(head(x), tail(x))</code> One drawback of Lisp is when many functions are nested, the parentheses may look confusing.<ref name="cpl_3rd-ch9-230"/> Modern Lisp [[Integrated development environment|environments]] help ensure parenthesis match. As an aside, Lisp does support the ''imperative language'' operations of the assignment statement and goto loops.<ref name="cpl_3rd-ch9-229">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 229 | isbn = 0-201-71012-9 }}</ref> Also, ''Lisp'' is not concerned with the [[datatype]] of the elements at compile time.<ref name="cpl_3rd-ch9-227">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 227 | isbn = 0-201-71012-9 }}</ref> Instead, it assigns (and may reassign) the datatypes at [[Runtime (program lifecycle phase)|runtime]]. Assigning the datatype at runtime is called [[Name binding#Binding time|dynamic binding]].<ref name="cpl_3rd-ch9-222">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 222 | isbn = 0-201-71012-9 }}</ref> Whereas dynamic binding increases the language's flexibility, programming errors may linger until late in the [[software development process]].<ref name="cpl_3rd-ch9-222"/> Writing large, reliable, and readable Lisp programs requires forethought. If properly planned, the program may be much shorter than an equivalent ''imperative language'' program.<ref name="cpl_3rd-ch9-230"/> ''Lisp'' is widely used in [[artificial intelligence]]. However, its usage has been accepted only because it has ''imperative language'' operations, making unintended side-effects possible.<ref name="cpl_3rd-ch9-241"/> ====ML==== [[ML (programming language)|ML]] (1973)<ref name="Gordon1996">{{cite web | last = Gordon | first = Michael J. C. | author-link = Michael J. C. Gordon | year = 1996 | title = From LCF to HOL: a short history | url = http://www.cl.cam.ac.uk/~mjcg/papers/HolHistory.html | access-date = 2021-10-30 | archive-date = 2016-09-05 | archive-url = https://web.archive.org/web/20160905201847/http://www.cl.cam.ac.uk/~mjcg/papers/HolHistory.html | url-status = live }}</ref> stands for "Meta Language". ML checks to make sure only data of the same type are compared with one another.<ref name="cpl_3rd-ch9-233">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 233 | isbn = 0-201-71012-9 }}</ref> For example, this function has one input parameter (an integer) and returns an integer: {{sxhl|2=sml|1=fun times_10(n : int) : int = 10 * n;}} ''ML'' is not parenthesis-eccentric like ''Lisp''. The following is an application of <code>times_10()</code>: times_10 2 It returns "20 : int". (Both the results and the datatype are returned.) Like ''Lisp'', ''ML'' is tailored to process lists. Unlike ''Lisp'', each element is the same datatype.<ref name="cpl_3rd-ch9-235">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 235 | isbn = 0-201-71012-9 }}</ref> Moreover, ''ML'' assigns the datatype of an element at [[compile time]]. Assigning the datatype at compile time is called [[Name binding#Binding time|static binding]]. Static binding increases reliability because the compiler checks the context of variables before they are used.<ref name="cpl_3rd-ch3-55">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 55 | isbn = 0-201-71012-9 }}</ref> ====Prolog==== [[Prolog]] (1972) stands for "PROgramming in LOGic". It is a [[logic programming]] language, based on formal [[logic]]. The language was developed by [[Alain Colmerauer]] and Philippe Roussel in Marseille, France. It is an implementation of [[SLD resolution|Selective Linear Definite clause resolution]], pioneered by [[Robert Kowalski]] and others at the [[University of Edinburgh]].<ref>{{Cite journal | publisher = Association for Computing Machinery | doi = 10.1145/155360.155362 | first1 = A. | last1 = Colmerauer | first2 = P. | last2 = Roussel | title = The birth of Prolog | journal = ACM SIGPLAN Notices | volume = 28 | issue = 3 | page = 5 | year = 1992 | url=http://alain.colmerauer.free.fr/alcol/ArchivesPublications/PrologHistory/19november92.pdf}}</ref> The building blocks of a Prolog program are ''facts'' and ''rules''. Here is a simple example: <syntaxhighlight lang=prolog> cat(tom). % tom is a cat mouse(jerry). % jerry is a mouse animal(X) :- cat(X). % each cat is an animal animal(X) :- mouse(X). % each mouse is an animal big(X) :- cat(X). % each cat is big small(X) :- mouse(X). % each mouse is small eat(X,Y) :- mouse(X), cheese(Y). % each mouse eats each cheese eat(X,Y) :- big(X), small(Y). % each big animal eats each small animal </syntaxhighlight> After all the facts and rules are entered, then a question can be asked: : Will Tom eat Jerry? <syntaxhighlight lang=prolog> ?- eat(tom,jerry). true </syntaxhighlight> The following example shows how Prolog will convert a letter grade to its numeric value: <syntaxhighlight lang="prolog"> numeric_grade('A', 4). numeric_grade('B', 3). numeric_grade('C', 2). numeric_grade('D', 1). numeric_grade('F', 0). numeric_grade(X, -1) :- not X = 'A', not X = 'B', not X = 'C', not X = 'D', not X = 'F'. grade('The Student', 'A'). </syntaxhighlight> <syntaxhighlight lang="prolog"> ?- grade('The Student', X), numeric_grade(X, Y). X = 'A', Y = 4 </syntaxhighlight> Here is a comprehensive example:<ref name="Logical English">Kowalski, R., Dávila, J., Sartor, G. and Calejo, M., 2023. Logical English for law and education. In Prolog: The Next 50 Years (pp. 287–299). Cham: Springer Nature Switzerland.</ref> 1) All dragons billow fire, or equivalently, a thing billows fire if the thing is a dragon: <syntaxhighlight lang="prolog"> billows_fire(X) :- is_a_dragon(X). </syntaxhighlight> 2) A creature billows fire if one of its parents billows fire: <syntaxhighlight lang="prolog"> billows_fire(X) :- is_a_creature(X), is_a_parent_of(Y,X), billows_fire(Y). </syntaxhighlight> 3) A thing X is a parent of a thing Y if X is the mother of Y or X is the father of Y: <syntaxhighlight lang="prolog"> is_a_parent_of(X, Y):- is_the_mother_of(X, Y). is_a_parent_of(X, Y):- is_the_father_of(X, Y). </syntaxhighlight> 4) A thing is a creature if the thing is a dragon: <syntaxhighlight lang="prolog"> is_a_creature(X) :- is_a_dragon(X). </syntaxhighlight> 5) Norberta is a dragon, and Puff is a creature. Norberta is the mother of Puff. <syntaxhighlight lang="prolog"> is_a_dragon(norberta). is_a_creature(puff). is_the_mother_of(norberta, puff). </syntaxhighlight> Rule (2) is a [[Recursion (computer science)|recursive]] (inductive) definition. It can be understood declaratively, without the need to understand how it is executed. Rule (3) shows how [[Function (computer programming)|functions]] are represented by using relations. Here, the mother and father functions ensure that every individual has only one mother and only one father. Prolog is an untyped language. Nonetheless, [[Inheritance (object-oriented programming)|inheritance]] can be represented by using predicates. Rule (4) asserts that a creature is a superclass of a dragon. Questions are answered using [[backward reasoning]]. Given the question: <syntaxhighlight lang="prolog"> ?- billows_fire(X). </syntaxhighlight> Prolog generates two answers : <syntaxhighlight lang="prolog"> X = norberta X = puff </syntaxhighlight> Practical applications for Prolog are [[knowledge representation]] and [[problem solving]] in [[artificial intelligence]]. ===Object-oriented programming=== [[Object-oriented programming]] is a programming method to execute [[Method (computer programming)|operations]] ([[Function (computer programming)|functions]]) on [[Object (computer science)|objects]].<ref name="cpl_3rd-ch2-35_quote1">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 35 | isbn = 0-201-71012-9 | quote = Simula was based on Algol 60 with one very important addition — the class concept. ... The basic idea was that the data (or data structure) and the operations performed on it belong together[.] }}</ref> The basic idea is to group the characteristics of a [[phenomenon]] into an object [[Record (computer science)|container]] and give the container a name. The ''operations'' on the phenomenon are also grouped into the container.<ref name="cpl_3rd-ch2-35_quote1"/> ''Object-oriented programming'' developed by combining the need for containers and the need for safe [[functional programming]].<ref name="cpl_3rd-ch2-39_quote1">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 39 | isbn = 0-201-71012-9 | quote = Originally, a large number of experimental languages were designed, many of which combined object-oriented and functional programming. }}</ref> This programming method need not be confined to an ''object-oriented language''.<ref name="se-ch9-284_quote1">{{cite book | last = Schach | first = Stephen R. | title = Software Engineering | publisher = Aksen Associates Incorporated Publishers | year = 1990 | page = 284 | isbn = 0-256-08515-3 | quote = While it is true that OOD [(object oriented design)] as such is not supported by the majority of popular languages, a large subset of OOD can be used. }}</ref> In an object-oriented language, an object container is called a [[Class (computer programming)|class]]. In a non-object-oriented language, a [[data structure]] (which is also known as a [[Record (computer science)|record]]) may become an object container. To turn a data structure into an object container, operations need to be written specifically for the structure. The resulting structure is called an [[abstract datatype]].<ref name="dsa-ch3-p57">{{cite book | last = Weiss | first = Mark Allen | title = Data Structures and Algorithm Analysis in C++ | publisher = Benjamin/Cummings Publishing Company, Inc. | year = 1994 | page = 57 | isbn = 0-8053-5443-3 }}</ref> However, [[Inheritance (object-oriented programming)|inheritance]] will be missing. Nonetheless, this shortcoming can be overcome. Here is a [[C programming language]] ''header file'' for the ''GRADE abstract datatype'' in a simple school application: <syntaxhighlight lang="c"> /* grade.h */ /* ------- */ /* Used to allow multiple source files to include */ /* this header file without duplication errors. */ /* ---------------------------------------------- */ #ifndef GRADE_H #define GRADE_H typedef struct { char letter; } GRADE; /* Constructor */ /* ----------- */ GRADE *grade_new( char letter ); int grade_numeric( char letter ); #endif </syntaxhighlight> The <code>grade_new()</code> function performs the same algorithm as the C++ [[Constructor (object-oriented programming)|constructor]] operation. Here is a C programming language ''[[source file]]'' for the ''GRADE abstract datatype'' in a simple school application: <syntaxhighlight lang="c"> /* grade.c */ /* ------- */ #include "grade.h" GRADE *grade_new( char letter ) { GRADE *grade; /* Allocate heap memory */ /* -------------------- */ if ( ! ( grade = calloc( 1, sizeof ( GRADE ) ) ) ) { fprintf(stderr, "ERROR in %s/%s/%d: calloc() returned empty.\n", __FILE__, __FUNCTION__, __LINE__ ); exit( 1 ); } grade->letter = letter; return grade; } int grade_numeric( char letter ) { if ( ( letter == 'A' || letter == 'a' ) ) return 4; else if ( ( letter == 'B' || letter == 'b' ) ) return 3; else if ( ( letter == 'C' || letter == 'c' ) ) return 2; else if ( ( letter == 'D' || letter == 'd' ) ) return 1; else if ( ( letter == 'F' || letter == 'f' ) ) return 0; else return -1; } </syntaxhighlight> In the constructor, the function <code>calloc()</code> is used instead of <code>malloc()</code> because each memory cell will be set to zero. Here is a C programming language ''header file'' for the ''PERSON abstract datatype'' in a simple school application: <syntaxhighlight lang="cpp"> /* person.h */ /* -------- */ #ifndef PERSON_H #define PERSON_H typedef struct { char *name; } PERSON; /* Constructor */ /* ----------- */ PERSON *person_new( char *name ); #endif </syntaxhighlight> Here is a C programming language ''source file'' for the ''PERSON abstract datatype'' in a simple school application: <syntaxhighlight lang="cpp"> /* person.c */ /* -------- */ #include "person.h" PERSON *person_new( char *name ) { PERSON *person; if ( ! ( person = calloc( 1, sizeof ( PERSON ) ) ) ) { fprintf(stderr, "ERROR in %s/%s/%d: calloc() returned empty.\n", __FILE__, __FUNCTION__, __LINE__ ); exit( 1 ); } person->name = name; return person; } </syntaxhighlight> Here is a C programming language ''header file'' for the ''STUDENT abstract datatype'' in a simple school application: <syntaxhighlight lang="c"> /* student.h */ /* --------- */ #ifndef STUDENT_H #define STUDENT_H #include "person.h" #include "grade.h" typedef struct { /* A STUDENT is a subset of PERSON. */ /* -------------------------------- */ PERSON *person; GRADE *grade; } STUDENT; /* Constructor */ /* ----------- */ STUDENT *student_new( char *name ); #endif </syntaxhighlight> Here is a C programming language ''source file'' for the ''STUDENT abstract datatype'' in a simple school application: <syntaxhighlight lang="cpp"> /* student.c */ /* --------- */ #include "student.h" #include "person.h" STUDENT *student_new( char *name ) { STUDENT *student; if ( ! ( student = calloc( 1, sizeof ( STUDENT ) ) ) ) { fprintf(stderr, "ERROR in %s/%s/%d: calloc() returned empty.\n", __FILE__, __FUNCTION__, __LINE__ ); exit( 1 ); } /* Execute the constructor of the PERSON superclass. */ /* ------------------------------------------------- */ student->person = person_new( name ); return student; } </syntaxhighlight> Here is a driver program for demonstration: <syntaxhighlight lang="c"> /* student_dvr.c */ /* ------------- */ #include <stdio.h> #include "student.h" int main( void ) { STUDENT *student = student_new( "The Student" ); student->grade = grade_new( 'a' ); printf( "%s: Numeric grade = %d\n", /* Whereas a subset exists, inheritance does not. */ student->person->name, /* Functional programming is executing functions just-in-time (JIT) */ grade_numeric( student->grade->letter ) ); return 0; } </syntaxhighlight> Here is a [[makefile]] to compile everything: <syntaxhighlight lang="make"> # makefile # -------- all: student_dvr clean: rm student_dvr *.o student_dvr: student_dvr.c grade.o student.o person.o gcc student_dvr.c grade.o student.o person.o -o student_dvr grade.o: grade.c grade.h gcc -c grade.c student.o: student.c student.h gcc -c student.c person.o: person.c person.h gcc -c person.c </syntaxhighlight> The formal strategy to build object-oriented objects is to:<ref name="se-ch9-285">{{cite book | last = Schach | first = Stephen R. | title = Software Engineering | publisher = Aksen Associates Incorporated Publishers | year = 1990 | page = 285 | isbn = 0-256-08515-3 }}</ref> * Identify the objects. Most likely these will be nouns. * Identify each object's attributes. What helps to describe the object? * Identify each object's actions. Most likely these will be verbs. * Identify the relationships from object to object. Most likely these will be verbs. For example: * A person is a human identified by a name. * A grade is an achievement identified by a letter. * A student is a person who earns a grade. ===Syntax and semantics=== [[File:Terminal and non-terminal symbols example.png|300px|thumb|right|Production rules consist of a set of terminals and non-terminals.]] The [[Syntax (programming languages)|syntax]] of a ''computer program'' is a [[list]] of [[Production (computer science)|production rules]] which form its [[formal grammar|grammar]].<ref name="cpl_3rd-ch12-290_quote">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 290 | quote = The syntax (or grammar) of a programming language describes the correct form in which programs may be written[.] | isbn = 0-201-71012-9 }}</ref> A programming language's grammar correctly places its [[Declaration (computer programming)|declarations]], [[Expression (computer science)|expressions]], and [[Statement (computer science)|statements]].<ref name="cpl_3rd-ch4-78_quote1">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 78 | isbn = 0-201-71012-9 | quote = The main components of an imperative language are declarations, expressions, and statements. }}</ref> Complementing the ''syntax'' of a language are its [[Semantics (computer science)|semantics]]. The ''semantics'' describe the meanings attached to various syntactic constructs.<ref name="cpl_3rd-ch12-290">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 290 | isbn = 0-201-71012-9 }}</ref> A syntactic construct may need a semantic description because a production rule may have an invalid interpretation.<ref name="cpl_3rd-ch12-294">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 294 | isbn = 0-201-71012-9 }}</ref> Also, different languages might have the same syntax; however, their behaviors may be different. The syntax of a language is formally described by listing the production rules. Whereas the syntax of a [[natural language]] is extremely complicated, a subset of the English language can have this production rule listing:<ref name="discrete-ch10-p615">{{cite book | last = Rosen | first = Kenneth H. | title = Discrete Mathematics and Its Applications | publisher = McGraw-Hill, Inc. | year = 1991 | page = [https://archive.org/details/discretemathemat00rose/page/615 615] | isbn = 978-0-07-053744-6 | url = https://archive.org/details/discretemathemat00rose/page/615}}</ref> # a '''sentence''' is made up of a '''noun-phrase''' followed by a '''verb-phrase'''; # a '''noun-phrase''' is made up of an '''article''' followed by an '''adjective''' followed by a '''noun'''; # a '''verb-phrase''' is made up of a '''verb''' followed by a '''noun-phrase'''; # an '''article''' is 'the'; # an '''adjective''' is 'big' or # an '''adjective''' is 'small'; # a '''noun''' is 'cat' or # a '''noun''' is 'mouse'; # a '''verb''' is 'eats'; The words in '''bold-face''' are known as ''non-terminals''. The words in 'single quotes' are known as ''terminals''.<ref name="cpl_3rd-ch12-291">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 291 | isbn = 0-201-71012-9 }}</ref> From this production rule listing, complete sentences may be formed using a series of replacements.<ref name="discrete-ch10-p616">{{cite book | last = Rosen | first = Kenneth H. | title = Discrete Mathematics and Its Applications | publisher = McGraw-Hill, Inc. | year = 1991 | page = [https://archive.org/details/discretemathemat00rose/page/616 616] | isbn = 978-0-07-053744-6 | url = https://archive.org/details/discretemathemat00rose/page/616}}</ref> The process is to replace ''non-terminals'' with either a valid ''non-terminal'' or a valid ''terminal''. The replacement process repeats until only ''terminals'' remain. One valid sentence is: * '''sentence''' * '''noun-phrase''' '''verb-phrase''' * '''article''' '''adjective''' '''noun''' '''verb-phrase''' * ''the'' '''adjective''' '''noun''' '''verb-phrase''' * ''the'' ''big'' '''noun''' '''verb-phrase''' * ''the'' ''big'' ''cat'' '''verb-phrase''' * ''the'' ''big'' ''cat'' '''verb''' '''noun-phrase''' * ''the'' ''big'' ''cat'' ''eats'' '''noun-phrase''' * ''the'' ''big'' ''cat'' ''eats'' '''article''' '''adjective''' '''noun''' * ''the'' ''big'' ''cat'' ''eats'' ''the'' '''adjective''' '''noun''' * ''the'' ''big'' ''cat'' ''eats'' ''the'' ''small'' '''noun''' * ''the'' ''big'' ''cat'' ''eats'' ''the'' ''small'' ''mouse'' However, another combination results in an invalid sentence: * ''the'' ''small'' ''mouse'' ''eats'' ''the'' ''big'' ''cat'' Therefore, a ''semantic'' is necessary to correctly describe the meaning of an ''eat'' activity. One ''production rule'' listing method is called the [[Backus–Naur form]] (BNF).<ref name="discrete-ch10-p623">{{cite book | last = Rosen | first = Kenneth H. | title = Discrete Mathematics and Its Applications | publisher = McGraw-Hill, Inc. | year = 1991 | page = [https://archive.org/details/discretemathemat00rose/page/623 623] | isbn = 978-0-07-053744-6 | url = https://archive.org/details/discretemathemat00rose/page/623}}</ref> BNF describes the syntax of a language and itself has a ''syntax''. This recursive definition is an example of a [[metalanguage]].<ref name="cpl_3rd-ch12-290"/> The ''syntax'' of BNF includes: * <code>::=</code> which translates to ''is made up of a[n]'' when a non-terminal is to its right. It translates to ''is'' when a terminal is to its right. * <code>|</code> which translates to ''or''. * <code><</code> and <code>></code> which surround '''non-terminals'''. Using BNF, a subset of the English language can have this ''production rule'' listing: <syntaxhighlight lang="bnf"> <sentence> ::= <noun-phrase><verb-phrase> <noun-phrase> ::= <article><adjective><noun> <verb-phrase> ::= <verb><noun-phrase> <article> ::= the <adjective> ::= big | small <noun> ::= cat | mouse <verb> ::= eats </syntaxhighlight> Using BNF, a signed-[[Integer (computer science)|integer]] has the ''production rule'' listing:<ref name="discrete-ch10-p624">{{cite book | last = Rosen | first = Kenneth H. | title = Discrete Mathematics and Its Applications | publisher = McGraw-Hill, Inc. | year = 1991 | page = [https://archive.org/details/discretemathemat00rose/page/624 624] | isbn = 978-0-07-053744-6 | url = https://archive.org/details/discretemathemat00rose/page/624}}</ref> <syntaxhighlight lang="bnf"> <signed-integer> ::= <sign><integer> <sign> ::= + | - <integer> ::= <digit> | <digit><integer> <digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 </syntaxhighlight> Notice the recursive production rule: <syntaxhighlight lang="bnf"> <integer> ::= <digit> | <digit><integer> </syntaxhighlight> This allows for an infinite number of possibilities. Therefore, a ''semantic'' is necessary to describe a limitation of the number of digits. Notice the leading zero possibility in the production rules: <syntaxhighlight lang="bnf"> <integer> ::= <digit> | <digit><integer> <digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 </syntaxhighlight> Therefore, a ''semantic'' is necessary to describe that leading zeros need to be ignored. Two formal methods are available to describe ''semantics''. They are [[denotational semantics]] and [[axiomatic semantics]].<ref name="cpl_3rd-ch12-297">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 297 | isbn = 0-201-71012-9 }}</ref>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)