Search results
Top results related to what is the difference between procedural and event driven programming?
Top Answer
Answered Aug 06, 2019 · 182 votes
A functional language (ideally) allows you to write a mathematical function, i.e. a function that takes n arguments and returns a value. If the program is executed, this function is logically evaluated as needed.1
A procedural language, on the other hand, performs a series of sequential steps. (There's a way of transforming sequential logic into functional logic called continuation passing style.)
As a consequence, a purely functional program always yields the same value for an input, and the order of evaluation is not well-defined; which means that uncertain values like user input or random values are hard to model in purely functional languages.
1 As everything else in this answer, that’s a generalisation. This property, evaluating a computation when its result is needed rather than sequentially where it’s called, is known as “laziness”. Not all functional languages are actually universally lazy, nor is laziness restricted to functional programming. Rather, the description given here provides a “mental framework” to think about different programming styles that are not distinct and opposite categories but rather fluid ideas.
Other Answers
Answered Nov 28, 2012 · 109 votes
I've never seen this definition given elsewhere, but I think this sums up the differences given here fairly well:
Functional programming focuses on expressions
Procedural programming focuses on statements
Expressions have values. A functional program is an expression who's value is a sequence of instructions for the computer to carry out.
Statements don't have values and instead modify the state of some conceptual machine.
In a purely functional language there would be no statements, in the sense that there's no way to manipulate state (they might still have a syntactic construct named "statement", but unless it manipulates state I wouldn't call it a statement in this sense). In a purely procedural language there would be no expressions, everything would be an instruction which manipulates the state of the machine.
Haskell would be an example of a purely functional language because there is no way to manipulate state. Machine code would be an example of a purely procedural language because everything in a program is a statement which manipulates the state of the registers and memory of the machine.
The confusing part is that the vast majority of programming languages contain both expressions and statements, allowing you to mix paradigms. Languages can be classified as more functional or more procedural based on how much they encourage the use of statements vs expressions.
For example, C would be more functional than COBOL because a function call is an expression, whereas calling a sub program in COBOL is a statement (that manipulates the state of shared variables and doesn't return a value). Python would be more functional than C because it allows you to express conditional logic as an expression using short circuit evaluation (test && path1 || path2 as opposed to if statements). Scheme would be more functional than Python because everything in scheme is an expression.
You can still write in a functional style in a language which encourages the procedural paradigm and vice versa. It's just harder and/or more awkward to write in a paradigm which isn't encouraged by the language.
Other Answers
Answered May 07, 2023 · 106 votes
Basically the two styles, are like Yin and Yang. One is organized, while the other chaotic. There are situations when Functional programming is the obvious choice, and other situations were Procedural programming is the better choice. This is why there are at least two languages that have recently come out with a new version, that embraces both programming styles. ( Perl 6 and D 2 )
Procedural:
- The output of a routine does not always have a direct correlation with the input.
- Everything is done in a specific order.
- Execution of a routine may have side effects.
- Tends to emphasize implementing solutions in a linear fashion.
Perl 6
sub factorial ( UInt:D $n is copy ) returns UInt {- # modify "outside" state state $call-count++; # in this case it is rather pointless as # it can't even be accessed from outside- my $result = 1;- loop ( ; $n > 0 ; $n-- ){- $result *= $n;- }- return $result;}-
D 2
int factorial( int n ){- int result = 1;- for( ; n > 0 ; n-- ){ result *= n; }- return result;}-
Functional:
- Often recursive.
- Always returns the same output for a given input.
- Order of evaluation is usually undefined.
- Must be stateless. i.e. No operation can have side effects.
- Good fit for parallel execution
- Tends to emphasize a divide and conquer approach.
- May have the feature of Lazy Evaluation.
Haskell
( copied from Wikipedia );
fac :: Integer -> Integer-fac 0 = 1fac n | n > 0 = n * fac (n-1)-
or in one line:
fac n = if n > 0 then n * fac (n-1) else 1-
Perl 6
proto sub factorial ( UInt:D $n ) returns UInt {*}-multi sub factorial ( 0 ) { 1 }multi sub factorial ( $n ) { $n * samewith $n-1 } # { $n * factorial $n-1 }-
D 2
pure int factorial( invariant int n ){ if( n <= 1 ){ return 1; }else{ return n * factorial( n-1 ); }}-
Side note:
Factorial is actually a common example to show how easy it is to create new operators in Perl 6 the same way you would create a subroutine. This feature is so ingrained into Perl 6 that most operators in the Rakudo implementation are defined this way. It also allows you to add your own multi candidates to existing operators.
sub postfix:< ! > ( UInt:D $n --> UInt ) is tighter(&infix:<*>) { [*] 2 .. $n }-say 5!; # 120-
This example also shows range creation (2..$n) and the list reduction meta-operator ([ OPERATOR ] LIST) combined with the numeric infix multiplication operator. (*) It also shows that you can put --> UInt in the signature instead of returns UInt after it.
( You can get away with starting the range with 2 as the multiply "operator" will return 1 when called without any arguments )
1/5
Top Answer
Answered Feb 11, 2021 · 5 votes
Event sourcing as an implementation strategy for the persistence of state, e.g. of aggregates. This strategy should not be exposed beyond the boundaries of aggregates. The events from event sourcing should therefore only be used internally in the corresponding aggregate or in the context of CQRS to build related read models.
Domain events, on the other hand, represent a specific fact or happening that is relevant regardless of the type of persistence strategy for aggregates, for example, for integrating bounded contexts.
Event sourcing and domain events can of course be used both at the same time, but should not influence each other. The two concepts are used for different purposes and should therefore not be mixed.
Please, read from the link below to learn more: check here
2/5
Top Answer
Answered Sep 10, 2018 · 127 votes
Imperative
There are several sub-paradigms of the imperative programming paradigm, such as the procedural or the object-oriented programming paradigms.
In the imperative programming paradigm, you describe the algorithm step-by-step, at various degrees of abstraction.
Examples of programming languages which support the procedural paradigm:
- C (and most other legacy languages)
- PHP, mostly
- In some sense, all major languages
Object-Oriented
It typically refers to languages that exhibit a hierarchy of types that inherit both methods and state from base types to derived types, but also includes the unusual prototype-based JavaScript.
Examples of programming languages which support the OO paradigm:
- Java
Declarative
There are several sub-paradigms of the declarative programming paradigm, such as the functional or the logic programming paradigms.
In the declarative programming paradigm, you describe a result or a goal, and you get it via a "black box". The opposite of imperative.
Examples of programming languages which support the declarative programming paradigm:
- yacc
- Treetop
- SQL
- Regular Expressions
- lex
- XSLT
- markup, troff, CSS, VHDL
Functional
Functional programming emphasizes the application of functions without side effects and without mutable state. The declarative systems above exhibit certain aspects of functional programming.
Examples of programming languages which support the declarative functional paradigm:
- Haskell
- OCaml
- Scheme
- Erlang
- F#
- Scala
Other Answers
Answered Nov 28, 2012 · 109 votes
I've never seen this definition given elsewhere, but I think this sums up the differences given here fairly well:
Functional programming focuses on expressions
Procedural programming focuses on statements
Expressions have values. A functional program is an expression who's value is a sequence of instructions for the computer to carry out.
Statements don't have values and instead modify the state of some conceptual machine.
In a purely functional language there would be no statements, in the sense that there's no way to manipulate state (they might still have a syntactic construct named "statement", but unless it manipulates state I wouldn't call it a statement in this sense). In a purely procedural language there would be no expressions, everything would be an instruction which manipulates the state of the machine.
Haskell would be an example of a purely functional language because there is no way to manipulate state. Machine code would be an example of a purely procedural language because everything in a program is a statement which manipulates the state of the registers and memory of the machine.
The confusing part is that the vast majority of programming languages contain both expressions and statements, allowing you to mix paradigms. Languages can be classified as more functional or more procedural based on how much they encourage the use of statements vs expressions.
For example, C would be more functional than COBOL because a function call is an expression, whereas calling a sub program in COBOL is a statement (that manipulates the state of shared variables and doesn't return a value). Python would be more functional than C because it allows you to express conditional logic as an expression using short circuit evaluation (test && path1 || path2 as opposed to if statements). Scheme would be more functional than Python because everything in scheme is an expression.
You can still write in a functional style in a language which encourages the procedural paradigm and vice versa. It's just harder and/or more awkward to write in a paradigm which isn't encouraged by the language.
Other Answers
Answered May 07, 2023 · 106 votes
Basically the two styles, are like Yin and Yang. One is organized, while the other chaotic. There are situations when Functional programming is the obvious choice, and other situations were Procedural programming is the better choice. This is why there are at least two languages that have recently come out with a new version, that embraces both programming styles. ( Perl 6 and D 2 )
Procedural:
- The output of a routine does not always have a direct correlation with the input.
- Everything is done in a specific order.
- Execution of a routine may have side effects.
- Tends to emphasize implementing solutions in a linear fashion.
Perl 6
sub factorial ( UInt:D $n is copy ) returns UInt {- # modify "outside" state state $call-count++; # in this case it is rather pointless as # it can't even be accessed from outside- my $result = 1;- loop ( ; $n > 0 ; $n-- ){- $result *= $n;- }- return $result;}-
D 2
int factorial( int n ){- int result = 1;- for( ; n > 0 ; n-- ){ result *= n; }- return result;}-
Functional:
- Often recursive.
- Always returns the same output for a given input.
- Order of evaluation is usually undefined.
- Must be stateless. i.e. No operation can have side effects.
- Good fit for parallel execution
- Tends to emphasize a divide and conquer approach.
- May have the feature of Lazy Evaluation.
Haskell
( copied from Wikipedia );
fac :: Integer -> Integer-fac 0 = 1fac n | n > 0 = n * fac (n-1)-
or in one line:
fac n = if n > 0 then n * fac (n-1) else 1-
Perl 6
proto sub factorial ( UInt:D $n ) returns UInt {*}-multi sub factorial ( 0 ) { 1 }multi sub factorial ( $n ) { $n * samewith $n-1 } # { $n * factorial $n-1 }-
D 2
pure int factorial( invariant int n ){ if( n <= 1 ){ return 1; }else{ return n * factorial( n-1 ); }}-
Side note:
Factorial is actually a common example to show how easy it is to create new operators in Perl 6 the same way you would create a subroutine. This feature is so ingrained into Perl 6 that most operators in the Rakudo implementation are defined this way. It also allows you to add your own multi candidates to existing operators.
sub postfix:< ! > ( UInt:D $n --> UInt ) is tighter(&infix:<*>) { [*] 2 .. $n }-say 5!; # 120-
This example also shows range creation (2..$n) and the list reduction meta-operator ([ OPERATOR ] LIST) combined with the numeric infix multiplication operator. (*) It also shows that you can put --> UInt in the signature instead of returns UInt after it.
( You can get away with starting the range with 2 as the multiply "operator" will return 1 when called without any arguments )
3/5
Top Answer
Answered Jul 08, 2009 · 9 votes
How's about this for a succinct description:
Procedural Programming is primarily organized around "actions" and "logic". OOP is primarily organized around "objects" and "data". OOP takes the view that what we really care about are the objects we want to manipulate rather than the logic required to manipulate them.
4/5
Top Answer
Answered Aug 06, 2015 · 110 votes
The event target is the element to which the event is dispatched:
The object to which an event is targeted using the DOM event flow. The event target is the value of the Event.target
attribute.
srcElement is a IE non-standard way to obtain the target.
The current event target is the element which has the event listener which is currently invoked:
In an event flow, the current event target is the object associated with the event handler that is currently being dispatched. This object MAY be the event target itself or one of its ancestors. The current event target changes as the event propagates from object to object through the various phases of the event flow. The current event target is the value of the Event.currentTarget
attribute.
Using this inside an event listener is a common (and standard) way to obtain the current event target.
Some kind events have a relatedTarget
:
Used to identify a secondary EventTarget
related to a UI event, depending on the type of event.
fromElement and toElement are IE non-standard ways to obtain the relatedTarget.
5/5
documentation.progress.com › output › uaProcedure-driven and Event-driven Programming - Progress.com
documentation.progress.com › output › uaEvent-driven programming defines the programming process as the development of procedures that respond to the flow of data and control as directed by the user, program, or operating system. These programming models differ in flow of execution and structure.
www.geeksforgeeks.org › what-is-the-event-drivenWhat is the Event Driven Programming Paradigm - GeeksforGeeks
www.geeksforgeeks.org › what-is-the-event-drivenFeb 2, 2024 · Event-driven programming is a paradigm where the execution of a program is determined by events such as user actions or messages. Programs respond to events with predefined actions, allowing for asynchronous and responsive behavior, often seen in GUI applications and distributed systems. Advantages of Event-Driven Programming Paradigm
People also ask
What is the event driven programming paradigm?
- What is the Event Driven Programming Paradigm ? Event-driven programming is a paradigm where the execution of a program is determined by events such as user actions or messages. Programs respond to events with predefined actions, allowing for asynchronous and responsive behavior, often seen in GUI applications and distributed systems.
What is the Event Driven Programming Paradigm - GeeksforGeeks
www.geeksforgeeks.org/what-is-the-event-driven-programming-paradigm/What is event-driven programming?
- Event-driven programming defines the programming process as the development of procedures that respond to the flow of data and control as directed by the user, program, or operating system. These programming models differ in flow of execution and structure. In addition, each model works best with a particular programming environment.
Procedure-driven and Event-driven Programming - Progress.com
documentation.progress.com/output/ua/OpenEdge_latest/gsdev/procedure-driven-and-event-driven-programming.htmlWhat is the producer-consumer pattern in event-driven programming?
- The producer-consumer pattern plays a fundamental role in event-driven programming. Producers generate events, and consumers subscribe to them, taking action upon their occurrence. An event broker/queue mediates between producers and consumers, guaranteeing reliable event delivery.
Event-driven programming—a deep dive into event-based architectures
redpanda.com/guides/event-stream-processing/event-driven-programmingWhat is a callback function in event-driven programming?
- In the event-driven programming paradigm, we are using callback functions. This entails defining functions to handle specific events, such as button clicks.
What is the Event Driven Programming Paradigm - GeeksforGeeks
www.geeksforgeeks.org/what-is-the-event-driven-programming-paradigm/redpanda.com › event-driven-programmingEvent-driven programming—a deep dive into event-based ...
redpanda.com › event-driven-programmingEvent-driven programming. Events are discrete incidents capturing state changes in systems. These events are generated by event sources, which encompass a broad spectrum, ranging from user interfaces to IoT devices and serverless functions. Event-driven programming is a paradigm in which program execution is governed by such events, as opposed ...
www.quix.io › blog › what-why-how-of-event-drivenThe what, why and how of event-driven programming
www.quix.io › blog › what-why-how-of-event-drivenOct 11, 2023 · Event-driven programming (EDP) is a programming paradigm where external events determine the flow of program execution. These events come in different shapes: user actions (e.g., button clicks, keyboard inputs), system events (like a finished file download), messages from other programs, sensor outputs, etc.
en.wikipedia.org › wiki › Event-driven_programmingEvent-driven programming - Wikipedia
en.wikipedia.org › wiki › Event-driven_programmingIn computer programming, event-driven programming is a programming paradigm in which the flow of the program is determined by external events. Typical event can be UI events from mice, keyboards, touchpads and touchscreens, or external sensor inputs, or be programmatically generated ( message passing) from other programs or threads, or network ...
stackoverflow.com › questions › 28354991What is the advantage to using an event-driven approach vs ...
stackoverflow.com › questions › 28354991So, really, both processes you've described are event-driven. The difference between the two examples is that by introducing the publish / subscribe abstraction between the "observer" object and the "responder" objects you are, as D Stanley mentions, decoupling the two layers by adding a layer of indirection.
medium.com › java-content-hub › the-ultimate-guideThe Ultimate Guide to Event-driven Programming in Java
medium.com › java-content-hub › the-ultimate-guideJan 31, 2024 · Event-driven programming is one of the most popular and widely used programming paradigms in the world, with 69.4% of developers using it in some form or another. It enables applications to be ...
Searches related to What is the difference between procedural and event driven programming?