Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
22 views
CPT211 Unit 2
computer programming
Uploaded by
fayyadmuhammad45
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save CPT211 Unit 2 For Later
Download
Save
Save CPT211 Unit 2 For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
0% found this document useful (0 votes)
22 views
CPT211 Unit 2
computer programming
Uploaded by
fayyadmuhammad45
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save CPT211 Unit 2 For Later
Carousel Previous
Carousel Next
Save
Save CPT211 Unit 2 For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 9
Search
Fullscreen
Programming Paradigms given programming language can be organized. 4, styles in which a , features and opinions about how common These are different ways or Each paradigm consists of certain. structure Programming problems should be solved. Certain paradigms are better suited for some problems, There are many programming paradigms such as: Object-oriented (OOP); functional; imperative; procedural; declarative; structured; multi- paradigm; parallel; generic; ‘even-driven; _ prototype-bas: Reactive and Concurrent programming, The list is growing. Imperative, procedural, functional, declarative, and object oriented paradigms are some of the most popular and widely used paradigms today. knowing the basics about them te good for Seneral knowledge and also for betier understanding other topics of the coding world. the most popular programming paradigms are: Imperative programming, Procedural Programming, Functional programming, Declarative Programming, and Object-oriented Programming. However, i. ii, iii, iv. v. Imperative Programming instructions that are given to the computer to 41 luse as programmers we dictate exactly Imperative programming consists of sets of detailed i + execute in a given order. It's called "imperative" beca what the computer has to do, in a very specific way, Imperative programming focuses on describing how a program operates, step by step. Say you want to bake a cake. Your imperative Dae do this might look like this: ° (Che ne ea. Ist SO odd Namhevs J- Pour flour in a bow! . 2- Pour a couple eggs in the same bowl 3+ Pour some milk in the same bow! 1 Stee { so ee O, nt 7 Gan = San th nen +b : an gn nz9) repo ckp CrnA- ue ShBern “Find the Qn of os4- Mix the ingredients $+ Pour the mix ina mold _ 6 Cook for 35 minutes 7-Let chill ' Using an actual code example, let's say we want to filter an array of numbers to only Keep the elements bigger than 5. Our imperative code might look like this: aon oo 4 ; a abit const nums = [143.6.78.9.2| lwp ¢fiv clovlipenack whice functions const result = |) ; Gre vmphed | Locked vy Ver for (let i 0: i
5) result. push(nums|i|) Step read cs ode G ceginpte °F d prow a4 4% Sejnence of Gbps console log(result) // Output: | 67.8.9 | See that we're telling the program to iterate through each element in the array, compare the item = value with 5, and if the ifem is bigger than 5, push it into an array. We're being detailed and specific in our instructions, and that's what imperative programming stands for. 4.2. Procedural Programming Procedural programming is a derivation of imperative programming, functions (also known as "procedures" or "subroutines"). adding to it the feature of the user is encouraged to subdivide the program exécution into In procedural programming, dularity and organization. functions, as a way of improving mo Je, procedural programming may look like this: oy pavads function pouringredients() { . . a iene Ben ie , Pour flour in a bow! wed frown ay prea laged otter Concagt of proc dor: ple eggs in the same bow! icin the oe - Pour a cou} Cals: i me milk in the same bowl Following our cake examp - Pour solfunction mix AndTransferToMold() { - Mix the ingredients - Pour the mix in a mold function cookAndLetChillQ) { - Cook for 35 minutes - Let chill pourIngredients() mixAndTransferToMold() cookAndLetChill() You can see that, thanks to the implementation of functions, we could just read the three function 4 - calls at the end of the filé and get a good idea of what our program does. That simplification and abstraction is one of the benefits of procedural programming. But within the functions, we still got same old imperative code. 43. Functional Programming (FP) Functional programming takes the concept of functions a little bit further. In functional programming, functions are treated as first-class citizens, meaning that they can be assigned to variables, passed as arguments, and returned from other functions. : ‘Another key concept is the idea of pure functions. A pure function is one that relies only on its inputs to generate its result. And given the same input, it will always produce the same result, Besides, it produces no side effects (any change outside the function's environment). With these concepts in mind, functional programming encourages programs written mostly with fanctions (surprise 0). It also defends the idea that code modularity and the absence of side effects makes it easier to identify, and separate responsibilities within the codebase. This therefore improves the code maintainability. xample, we can see that with the imperative ‘paradigm we result, which can be considered a side ~ Going back to the array filtering ¢: a ight use an external variable to store the function's Gree pure fry to Creat Mai whonable S|const result = {} // External variable for (let i= 0; i
5) result.push(nums{i}) * console.log(result) // Outputs 67,89] To transform this into functional programming, we could do it like this: const nums = {1,4,3,6,7.8,9.2] funetion filterNums() { const result = |} / Internal variable ‘for (let i= 0; i
5) result. push(nums{i}) return result console.log(filterNums()) // Output: [ 6, 7.8,9 | It's almost the same code, but we wrap our iteration within a function, in which we also store the result array. In this way, we can assure the function doesn't modify anything outside its scope. It only creates a variable to process its own information, and once the execution is finished, the variable is gone too. 4.4 Declarative Programming “Declarative programming is all about hiding away’ complexity and bringing programming iguages closer to human language and thinking. It’s the direct opposite of imperative programming in the sense that the programmer doesn't give instructions about ow the computer should execute the task, but rather on what result is needed. is will be much clearer with an example. Following the same array filtering story, a Instruct a Oni, ee Be ws Abe Ub 4 oe Lorton, DEL)( Sonstnums © |1.4.3.6.7.8.9.2| € DP piole.toginums.Slter(aum => num > 5)) / Output: [ 6, 7,8, 9 | © je thet withthe filter function, we've not explicitly telling the computer to iterate over the array ..2. 9 store the values in a separate array, We just say what we want ("filter") and the condition to be t ("num > 5"), What's nice about this is that it's easier to read and comprehend, and often shorter to write. JavaScript's filter, map, reduce and sort functions are good examples of declarative code, . Take this code for example:
console.log("You clicked me!")}>Click me
Here we have a button element, with an event listener that fires a console.log function when the button is clicked. ISX syntax (what React uses) mixes HTML and JS in the same thing, which makes it easier and faster to write apps. But that's not what browsers read and execute. React code is later on transpiled into regular HTML and JS, and that's what browsers run in reality. JSX is declarative, in the sense that its purpose is to give developers a friendlier and more efficient interface to work with. ‘An important thing to notice about declarative prégramming is that under the hood, the computer processes this information as imperative code anyway. : ke a ar Following the array example, the computer still iterates over the array like in a for loop, but as - programmers we don't heed to code that directly. What declarative programming does is to hide away that complexity from the direct view of the programmer. Re "45,. Object-Oriented Programming mn of the most popular programming paradigms is object-oriented programming (OOP). “ OOP organizes o[W dey wh yes eros” daka wr Objeoks abstya bh Yor trom fon Kiem x emcapsulatn es AG “babi Pom ©) 0_ The core concept of OOP is to separate concerns into entities which are coded as objects, Each _ entity will group a given set of information (properties) and actions (methods) that can be performed by the entity. -— OOP makes heavy usage of classes (which are a way of creating new objects starting out from a blueprint or boilerplate that the programmer sets). Objects that are created from a class are called instances. Following our pseudo-code cooking, example, now let's say in our bakery we have a main cook (called Frank) and an assistant cook (called Anthony) and each of them will have certain responsibilities in the baking process. If we used OOP, our program might look like this. // Create the two dla: 3 corresponding to ch entity class Cook { constructor constructor (name) ( this.name = name mixAndBake() { - Mix the ingredients - Pour the mix in a mold ~ Cook for 35 minutes class AssistantCook { constructor’ (name) { this.name + name . y pourIngredients() { = Pour flour in a bowl ~ Pour a couple eggs in the same bowl = Pour some milk in the same bowl Se i chill thecake()- ( 12~ Let chili Instantiate an object from_each class. (Qeorist Frank = new Cook ('Frank') = qoast Anthony = new AssistantCook('Anthony') G2 //. call the corresponding methods from each instance Anthony .pourIngredients () Frank. mixAndBake () Anthony. chill Thecake () - What's nice about OOP is that it facilitates the understanding of a program, by the oo separation of concerns and responsibilities. In this example I've just scratched the surface of the many features of OOP. You will learn more about these in subsequent lectures. As we've seen, programming paradigms are different ways in which we can face programming problems, and organize our code. Tutorial Questions * “he References ; vo L. | Problem solving with C++ 7ed. By Walter Savitch. Pearson Int'l Edition. 2. Programming paradigms: Examples for bginners. By German Cocea (2012). www.freecodecamp.org. 13eh i nr chow ab a ks Sumas Noes idertilce v 5 Nor heS Declovabows Ksigu~e” gts t/o gts 7 y, ex resets Dake Type ond “XP Se A oolenn hedawre fs of Conbrol Ke opecatov! exprcsSuS
You might also like
Eloquent JavaScript 4th Edition Marijn Haverbekedownload
PDF
100% (2)
Eloquent JavaScript 4th Edition Marijn Haverbekedownload
60 pages
Mena As Functional Programming Ideas For The Curious Kotline
PDF
No ratings yet
Mena As Functional Programming Ideas For The Curious Kotline
186 pages
Functional Programming in Swift (2014!10!01)
PDF
100% (1)
Functional Programming in Swift (2014!10!01)
220 pages
Programming Paradigms: A Must Know For All Programmers
PDF
No ratings yet
Programming Paradigms: A Must Know For All Programmers
4 pages
Ch-1_1
PDF
No ratings yet
Ch-1_1
48 pages
3. Object Oriented Programming MODIFIED bonga
PDF
No ratings yet
3. Object Oriented Programming MODIFIED bonga
121 pages
Java CS Tut n Questions (7)
PDF
No ratings yet
Java CS Tut n Questions (7)
31 pages
oops
PDF
No ratings yet
oops
8 pages
Integrative Programming Technology Programming Paradigm
PDF
No ratings yet
Integrative Programming Technology Programming Paradigm
17 pages
Name: Joel James P. Alao Assignment 1
PDF
No ratings yet
Name: Joel James P. Alao Assignment 1
7 pages
APLC Notes
PDF
No ratings yet
APLC Notes
21 pages
Programming Constructs Paradigms PDF
PDF
No ratings yet
Programming Constructs Paradigms PDF
26 pages
Programming Paradigms - Rekik & Haileyesus
PDF
No ratings yet
Programming Paradigms - Rekik & Haileyesus
7 pages
INFO134 INTRO
PDF
No ratings yet
INFO134 INTRO
48 pages
lecture 1
PDF
No ratings yet
lecture 1
81 pages
01chapter One - Introduction To OOP
PDF
No ratings yet
01chapter One - Introduction To OOP
27 pages
Lecture One
PDF
No ratings yet
Lecture One
14 pages
Programming Languages and Paradigms
PDF
No ratings yet
Programming Languages and Paradigms
13 pages
All in One Oop
PDF
No ratings yet
All in One Oop
99 pages
Fundamentals of Structured Programming: Lubia Vinhas March 16, 2016
PDF
No ratings yet
Fundamentals of Structured Programming: Lubia Vinhas March 16, 2016
25 pages
OOPS Notes
PDF
No ratings yet
OOPS Notes
90 pages
Programming Language Types and Paradigm: CS1303 - Object Oriented Programming Unit - I Java Fundamentals
PDF
No ratings yet
Programming Language Types and Paradigm: CS1303 - Object Oriented Programming Unit - I Java Fundamentals
52 pages
Computer Sc_ UT_1
PDF
No ratings yet
Computer Sc_ UT_1
31 pages
Prompt
PDF
No ratings yet
Prompt
7 pages
The Programmer Dream PDF
PDF
No ratings yet
The Programmer Dream PDF
42 pages
04-Logic Programming Class notes
PDF
No ratings yet
04-Logic Programming Class notes
6 pages
Eloquent JavaScript A Modern Introduction to Programming 2nd Edition Marijn Haverbeke download
PDF
100% (1)
Eloquent JavaScript A Modern Introduction to Programming 2nd Edition Marijn Haverbeke download
32 pages
Programming Paradigm
PDF
No ratings yet
Programming Paradigm
14 pages
Programação Básica
PDF
No ratings yet
Programação Básica
41 pages
Eloquent JavaScript A Modern Introduction to Programming 2nd Edition Marijn Haverbeke - The full ebook with all chapters is available for download now
PDF
100% (2)
Eloquent JavaScript A Modern Introduction to Programming 2nd Edition Marijn Haverbeke - The full ebook with all chapters is available for download now
47 pages
03-Design and Analysis of Alogrithms
PDF
No ratings yet
03-Design and Analysis of Alogrithms
5 pages
Module1 - Intro To Prog Paradigm
PDF
No ratings yet
Module1 - Intro To Prog Paradigm
21 pages
Assignment Final
PDF
No ratings yet
Assignment Final
8 pages
Eloquent JavaScript 3rd Edition Marijn Haverbeke - The ebook is available for quick download, easy access to content
PDF
100% (1)
Eloquent JavaScript 3rd Edition Marijn Haverbeke - The ebook is available for quick download, easy access to content
55 pages
Chapter One 1.1 Type of Paradigm
PDF
No ratings yet
Chapter One 1.1 Type of Paradigm
9 pages
JavaScript and Node FUNdamentals 1st Edition by Azat Mardan ISBN B00HDYHKN6 - Download the ebook now to never miss important content
PDF
100% (7)
JavaScript and Node FUNdamentals 1st Edition by Azat Mardan ISBN B00HDYHKN6 - Download the ebook now to never miss important content
82 pages
Elon musk Program
PDF
No ratings yet
Elon musk Program
4 pages
Big O Notation, Programming Paradigms
PDF
No ratings yet
Big O Notation, Programming Paradigms
3 pages
Deep JavaScript Theory and Techniques
PDF
100% (1)
Deep JavaScript Theory and Techniques
226 pages
Orp Intjava
PDF
No ratings yet
Orp Intjava
43 pages
Eloquent JavaScript 3rd Edition Marijn Haverbeke - Download the complete ebook in PDF format and read freely
PDF
100% (1)
Eloquent JavaScript 3rd Edition Marijn Haverbeke - Download the complete ebook in PDF format and read freely
47 pages
Advanced Programming Ch1
PDF
No ratings yet
Advanced Programming Ch1
89 pages
JavaScript and Node FUNdamentals 1st Edition by Azat Mardan ISBN B00HDYHKN6 instant download
PDF
100% (4)
JavaScript and Node FUNdamentals 1st Edition by Azat Mardan ISBN B00HDYHKN6 instant download
52 pages
Introduction To Programming Paradigms
PDF
No ratings yet
Introduction To Programming Paradigms
16 pages
Object Oriented Programming Languages
PDF
No ratings yet
Object Oriented Programming Languages
20 pages
1- INTRODUCTION TO PROGRAMMING
PDF
No ratings yet
1- INTRODUCTION TO PROGRAMMING
28 pages
Implement and Explain Four Programming Paradigms to Solve an Array Function
PDF
No ratings yet
Implement and Explain Four Programming Paradigms to Solve an Array Function
5 pages
Programming Paradigms
PDF
No ratings yet
Programming Paradigms
14 pages
Unit 1 Introduction
PDF
No ratings yet
Unit 1 Introduction
29 pages
Code Innovators
PDF
No ratings yet
Code Innovators
11 pages
JavaScript Fundamentals
PDF
100% (6)
JavaScript Fundamentals
80 pages
Tran Minh Quan (FGW HN) - 259158 - 0
PDF
No ratings yet
Tran Minh Quan (FGW HN) - 259158 - 0
55 pages
02-Linear Algebra Ordinary Differential Equations I
PDF
No ratings yet
02-Linear Algebra Ordinary Differential Equations I
4 pages
JavaScript and Node FUNdamentals 1st Edition by Azat Mardan ISBN B00HDYHKN6 - Read the ebook online or download it for a complete experience
PDF
100% (6)
JavaScript and Node FUNdamentals 1st Edition by Azat Mardan ISBN B00HDYHKN6 - Read the ebook online or download it for a complete experience
91 pages
[FREE PDF sample] (Ebook) Eloquent JavaScript by Marijn Haverbeke ebooks
PDF
100% (16)
[FREE PDF sample] (Ebook) Eloquent JavaScript by Marijn Haverbeke ebooks
65 pages
Functional vs. Procedural Vs
PDF
No ratings yet
Functional vs. Procedural Vs
6 pages
JS Advanced Notes
PDF
No ratings yet
JS Advanced Notes
557 pages
JavaScript and Node FUNdamentals 1st Edition by Azat Mardan ISBN B00HDYHKN6instant download
PDF
100% (5)
JavaScript and Node FUNdamentals 1st Edition by Azat Mardan ISBN B00HDYHKN6instant download
80 pages
Essential Fsharp
PDF
No ratings yet
Essential Fsharp
207 pages