Systematic Program Design for function
Systematic Program Design for Function
To design a function, we need to do following things:
-
Signature Firstly we need to know what kind of data type the function consumes and what of data it results in. In the conditions where it is unsure of result data type, we can use
voidas data type. -
Produce This is just the pseudo-code of what the problem expects the result to be.
-
Example Write example of what the function should be like.
-
Templates and constants Make a template or constants that a function would take as a parameter and code the body of the function.
-
Test and debug Lastly, test and debug the function and see if it does what it is expected to do.
As a basic example, suppose we want to write a function that takes a number as a parameter and returns the number plus 1.
// 1. signature
// Number --> Number
// We have a problem that takes number and results in
// number data type. Can be specific and say `int`
// 2. produce
// To get the number plus 1, we need to add 1 to the given number.
// 3. example
// function addOne(parameter_number){
// ...result...
// }
// 4. template and constants
// function addOne will take num as a parameter
// function addOne(num){
// num + 1; // result
// }
// 5. test and debug
// here we write what our function is and what expected result it should give.
function addOne(num){
return num + 1;
}
addOne(1); // 2
addOne(92); // 93
addOne(76399); // 76400In the above code block we can see how to design a function systematically.