0% found this document useful (0 votes)
7 views8 pages

PPL 3 Unit

The document discusses subprograms, highlighting their benefits such as modularity, reusability, abstraction, and readability. It also covers coroutines, which allow for cooperative multitasking, and generic subprograms that enable code to operate on different data types without rewriting. Additionally, it touches on operator overloading to enhance code readability and functionality.

Uploaded by

Akash Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
7 views8 pages

PPL 3 Unit

The document discusses subprograms, highlighting their benefits such as modularity, reusability, abstraction, and readability. It also covers coroutines, which allow for cooperative multitasking, and generic subprograms that enable code to operate on different data types without rewriting. Additionally, it touches on operator overloading to enhance code readability and functionality.

Uploaded by

Akash Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 8
eee Pane Pee ee eer OR ea Oana ene Te ed Pee) RIOR DEE): a au re CROLL A subprogram, often also called a function, procedure, or method, isa self-contained block of code designed to See ee ae se a ne eed problems into smaller, manageable pieces, making code easier to understand, write, and maintain, Cee 1, Modularity: Subprograms promote modularity by allowing a program to be divided into independent modules Each module can be developed and tested separately. 2, Reusability: Once defined, a subprogram can be called and executed multiple times from different parts of, Ce ee ee ee 3, Abstraction: Subprograms hide the internal details of how a task is performed. Users only need to know what the subprogram does and how to calli, not the exact steps it takes. Ee ee ne een eee ney long ass interface (inputs and outputs) remains consistent. This makes maintaining and updating code much ca 5, Readability: Breaking down a large program into smaller, named subprograms improves the overall readability Ee oe eee acs 6, Parameterization: Subprograms can accept input values (parameters or arguments) and often return output values, making them flexible and adaptable to different scenarios. EC Rea Ie Tec See cure eet ec eee) LET) Deen eC ec cee ees ee ec uEy ‘subprogram (function or method) and potentially back. Different methods have different implications for how the ‘tina datas affected. The mest common methods are: ead Te eRe ee a a ee Bee ee ett he calling scope. Ce Bee eee one eon ened Deion ox o Pott esters SoC treet acetone nay eet Poe eee at ee aero Senet etre ee teenie eee ce Tee ake ESCO e TT cea eerie Pee aces c rere es Prt aeos Varese Crescent e Rm CTE Sea errces ny ore eet ae meester tees eer) ean 7 Cron ae ee eae Pee teste ee Tater Pert rete ere Sars ey CU Grog Python oO Caen roc cel sac TOES ee ee CN Ror ceva amare Sstatasc rst eau cats Mt Lee sm cre ELT i) Cote ear) estate correo ve mkt ee See S USEC eSC Uva Ch) increment_by value (original num) strata Vac sea est eS CeO Eos recy To Estreemen emcee! roy Cece atest eee eek) Perec ee at stm hee sak eC ase) Nooeen Steen eee TSC) (ote: Python's behavior for immutable types hike integers, strings, and tuples resembles pass by value. For ‘mutable types like lists and dctonaries, i's more hike pass by abject reference.) ore ad Breen Ce he et ue ny et nee Ee Tad eae Bete ne gee a Ree Cea eos + When to use: When you want the subprogram to modify the original data, orto avoid copying large data Benes Cane Ge fe finclude Stes eos eT original Pre C/A eas ost emg ete ecco Sct ee aL cece) cre oreo FroeSst ren wey) ecrreents Doe este eee sect ee Ost nities nti eS ee eet st eee Breet sees tac sae ccs create ar rcs) est rerel ie otc Output: Pec ese eS e esa creo Ea) reese site Sane Nec ttcet eee Smt Ce Gn ee en ee a Python oO Carson mwa ase PNaSERTaeT nthe recreates rat Sone C eet eee a Stee eee CeCe) original list = [1, 2, 3] eater ce anata ORR een eS eee Coca cece meres) append_to_list (original_list) sata aca rete Tea ser treme ets bo ie Mata s by Object Reference) : feet roy CeCe eee a See cee ee Eee Perec Oeste CoM eso Soa ec Pee eae] eevee Metre Te Rs Seal Croc Pace ee] Ot el dee ae ee eee eg Sea ee ee ete Se ee tod Pee Ce eu Tee Te et oe Re Re Cee oat econ Be ne nee to the formal paremeter, only the last one matters Sn ee nee uae Ste eee Cee eee ne MeL ud anand returns multiple values as a tuple to achieve a similar effect if you need to “output” several results.) ea Cac aa Te en aR when the subprogram completes, the final value ofthe formal parameter is copied back to the actual Fee) See Cee oe Ce ee ee ee Loos ee cea eens Cree Ee Og ee eee eer correctness, and optimizing performance. peer TT) 3{(e) Write short notes on: (i) Coroutines (i ret) Here are short notes on the requested topics: Cet rity emery Concept: Coroutines are specialized functions that allow for cooperative multitasking. Unlike regular functions, which run to completion once called, coroutines can suspend their execution at certain points and resume later from where they eft off. This “pausing and resuming” capability allows them to yield control to another coroutine, ‘enabling efficient and flexible control flow, especially in asynchronous programming. Pome caniod + Yielding: Coroutines use a yi ld mechanism (or similar keyword) to temporarily suspend their execution Ce TC Caen OR Trad BCC Le un Se oa uC + Cooperative: They explicitly decide when to yield control, meaning they cooperate with other coroutines Dee ee eo + Stateful: They maintain ther local variables and execution state across multiple cals. Use Cases: + Asynchronous VO: Handling network requests or file operations without blocking the main program. «+ Event-driven programming: Processing events in a sequential manner while allowing other events to be force! + Generators: In some languages, coroutines are the underlying mechanism for implementing generators, which produce @ sequence of values on demand. Example (Python - using a simple generator as a coroutine): Python oO Pato C meant i) PS hia reset scree rat) eee rr Rsstr secs rue ieel Penatar ote eee) y = yield ” print (f"Received y: {y) Seats ay outine = simple « Pence acc) c c rt art 1 Peasy 7 z 7 ana aes oeeetTto) F r Patan eee) nt ived Cae estat acoustsc Oster Rte essa SCCoR CACO) . (ii) Generic Subprogram (Generics/Templates) Ce eee ee eee eo eee te that can operate on different data types without being rewritten for each type. I's a powerful feature that promotes code reusability and type safety. Instead of writing separate functions for, say, sorting integers, sorting loating- point numbers, and sorting strings, you can write one generic sort function that works forall these types. Cre cid + Type Parameterization: They use type parameters (placeholders for actual data types) in their definition. Be ee ieee ae Lee eee Cond es + Type Safety: The compiler or interpreter performs type checking at compile time (or runtime for some languages), ensuring that operations are valid forthe specific types used DTP ie. Ee ee eS Cee my Use Cases Ree eee oe ee ew cy Cron + Sorting and Searching Algorithms: Writing algorithms that can sort or search collections of various data Dod + Mathematical Operations: Creating functions that perform operations on different numeric types. en meee ene Gs o Pree es ttace es freee nests eee eee oe ae : Pst cae | Parte aCe) Tran re fon ene pea eest Cr std::cout << "Max of 5 and 10: " << maxInt << std::endl; a. Corot renin staat sae ee each cleanest Sak ace WC a eo es Carrere t ey ois 1 Petre std::string strl eee tree en Seer std:tstring maxString - findMax(str1, s eet eeea saan SCE tetra pores c 7 renee cer) Coe ete eae ea ec Den ee eee a eee eee ee functions to naturally work with different types as long as the operations are supported. Python oO 7 an 7 r 7 Coast t we pate are eee Peer ttsee str sae et) Sea eae eC ees i) Porawrotr ee str vice earn) Searcy aerate ee eee scr ts io) poe eee Pree ce esc rata arene CEC SELLY rarest c Cee eee te uo eke ea Cec EC et ech CeO uae ‘operator perform a specific action when applied to objects of your custom type, making the code more intuitive Se Sou nL Le a kas ere Ei fad sad Cy foment + Syntactic Sugar: it makes code more natural and readable by using familar operator symbols for custom era + Applicability: Applies only to existing operators; new operators cannot be created. + Context-Dependent: The behavior of an overloaded operator depends on the types of the operands i's Sony ON eee ee ec eee ay Cree Cn Re sc emaTaas) >: Defining adcition, subtraction, multiplication for custom numeric types (e.g. complex De Se ee + Stream V0: Overloading << (insertion) and >> (extraction) operators for custom object printing and cet ee es ae Cee ac Ce ee ee ae Os fa] Fresneee eres Ceca sua rset Tae PoE er Se ee PaCS ee Pay nese ea Panacea ee ae nee cee meee Pee st twat ame aie) H aR eS ere eae eT Serres se eee ee eva Posy Carte ara a oes ta Peon H Ye int main() Point p1(10, 20); CSUs SOE atte ee See ae CSL Portree eee Seas EET Tne} tee aes 7 Paar eee ery crete ee Se een Cac Te ee eee Poor Example (Python - inherent method overriding for similar behavior): Deno ee tt ee ee Ce Pe es ec ee ee ee a ect Ce tea ore o Sere stras amet CNC Stee See) tes Pre) # Overloading the '+' operator via the __add__ method Came MC STS) Gf isinstance(other, Point): Pete Sta er tt ears s eee eames ae) else: Se te oe eC Re Ste ce OC) Coste esse Sori sC eer SSeSte] Comm c ots Protea OtSo De see ELAR Cis Potato) Point (5, 15) CeO METS rect See eer eed print (£"p2: (p2}") print (£"P1 + P2 = {p3)") ey Se eae ad end wa

You might also like