4. functions

4. functions - ENGG1002 Computer Programming and...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ENGG1002 Computer Programming and Applications Prepared by Dr. K.K.Y. Wong Top-Down Design A good way to design a program is to break down the task to be accomplished into a few sub-tasks Each sub-task can be further decomposed into smaller sub- tasks, and this process is repeated until all sub-tasks are small enough that their implementations become manageable This approach is called top-down design (a.k.a. divide and conquer ) 2 Generate the standard deviation Read in data Output the result to screen Compute standard deviation Compute mean Compute variance Compute square root Functions Preserving the top-down design structure in a program will make it easier to understand and change the program, as well as to write, test, and debug the program In C++, sub-tasks are implemented as functions A function is a group of statements that is executed when it is called from some point of the program E.g., the main function main() in previous examples A program is composed of a collection of functions When a program is put into execution, it always starts at the main function, which may in turn call other functions 3 Function Definitions A function is defined using a function definition which Describes how a function computes the value it returns Consists of a function header followed by a function body The function header specifies the type of the return value , the function name (identifier), and the list of parameters (with types and identifiers) The function body consists of variable declarations and executable statements enclosed within a pair of braces { } 4 Syntax type_ret func_name(type_1 par_1, type_2 par_2, …) { // variable declarations … // executable statements … } function header function body Function Definitions Example: 5 double f2c(double f) { // Fahrenheit to Celsius Conversion double c; c = (f – 32)*5/9; return c; } function name: f2c parameters: double f return type: double variable declaration executable statement return statement returns the specified value to the caller terminates the execution of the function function body function header Function Calls A function call (i.e., the process of calling a function) is made using the function name with the necessary parameters A function call is itself an expression , and can be put in any places where an expression is expected Example: Parameters vs arguments The parameters used in the function definition are called formal parameters or simply parameters . They are placeholders in the function The actual values passed to a function in a function call are referred to as actual parameters or arguments . They are the actual values used in the execution of the function to produce the return value 6 double Tc = f2c(98.6); Function Calls The arguments used in a function call can be constants , variables , expressions , or even functions , e.g., In using expressions as arguments, the expressions will be...
View Full Document

Page1 / 44

4. functions - ENGG1002 Computer Programming and...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online