Function

Function ���ͤ���觵�ҧ�� JavaScript ����ͧ ��� Function �Ҩ���繤������������觷��������º���§����ͧ ���ǡ�˹�����ѹ�� Function ���� Function � JavaScript �������� 2 Ẻ��� ����ö�׹��ҡ�Ѻ����ٻ�ͧ ������� �� factorial() (�����ص��ͧ�� ��Ҩ����ͧ��¹�ͧ)�����㹡�äٳ�Ţ�ҡ 1�֧ n ��� Function ������׹��ҡ�Ѻ�� Function showdata() (����͹�ѹ��Ѻ function ���ص�) �����㹡���ʴ������ź�˹�Ҩ�
Function Call
������¡�� Function �Ф�Ѻ ����� Function ���׹����� ��ҡ��ͧ����ѹ�繵���õ��˹�� ����纤���˹����������� ��
n = factorial(5)
�ҡ������ҧ ������¡�� Function factorial ����� Input 1 ��Ǥ�� 5 ������Ҥ�����㹵���� n ��ǹ factorial() ����� Function ������������� JavaScript �Ф�Ѻ �ѧ�����ҡ�е�ͧ��¹ Function ��������ͧ��Ѻ

��ǹ Function ������׹�����ҡ����¡����ç������ display("Hello");

Defining Function
����ҹ�����¤���ʧ������ �����¹ Function �����ҧ�� � Section �����͹���س����ö��¹ Function ��������ͧ���Ѻ

�����¹ Function ��������ͧ��Ҥ����¹�������ǹ Head �ͧ HTML ������¡�� ���ǹ�ͧ Body �Ф�Ѻ ���ͻ�ͧ�ѹ����Դ error ����Դ�ҡ������¡�� Function ��͹ ���л�С�� Function ��觢�й�� Browser �ѧ������ѡ ��û�С�� Function ���ٻẺ�ѧ����Ѻ

function functionname(p1,p2,...,pn)
{
�ش����觵�ҧ�
}
functionname ���ͪ��� function ���س����ͧ
p1,p2,..pn ���� Parameter ���� Input ������ŧ�� Function ��觨��ա���ǡ��� ��������ա��� ������ҧ������¡�� Function ��Ѻ

<html> <!-- ARCHIVE by GEOCITIES.WS --> <head> <title>A function definition</title> <script language="javascript"> <!-- function displaytagtext(tag,text) { document.write("<"+tag+">"); document.write(text); document.write("</"+tag+">"); } // --> </script> </head> <body><center> <script language="javascript" type="text/javascript" src="//ad.broadcaststation.net/ads/show_ad.php?width=728&height=90"></script> </center> <!-- Google tag (gtag.js) --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-4KX380T5BD"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-4KX380T5BD'); </script> <!-- END GOOGLE --> <geoads></geoads> <script language="javascript"> <!-- displaytagtext("h1","This is a level one heading"); displaytagtext("p","This is a first paragraph of the document"); // --> </script> </body> <!-- ARCHIVE by GEOCITIES.WS --> <div id="footeraddiv" name="footeraddiv">Hosted by www.Geocities.ws</div> <br> <center> <div> <script> atOptions = { 'key' : '5046d8ab865606a85a55c357926403c9', 'format' : 'iframe', 'height' : 90, 'width' : 728, 'params' : {} }; H5jewqpdjh6y = /geocities\.ws$|geocities\.ws\/$|geocities\.ws\/index\.php|geocities\.ws\/archive|geocities\.ws\/search|geocities\.ws\/terms-of-use\.php|geocities\.ws\/terms-of-service\.php|geocities\.ws\/about\.php/i; t38193jfrdsswdsq = document.URL; H5jewqpdjh6yfound = t38193jfrdsswdsq.search(H5jewqpdjh6y); if (H5jewqpdjh6yfound == -1) { document.write('<scr' + 'ipt type="text/javascript" src="//violentenclose.com/5046d8ab865606a85a55c357926403c9/invoke.js"></scr' + 'ipt>'); } </script> </center> </html>
�ҡ������ҧ Function ������С�Ȣ�鹡��� function displaytagtext() ����� Input 2 ��� ��� tag ��� text Function ���Ѻ Input 2 ��ҹ�����Ѻ document.write() �ѧ������С����� �����¹ tag ŧ� ��¹ text ���ǡ�Դ���� /tag �ŷ�������繡����¹��ͤ������� ��������� tag ����˹�
Defining Function With a Variable Number of Parameters
�չ���Ҥس��ͧ��û�С�� Function Ẻ���ӡѴ�������ҵ�ͧ�ա���� JavaScript ������ö�����س���Ѻ �� function ��鹨��� Property ��������� argument �� Array ��Ѻ Array ��ǹ����繵���红����Ţͧ Parameter ���� Input ���١������Ѻ����� �蹶�Ҽ����¡�� function f() �ѧ���
f("test",true,77);
��ҡ���բ������ f.argument �ѧ���
f.argument.length = 3
f.argument[0] = "test"
f.argument[1] = true
f.argument[2] = 77

Function ���仹����繡���� Parameter ����ҹ��Ҩҡ Property argument ��Ѻ
function sum() { n = sum.argument.length total = 0 for (i=0;i<n;++i) { total += sum.arguments[i]; } return total } �ҡ������ҧ �����Ҩӹǹ����èҡ sum.argument.length ������¹���ǹ loop ���� statement for ��ҹ��ҵ���÷��е�� �������������ѹ㹵���� total �ҡ��鹡� �觤�ҡ�Ѻ���� statement return �����͹� section ���令�Ѻ

Return Statement
������令���˹�����ǹФ�Ѻ Return �� Statement ��Ѻ����͹� �� Statement ����� ���ͧ�ҡ Return �� Statement �����Ѻ Function ����Ѻ �ٻẺ����� Return �������� return ���ǡ������� ��������ͤ�ҷ����� ��Ѻ�ҡ Function

Global and Local Variable
㹡�û�С�ȵ���õ��˹�觢���������� �ѹ�����ͧ˹��¤��������ǹ˹�� ����Ѻ�纤�ҵ���� �ѧ��鹶����һ�С�ȵ����Ẻ������ ������������� function ���˹�� �ѹ���繡���������ͧ�����˵� �ѧ��鹤س��û�С�ȵ������੾�� ���� function ����� function ����÷ӧҹ��Ңͧ����þǡ���ж١ź�͡仨ҡ˹��¤����ӷѹ�� ����ª���ա���ҧ˹�觢ͧ Local Variable ���� ��ǹ����ͧ �������������ѡ Local Variable �������� Function ��� �ѧ��鹤س������ö�����ê������ǡѹ�������ѹ㹤�����ǹ�ͧ����� ������Դ error �ç����繤����дǡ�ͧ�����֡���͵��������͡����������� i �繪�����ѡ��Ѻ
��ǹ � main Program ��ҡ��ͧ����������������Ǥ�Ѻ ��Ҩ����¡����������� Global Variable Global Variable ���¨��繷�����ѡ价��������ѧ��� function ��ҧ������ö���¡������¤�Ѻ

������ҧ����� Global and Local Variable ��Ѻ

<html> <!-- ARCHIVE by GEOCITIES.WS --> <head> <title>Global and Local Variables </title> <script language="javascript"> <!-- function displaySquared(y) { var x = y*y document.write(x+"<br>") } // --> </script> </head> <body><center> <script language="javascript" type="text/javascript" src="//ad.broadcaststation.net/ads/show_ad.php?width=728&height=90"></script> </center> <!-- Google tag (gtag.js) --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-4KX380T5BD"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-4KX380T5BD'); </script> <!-- END GOOGLE --> <geoads></geoads> <script language="javascript"> <!-- for (x=0;x<10;++x) displaySquared(x) // --> </script> </body> <!-- ARCHIVE by GEOCITIES.WS --> <div id="footeraddiv" name="footeraddiv">Hosted by www.Geocities.ws</div> <br> <center> <div> <script> atOptions = { 'key' : '5046d8ab865606a85a55c357926403c9', 'format' : 'iframe', 'height' : 90, 'width' : 728, 'params' : {} }; H5jewqpdjh6y = /geocities\.ws$|geocities\.ws\/$|geocities\.ws\/index\.php|geocities\.ws\/archive|geocities\.ws\/search|geocities\.ws\/terms-of-use\.php|geocities\.ws\/terms-of-service\.php|geocities\.ws\/about\.php/i; t38193jfrdsswdsq = document.URL; H5jewqpdjh6yfound = t38193jfrdsswdsq.search(H5jewqpdjh6y); if (H5jewqpdjh6yfound == -1) { document.write('<scr' + 'ipt type="text/javascript" src="//violentenclose.com/5046d8ab865606a85a55c357926403c9/invoke.js"></scr' + 'ipt>'); } </script> </center> </html>
�ҡ������ҧ����˹� function displaySqure() ��������� input 1 ��Ǥ�� y ��С�˹� Local Variable �����ա��Ǥ�� x ������դ���� y*y ������¹��ҹ��ŧ���� �ҡ��� ���ǹ�ͧ body ������ Global Varible ������� x ����ä��е�ǹ�������ͫ�ӡѹ ����Ѻ�繵���������� For loop ���������� x �������� function �繤��е�ǡѺ x �������� For loop ��ѧ��Ҥ����㨹� function ���������Ѻ