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
Imperative programming
(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!
==Examples== ===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]] * [[subroutine]]s * [[For loop|"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 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 structure, where variables were local to their block * arrays with variable bounds * [[For loop|"for" loops]] * [[Subroutine#Functions|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 there's [[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">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 30 | isbn = 0-201-71012-9 }}</ref> 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 a line number * 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|Microsoft's]] [[Visual Basic]] is still widely used and produces a [[graphical user interface]].<ref name="cpl_3rd-ch2-31">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 31 | isbn = 0-201-71012-9 }}</ref> ===C=== [[C (programming language)|C programming language]] (1973) got its name because the language [[BCPL]] was replaced with [[B (programming language)|B]], and [[Bell Labs|AT&T Bell Labs]] called the next version "C." Its purpose was to write the [[UNIX]] [[operating system]].<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> 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 in which region of memory data is to be stored. ''Global variables'' and ''static variables'' require the fewest [[Clock signal|clock cycles]] 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 (computer programming)|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's 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 = 25 May 2022 | 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 ''[[Parameter (computer programming)#Parameters and arguments|formal parameter]] variables''. Parameter variables are enclosed within the parenthesis of function definitions.<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> They 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> * 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.<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.<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. 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 engineering|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 and concrete|abstract]] [[Data type|datatypes]].<ref name="cpl_3rd-ch2-38"/> At the time, languages supported concrete ([[Variable (computer science)|scalar]]) datatypes like [[integer]] numbers, [[Floating-point arithmetic|floating-point]] numbers, and [[String (computer science)|strings]] of [[Character (computing)|characters]]. Concrete datatypes have their representation as part of their name.<ref name="stroustrup-ch3-65">{{cite book | last = Stroustrup | first = Bjarne | title = The C++ Programming Language, Fourth Edition | publisher = Addison-Wesley | year = 2013 | page = 65 | isbn = 978-0-321-56384-2 }}</ref> 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, it's 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]], [[Method (computer programming)#Member functions in C++|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 persons don't 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 [[Include directive|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. // See: https://en.wikipedia.org/wiki/Include_guard // ---------------------------------------------- #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</code> statement. A module's other file is the ''[[source code|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 ); ~STUDENT(); 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. // ------------------- } STUDENT::~STUDENT() { // deallocate grade's memory // to avoid memory leaks. // ------------------------------------------------- delete this->grade; } </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"; // deallocate student's memory // to avoid memory leaks. // ------------------------------------------------- delete student; 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>
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)