Procedural vs. Event-Driven Programming PDF
Document Details
Uploaded by Deleted User
Tags
Summary
This document compares procedural and event-driven programming approaches. It explains how event-driven programs respond to user actions, highlighting the dynamic nature of this programming style, and contrasting it with the sequential structure of procedural programming. This approach is relevant to computer programming and software development.
Full Transcript
Procedural vs. Event driven Programming Procedural programming is a programming paradigm that uses a linear or top- down approach. It relies on procedures or subroutines to perform computations. It is also known as imperative programming. An event-driven program is one that largely re...
Procedural vs. Event driven Programming Procedural programming is a programming paradigm that uses a linear or top- down approach. It relies on procedures or subroutines to perform computations. It is also known as imperative programming. An event-driven program is one that largely responds to user events or other similar input. The concept of event-driven programming is an important one in application development and other kinds of programming, and has spawned the emergence of event handlers and other resources. An event-driven program is also known as an event-driven application. Most modern applications are interactive, responding to when users press a key on a keyboard, click buttons, type in a text-box, tilt the screen, swipe between screens, etc. In every instance, the user's action is generating some kind of event and the program responds by running an associated block of code. Programming a modern application is therefore typically an exercise in creating a user interface and then defining what will happen when the user interacts with that interface. The "event-driven" mindset of programming can take a little getting used to. Often when learning, you write sequential programs that run from the start (usually the first line of the program) to the end, or to some point when the program terminates. In an event-driven program your code must always be ready to respond to user events, like clicking a button, which may happen at any time, or not at all. More complex event-driven programs require interplay and coordination between so- called "event handlers" - which are actions that the programmer writes, but are triggered by the system in response to certain events. Modern programming is often event-driven rather than sequential. Sequential programs start at a beginning point and progress to the end point in a way that can be perfectly predicted when the program begins running. Event-driven programs on the other hand do not progress in a predictable order. User-generated events (e.g. mouse clicks, button press, key presses, etc.) are each handled individually. For example, a program may have two buttons, one to start a timer and one to stop the timer. In the program's code each button would have a different block of code to implement the separate and different action that the user expects the button to produce. Event-driven programs like these are dynamic, but also bring challenges since you don't know the order in which user-events may occur. This can lead to unpredictable program execution and another set of challenges. What events do familiar apps use to be interactive? Modern apps are interactive because they can respond to this and other forms of user input (i.e., human-generated events). Examples of these events include: swiping a screen clicking a button dragging your finger tilting a phone or tablet pressing a key, etc. What is event-driven programming? Have you ever clicked a link on a web page, or a button in an application, and nothing happens? It can be very frustrating when a component doesn't perform as expected. When we develop applications in any language, it is important that user interaction actually does something. Whenever you take action on a component, an event occurs. Whether anything happens after that event is up to you, the programmer, to determine. If you have actions defined for these events, then you have created an event-driven program or application. Event-driven programming is a programming paradigm in which the flow of program execution is determined by events - for example a user action such as a mouse click, key press, or a message from the operating system or another program. An event-driven application is designed to detect events as they occur, and then deal with them using an appropriate event-handling procedure. Event-driven programs can be written in any programming language, although some languages(Visual Basic for example) are specifically designed to facilitate event-driven programming, and provide an integrated development environment (IDE) that partially automates the production of code, and provides a comprehensive selection of built-in objects and controls, each of which can respond to a range of events. Virtually all object-oriented and visual languages support event-driven programming. Visual Basic, Visual C++ and Java are examples of such languages. A visual programming IDE such as VB.Net provides much of the code for detecting events automatically when a new application is created. The programmer can therefore concentrate on issues such as interface design, which involves adding controls such as command buttons, text boxes, and labels to standard forms (a form represents an application's workspace or window). Once the user interface is substantially complete, the programmer can add event- handling code to each control as required. Key Features of Event driven Programming Service Oriented Service oriented in event driven programming is a key feature that takes very little of the computers processing power so it does not slow down the computer. Services run in the background of your OS. It has services that make your computer system user friendly. An example of service oriented is when you plug in an external device like a USB flash drive. A service is run by the operating system to install the necessary drivers needed for it to be used with the computer system. Time Driven The time driven feature in event driven programming runs a specific code on a timely basis like once an hour, once a week or once a month, meaning it is pre- set to do a task on a time trigger. An example for this feature is the OS update in your computer system can be set to check for updates once a month or the anti- virus program set to run a virus scan every day. Event Handlers Event handlers in event driven programming are designed to run a specific action when a specific event is triggered. There are numerous event handlers. An example of some are the keys in your keyboard when the letter "t" is clicked while in Word document it sends a signal to input that letter. Also the "Ctrl + Alt + Del" buttons are clicked together. It sends a signal to start up task manager on your system. Mouse movements trigger a software routine that processes its actions to what the cursor is doing on the display. Trigger Functions Trigger functions in event driven programming are mechanisms that decide what code to run when a specific event occurs. Trigger functions are used to select which event handler to use for the specific event that has occurred. Many devices have trigger functions with a specific event that runs for it. For example, automated doors have a sensor that when someone is in proximity of the sensor, it sends a signal to open the doors. Events Mouse events- Moving a mouse in certain directions causing cursor to respond to the movements on the display. Scroll button up down and click. Left button click, right button click, double click. All these triggers have a specific event tied to them. Keyboard Events- Key press, key down, key up- each key has a preset event in the keyboard. Key down can be used in a computer game to make your character move continuously. Forms / User Interface- Forms are objects like the windows that open when a certain application is activated. For example, "My Computer", an internet browser and all the windows that could pop up for a specific application. User interface is important to make the user be able to understand what he sees and able to easily access certain functions. A user interface needs to be clear and user friendly. External Events- occur when a user specifies an application to run another application. Simplicity of Programming It is simple to program an application using event driven programming because it is easy to insert ready coded scripts into an existing application because you are able to pause the code while it is running, saving you from having to start from the beginning. Ease of Development It is easy to develop using event driven programming because of certain debugging tools and being able to test the application by skipping certain parts of code. For example, you would like to test if a device will be able to read the data of a debit card and process a transaction. You will be able to skip the part where the card actually gets charged and complete the programs cycle.