Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 12: Dividing Up Work. Why Using Functions Divide-and-conquer making large program development more manageable. Software reusability Use existing.

Similar presentations


Presentation on theme: "Lecture 12: Dividing Up Work. Why Using Functions Divide-and-conquer making large program development more manageable. Software reusability Use existing."— 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 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. Function header

5 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 return expression; Function header Function body

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 Close the switch case switch5 case switch1 true Close switch1 break false case switch2 true Close switch2 break false case switch3 true Close switch3 break false case switch4 true Close switch4 break false default Print “ Invalid switch ” true Close switch5break false case Exit false case switch6 true Close switch6 break false true Print “ Thanks ” break

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

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

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

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

15 Flowcharting HW#3

16 Practice Question Solution: C


Download ppt "Lecture 12: Dividing Up Work. Why Using Functions Divide-and-conquer making large program development more manageable. Software reusability Use existing."

Similar presentations


Ads by Google