160 likes | 182 Views
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.
E N D
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
Example Given three edges a, b and c of a triangle, compute the angle
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.
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;
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 );
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.
Flowcharting HW#3 begin Simulate the logical circuit. end
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”
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
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
redBulb = switch1 && switch2 false redBulb true Print “Red” Illuminate the red bulb
Illuminate the blue bulb xorA = switch3 || switch4; blueBulb = (!xorA && switch5) || (xorA && !switch5); false blueBulb true Print “Blue”
Illuminate the green bulb false switch6 true Print “Green”
Practice Question Solution: C