11Programming Structure VII

11Programming Structure VII - Introduction] to Functions [...

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

View Full Document Right Arrow Icon
Introduction to Functions [ Let’s summarize and introduce more ] Why do we write FUNCTIONS? Better organization of code. Functions can be reused: call the same function from different parts. Eg. Add 5 marks as bonus to all subjects: then we can also call c o nve rtAll to update all grades. One single function can serve different sets of input data values. (just need to pass the input values through parameters whenever we call the function). Function Names Should be descriptive. Bad examples: Often composed of 2 or more words: first one is a verb, eg. c o nve rt All, s ho w De ta ils A common way: First word starts with lowercase, others start with uppercase. Some other constraints: CANNOT start with 0 - 9. CANNOT be a keyword. Eg. 9To5 and else are wrong Format of Function Definition : function function_name ( parameter-list ) { ... some code . .. (may or may not return a value) } To Call a Function: function_name(argument-list) function Function1 () { .. }  bad names (not meaningful) function temp () { .. }
Background image of page 1

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

View Full DocumentRight Arrow Icon
func tio n s ho wMultiple ( a ,b ) { ale rt( a * b ); } s ho wMultiple ( 10 , 20 ); Parameters / Arguments parameters The showMultiple function "sees" 10, 20 as the values of its parameters a, b . a*b is the product of a and b. arguments showMultiple is called with the arguments 10 and 20 . ü Parameters  m ake  a func tio n fle xible : C an wo rk fo r diffe re nt data. Arguments (eg. 10, 20 here) are the actual values that we pass to the function in the function call, that fit the function parameters (ie. a and b). Arguments and parameters are separated by commas. Introduction to Functions Parameter names (a and b) should appear in the function only. Arguments in the argument-list should match the parameter-list.
Background image of page 2
function calculateMultiple(a,b) { return (a*b); } do c um e nt.F1.X.value  = c alc ula te Multiple (10,20); ale rt(c alc ulate Multiple (11,22)); The return  statement The return keyword tells the browser to return a value from the function definiti on to the statement that called the function, eg. return 'A' ; After running the return statement, the function is stopped immediately , eve n if there exists any other statement below the return statement. Example:
Background image of page 3

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

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

This note was uploaded on 03/10/2012 for the course CS 1301 taught by Professor Dr.wong during the Winter '08 term at City University of Hong Kong.

Page1 / 11

11Programming Structure VII - Introduction] to Functions [...

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

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