javascript - What are the advantages of using arguments in functions? -
when define function why might want use argument / parameter? benefits of using arguments? how useful? explain? thanks.
var playlist = [ 'i did way', 'respect', 'imagine', 'born run', 'louie louie', 'maybellene' ]; function print(message) { document.write(message); } function printlist (list) { var listhtml = '<ol>'; (var = 0; < list.length; += 1) { listhtml += '<li>' + list[i] + '</li>'; } listhtml += '</ol>'; print(listhtml); } printlist(playlist);
why have arguments been used in example above?
arguments let apply same logic different data, meaning can re-use code.
if don't use arguments there limitation on re-usability of functions or methods, can produce same output repeatedly, same how asking question on , on again no new information give same answer.
in example, printlist
function written such that, if wanted, generate multiple outputs invoking multiple times different list
arrays, rather being able generate single output based on array called playlist
the print
function wrapper document.write
. there many reasons against using document.write
can worry after you've had practice writing code.
Comments
Post a Comment