1 / 16

Lecture 12: Dividing Up Work

Learn how to leverage divide-and-conquer techniques to create reusable functions, optimize code structure, and avoid repetition. Understand function headers, bodies, prototypes, and best practices for efficient program design.

Download Presentation

Lecture 12: Dividing Up Work

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. Lecture 12: Dividing Up Work

  2. Why Using Functions • Divide-and-conquer • making large program development more manageable. • Software reusability • Use existing functions as building blocks for new program • Avoid re-inventing the wheel • Avoid code repetition

  3. Example Given three edges a, b and c of a triangle, compute the angle 

  4. Function header Function Definitions return-value-type function-name( parameter-list ) { definitions statements } • function-name: any valid identifier - using a meaningful name. • return-value-type: data type of the result. • Default is int. • void - indicates that the function returns nothing • parameter-list: comma-separated list • Specifies the parameters received by the function when it is called. • void - indicates the function does not receive any values. • A type must be listed explicitly for each parameter, unless the parameter is of type int.

  5. Function header Function body Function Definitions return-value-type function-name( parameter-list ) { definitions statements } • definitions: declare local variables used inside the function. • Known only inside the function defined • Parameters are local variables. • statements: perform the task of the function. • Returning control • If nothing returned return; Or, until reaches right brace. • If something returned returnexpression;

  6. Function Prototypes • Prototype only needed if function definition comes after use in program • Format return-value-type function-name( parameter-list ); • Parameter names are not necessarily included in the function prototype. • A function prototype is used to validate functions • The type of data returned by the function • The number of parameters the function expected to receive • The types of the parameters • The order in which these parameters are expected. • Example double Square( double x );

  7. Functions Function can not defined inside other functions. The function prototype, function header and function calls should all agree in the number, type, and order of arguments and parameters, and in the type of return value. Each function should be limited to performing a single, well-defined task. Function name should effectively express that task.

  8. Flowcharting HW#3 begin Simulate the logical circuit. end

  9. Flowcharting HW#3 Simulate the logical circuit. Print the menu Input the switch to be closed Close the switch If the input is not “Exit”, illuminate the light bulbs input != “Exit”

  10. true Close switch1 break false true case switch2 Close switch2 break false true Close switch3 case switch3 break false true Close switch4 break false true Close switch5 break false case switch1 case switch5 case switch4 case Exit case switch6 true Close switch6 break false true Print “Thanks” break false default Print “Invalid switch” Close the switch

  11. If the input is not “Exit”, illuminate the light bulbs false input != Exit true Illuminate the red bulb Illuminate the blue bulb Illuminate the green bulb

  12. redBulb = switch1 && switch2 false redBulb true Print “Red” Illuminate the red bulb

  13. Illuminate the blue bulb xorA = switch3 || switch4; blueBulb = (!xorA && switch5) || (xorA && !switch5); false blueBulb true Print “Blue”

  14. Illuminate the green bulb false switch6 true Print “Green”

  15. Flowcharting HW#3

  16. Practice Question Solution: C

More Related