1 / 22

Procedure-Based Programming

Procedure-Based Programming. main(). first function invoked at execution. func1(). func2(). func3(). func4(). The functions within a program communicate through values that they receive and through values that they return. Procedural programming language designed by D. Ritchie

Download Presentation

Procedure-Based Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Procedure-Based Programming main() first function invoked at execution func1() func2() func3() func4() ... The functions within a program communicate through values that they receive and through values that they return.

  2. Procedural programming language designed by D. Ritchie to implement the UNIX operating system (1972). Provides low-level memory access – very efficient! Provides language constructs that map efficiently to machine instructions Requires minimal run-time support. C Data structures such as arrays or structs allow too easy access and modification. Compromise of data integrity decreases program quality and software productivity.

  3. Information hiding – keep the interface separate from the implementation. Encapsulation – keep the data and the functions that manipulate them in one place. New data types can be created and manipulated as easily as built-in types. Object-Oriented Programming A programming paradigm based on two principles:  Users careabout the interface and operations of a new data type.  Only the implementer cares about the implementation details. Program with a black box! The two principles are implemented in C++ through the “class” facility.

  4. A superset of C (except for minor details) better in type checking and expressing modularity public interface + private implementation retains C’s efficiency dealing with hardware (bits, bytes, words etc.) public: Data Functions private: Data Functions C++ data items + functions (operators) = programs Class Members External Program Units

  5. Example – Exchange Sort 8 3 6 2 3 8 6 2 2 8 6 3 2 6 8 3 2 3 8 6 2 3 6 8

  6. C++ Code #include <iostream.h> // interchange the values of the two integer variables x and y void Swap(int &x, int &y) { int temp = x; // store original value of x x = y; // replace x by y y = temp; // assign y the original value of x } // sort the n element integer array a in ascending order. void ExchangeSort(int a[ ], int n) { int i, j; /* implement n-1 passes. locate correct values in a[0],...,a[n-2]. */ for (i=0; i<n-1; i++) // put minimum of a[i+1]...a[n-1] in a[i] for (j=i+1; j<n; j++) // exchange if a[i]>a[j] if (a[i] > a[j]) Swap(a[i],a[j]); } // step through the list and print each value. void PrintList(int a[ ], int n) { for (int i=0; i<n; i++) cout << a[i] << " "; cout << endl; } void main() { int list[15] = {38, 58, 13, 15, 51, 27, 10, 19, 12, 86, 49, 67, 84, 60, 25}; cout << "Original List \n"; PrintList(list, 15); ExchangeSort(list,15); cout << endl << "Sorted List" << endl; PrintList(list, 15); }

  7. Header and Source Files A header file declares each function, object, and data type that is part of the public interface // File add.h #ifndef ADD_H #define ADD_H int add(int, int); #endif ifndef tests whether ADD_H has been defined previously. If not, define it. This guarantees that the header file is processed only once in case multiple source files include it. Its implementation (definition) lies in a source file. // File add.cpp #include "add.h" int add(int a, int b) { return a + b; } The complier will check the declaration and definition for consistency.

  8. The User Code // File triple.cpp #include "add.h" int triple(int x) { return add(x, add(x, x)); } When the definition of add changes in add.cpp, triple.c needs not be modified, unless the declaration of add changes in add.h. So triple does not need to know the implementation details of the add function. It reduces the maintenance burden.

  9. Compile & Execute -- Makefile and make $ cat Makefile triple.exe: triple.o add.o g++ -o triple.exe triple.o add.o triple.o: triple.cpp add.h g++ -c triple.cpp add.o: add.cpp add.h g++ -c add.cpp $ make $ triple generate files add.o, triple.o, and triple.exe according to their dependencies. triple.exe depends on triple.o and add.o, and is generated by running the g++ complier on them. triple.o depends on triple.cpp and add.h. add.o depends on add.cpp and add.h. Note “g++ -c” instead of “g++ -o” is used to generate them.

  10. Unix File System (1969) bin dev etc tmp usr unix boot you mike paul mary junk junk temp junk data The Unix Programming Environment by B. W. Kernighan and R. Pike, Prentice-Hall, Inc., 1984. ISBN 0-13-937681-X for more see http://en.wikipedia.org/wiki/UNIX

  11. The Directory Hierarchy / root of the file system /bin essential programs in executable form /dev device files /etc system miscellany /etc/passwd password file /lib essential libraries, etc. /tmp temporary files; cleaned at system restart /usr user file system /usr/adm system administration /usr/bin user binaries /usr/include header files for C programs, e.g. math.h /usr/lib libraries for C, FORTRAN /usr/man on-line manual /usr/src source code for utilities and libraries /usr/you your login directory /usr/you/bin your personal programs

  12. Your Home Directory All files of yours start with /usr/you. $ ls junk file name /usr/you/junk print working directory $ pwd /usr/you $ mkdir recipes $ cd recipes $ pwd /usr/you/recipes $ mkdir pie cookie $ ls pie cookie $ cd .. $ pwd /usr/you return to the parent directory

  13. Tree Structure /usr/you recipes junk pie cookie apple crust choc.chip

  14. /usr/you recipes junk pie cookie apple crust choc.chip Command du Tells how much disc space (in bytes) is consumed by the files in a directory, including all its subdirectories. • $ pwd • /usr/you/ • $ du • ./recipes/pie • ./recipes/cookies • ./recipes • . • $ current directory

  15. /usr/you recipes junk pie cookie apple crust choc.chip Print out All the Files Use the option -a, for “all”. • $ du -a • ./recipes/pie/apple • 3 ./recipes/pie/crust • ./recipes/pie • ./recipes/cookie/choc.chip • ./recipes/cookie • ./recipes • ./junk • . • $

  16. /usr/you recipes junk pie cookie apple crust choc.chip Use of Regular Expressions A convenient means of referring to files/directories as long as ambiguities would not arise. r* zero or more characters beginning with r r+ one or more characters beginning with r r? r followed by any single character ? any single character $ pwd /usr/you $ cd r* $ pwd /usr/you/recipes $ cd *ie *ie: Ambiguous $ cd c*ie $ cd ../p* $ pwd /usr/you/recipes/pie $ cat *e [contents of the file apple]

  17. Looking up a Command $ man cd BASH_BUILTINS(1) BASH_BUILTINS(1) NNAAMMEE bash, :, ., [, alias, bg, bind, break, builtin, cd, command, compgen, complete, continue, declare, dirs, disown, echo, enable, eval, exec, exit, export, fc, fg, getopts, hash, help, history, jobs, kill, let, local, logout, popd, printf, pushd, pwd, read, readonly, return, set, shift, shopt, source, suspend, test, times, trap, type, typeset, ulimit, umask, unalias, unset, wait - bash built-in commands, see bbaasshh(1) BBAASSHH BBUUIILLTTIINN CCOOMMMMAANNDDSS Unless otherwise noted, each builtin command documented in this section as accepting options preceded by -- accepts ---- to signify the end of the options. :: [_a_r_g_u_m_e_n_t_s] No effect; the command does nothing beyond expanding _a_r_g_u_m_e_n_t_s and performing any specified redirections. A zero exit code is returned.

  18. Permissions Every file has a set of permissions associated with it, which determines who can do what with the file. The -l option of ls prints the permissions. $ ls -l /etc/passwd -rw-r--r-- 1 root 5115 Aug 30 10:40 /etc/passwd Owned by root, 5115 bytes long, last modified on August 30 at 10:30am, and has one link. -rw-r--r-- represented by 644 (i.e. 110100100) the rest of the users can only read. people in adm group can read but not write or execute. ordinary file root may read or write but not execute

  19. Change Permissions Change the permissions on junk to “rw-rw-rw-” (i.e. 110110110). $ chmod 666 junk Allow everyone to execute command: $ chmod +x command Turn off write permission for everyone $ chmod -w file

  20. /usr/you recipes junk pie cookie apple crust choc.chip Remove a File or Directory $ pwd /usr/you $ cd recipes/pie $ pwd /usr/you/recipes/pie $ ls apple crust $ rm apple crust $ ls $ cd ..; pwd /usr/you/recipes $ rmdir pie $ rmdir cookies rmdir: `cookies': Directory not empty execute two commands sequentially

  21. Copy and Move Files /usr/you $ pwd /usr/you $ cp junk recipes/copyofjunk $ cd recipes $ mv cookie/choc.chip .. $ cd .. $ ls junk recipes choc.chip $ mv junk nojunk choc.chip recipes junk nojunk cookie copyofjunk choc.chip

  22. Editors You can use vi or troff. But most people prefer emacs. (http://www.delorie.com/gnu/docs/emacs/emacs_toc.html) Start emacs $ emacs $ emacs & or $ emacs -nw Basic commands within the emacs window C-x C-w write to a file C-x C-s save to the current file C-x C-f load a file C-s search a string Esc-x replace-string replace all occurrences of a string Esc-% query replace C-v roll one page forward Esc-v roll one page backward C-x C-c exit the Emacs window C-x C-k kill a buffer

More Related