cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Browse apps to extend the software in the new JMP Marketplace
%3CLINGO-SUB%20id%3D%22lingo-sub-49052%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%C3%89crivez%20vos%20propres%20fonctions%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-49052%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CDIV%20class%3D%22lia-message-template-problem-zone%22%3E%0A%3CH2%20id%3D%22toc-hId--1330307000%22%20id%3D%22toc-hId-1617150400%22%20id%3D%22toc-hId-1617150400%22%3EProbl%C3%A8me%3C%2FH2%3E%0A%3CP%3EVous%20souhaitez%20appeler%20du%20code%20depuis%20plusieurs%20endroits%2C%20peut-%C3%AAtre%20avec%20des%20arguments%20diff%C3%A9rents.%3C%2FP%3E%0A%3C%2FDIV%3E%0A%3CDIV%20class%3D%22lia-message-template-diagnosis-zone%22%3E%0A%3CH2%20id%3D%22toc-hId--442803319%22%20id%3D%22toc-hId--935006561%22%20id%3D%22toc-hId--935006561%22%3ESolution%3C%2FH2%3E%0A%3CP%3EUtilisez%20une%20fonction%20%C3%A9crite%20par%20l'utilisateur%2C%20avec%20des%20param%C3%A8tres%2C%20des%20variables%20locales%20pour%20les%20%C3%A9loigner%20des%20autres%20codes%20et%20une%20instruction%20return%20pour%20renvoyer%20une%20r%C3%A9ponse.%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3E%2F%2F%20this%20example%20shows%20how%20to%20write%20a%20user%20written%20function%20to%20roll%20%0A%2F%2F%20ndice%20with%20nsides%0A%0A%2F%2F%20a%20user%20written%20function%20is%20created%20with%20an%20assignment%20statement%0A%2F%2F%20to%20a%20variable%20that%20will%20be%20the%20name%20of%20the%20function.%20rollDice%0A%2F%2F%20seems%20a%20good%20name%20to%20explain%20what%20this%20function%20does.%0A%0ArollDice%20%3D%20Function(%20%7Bndice%20%3D%202%2C%20nsides%20%3D%206%7D%2C%20%2F%2F%20parameters%0A%20%20%20%20%7Bi%2C%20sum%20%3D%200%7D%2C%20%2F%2F%20local%20variables%0A%20%20%20%20For(%20i%20%3D%201%2C%20i%20%26lt%3B%3D%20ndice%2C%20i%2B%2B%2C%20%2F%2F%20loop%20to%20add%20up%20the%20rolls%0A%20%20%20%20%20%20%20%20sum%20%2B%3D%20Random%20Integer(%201%2C%20nsides%20)%20%2F%2F%20one%20roll%0A%20%20%20%20)%3B%20%2F%2F%20end%20of%20the%20loop%0A%20%20%20%20Return(%20sum%20)%3B%20%2F%2F%20value%20to%20return%0A)%3B%20%2F%2F%20end%20of%20the%20definition%0A%0A%2F%2F%20make%20table%20and%20distributions.%20the%20number%20of%20dice%20%0A%2F%2F%20and%20the%20number%20of%20sides%20is%20chosen%20to%20make%20the%0A%2F%2F%20average%20roll%20be%2036%0A%0Adt%20%3D%20New%20Table(%20%22Untitled%22%2C%0A%20%20%20%20Add%20Rows(%2010000%20)%2C%0A%20%20%20%20%2F%2F%20each%20column%20has%20a%20formula%20that%20calls%20the%20function...%0A%20%20%20%20New%20Column(%20%22one%22%2C%20Formula(%20rollDice(%201%2C%2071%20)%20)%20)%2C%0A%20%20%20%20New%20Column(%20%22two%22%2C%20Formula(%20rollDice(%202%2C%2035%20)%20)%20)%2C%0A%20%20%20%20New%20Column(%20%22three%22%2C%20Formula(%20rollDice(%203%2C%2023%20)%20)%20)%2C%0A%20%20%20%20New%20Column(%20%22four%22%2C%20Formula(%20rollDice(%204%2C%2017%20)%20)%20)%0A)%3B%0A%0A%2F%2F%20check%20the%20Std%20Dev%20in%20the%204%20reports...%0A%2F%2F%20it%20gets%20smaller%20with%20more%20dice%0A%0Adt%20%26lt%3B%26lt%3B%20Distribution(%0A%20%20%20%20Continuous%20Distribution(%20Column(%20%3Aone%20)%20)%2C%0A%20%20%20%20Continuous%20Distribution(%20Column(%20%3Atwo%20)%20)%2C%0A%20%20%20%20Continuous%20Distribution(%20Column(%20%3Athree%20)%20)%2C%0A%20%20%20%20Continuous%20Distribution(%20Column(%20%3Afour%20)%20)%2C%0A%20%20%20%20SendToReport(%0A%20%20%20%20%20%20%20%20Dispatch(%20%7B%22one%22%7D%2C%20%22Distrib%20Histogram%22%2C%20FrameBox%2C%20%7BDispatchSeg(%20Hist%20Seg(%201%20)%2C%20Bin%20Span(%2024%2C%200%20)%20)%7D%20)%2C%0A%20%20%20%20%20%20%20%20Dispatch(%20%7B%22two%22%7D%2C%20%22Distrib%20Histogram%22%2C%20FrameBox%2C%20%7BDispatchSeg(%20Hist%20Seg(%201%20)%2C%20Bin%20Span(%2024%2C%200%20)%20)%7D%20)%2C%0A%20%20%20%20%20%20%20%20Dispatch(%20%7B%22three%22%7D%2C%20%22Distrib%20Histogram%22%2C%20FrameBox%2C%20%7BDispatchSeg(%20Hist%20Seg(%201%20)%2C%20Bin%20Span(%2024%2C%200%20)%20)%7D%20)%2C%0A%20%20%20%20%20%20%20%20Dispatch(%20%7B%22four%22%7D%2C%20%22Distrib%20Histogram%22%2C%20FrameBox%2C%20%7BDispatchSeg(%20Hist%20Seg(%201%20)%2C%20Bin%20Span(%2024%2C%200%20)%20)%7D%20)%0A%20%20%20%20)%0A)%3B%3CBR%20%2F%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3C%2FDIV%3E%0A%3CDIV%20class%3D%22lia-message-template-diagnosis-zone%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22More%20dice%2C%20smaller%20standard%20deviation%22%20style%3D%22width%3A%20999px%3B%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22More%20dice%2C%20smaller%20standard%20deviation%22%20style%3D%22width%3A%20999px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22More%20dice%2C%20smaller%20standard%20deviation%22%20style%3D%22width%3A%20999px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.jmp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F8816iF03E092CC03AE5A4%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Capture.PNG%22%20alt%3D%22More%20dice%2C%20smaller%20standard%20deviation%22%20%2F%3E%3Cspan%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EMore%20dice%2C%20smaller%20standard%20deviation%3C%2Fspan%3E%3C%2Fspan%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EMore%20dice%2C%20smaller%20standard%20deviation%3C%2FSPAN%3E%3C%2FSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EPlus%20de%20d%C3%A9s%2C%20plus%20petit%20%C3%A9cart%20type%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CDIV%20class%3D%22lia-message-template-solution-zone%22%3E%0A%3CH2%20id%3D%22toc-hId-444700362%22%20id%3D%22toc-hId-807803774%22%20id%3D%22toc-hId-807803774%22%3EDiscussion%3C%2FH2%3E%0A%3CP%3ELa%20fonction%20Function()%20renvoie%20une%20fonction.%20Le%20premier%20argument%20de%20Function()%20est%20une%20liste%20de%20param%C3%A8tres%20et%20est%20obligatoire.%20Utilisez%20%7B%7D%20si%20vous%20n'avez%20aucun%20param%C3%A8tre.%20Les%20param%C3%A8tres%20peuvent%20avoir%20des%20valeurs%20par%20d%C3%A9faut%20comme%20indiqu%C3%A9%20ci-dessus%26nbsp%3B%3B%20rollDice()%20utilisera%20deux%20d%C3%A9s%20%C3%A0%206%20faces.%20rollDice(3)%20utilisera%20trois%20d%C3%A9s%20%C3%A0%206%20faces%20et%20rollDice(1%2C7)%20utilise%20un%20d%C3%A9%20%C3%A0%207%20faces.%3C%2FP%3E%0A%3CP%3ELe%20deuxi%C3%A8me%20argument%20de%20la%20fonction%20est%20une%20liste%20facultative%20de%20variables%20locales%2C%20qui%20peuvent%20avoir%20des%20valeurs%20initiales.%20En%20utilisant%20des%20valeurs%20locales%2C%20vous%20pouvez%20demander%20%C3%A0%20des%20fonctions%20d'appeler%20d'autres%20fonctions%20sans%20vous%20soucier%20de%20la%20r%C3%A9utilisation%20de%20%22i%22%20dans%20une%20boucle%20for.%20Si%20vous%20n'avez%20pas%20besoin%20de%20sp%C3%A9cifier%20de%20param%C3%A8tres%20locaux%2C%20placez%20simplement%20le%20corps%20de%20la%20fonction%20comme%20deuxi%C3%A8me%20argument%20plut%C3%B4t%20que%20comme%20troisi%C3%A8me.%3C%2FP%3E%0A%3CP%3ELe%20dernier%20argument%20(2e%20ou%203e)%20est%20le%20corps%20de%20la%20fonction.%20Il%20peut%20r%C3%A9f%C3%A9rencer%20des%20valeurs%20globales%20si%20n%C3%A9cessaire.%20Vous%20pouvez%20utiliser%20une%20instruction%20return%20n'importe%20o%C3%B9%20dans%20le%20corps%20pour%20sp%C3%A9cifier%20une%20valeur%20%C3%A0%20renvoyer%20%C3%A0%20l'appelant.%20S'il%20n'y%20a%20pas%20d'instruction%20return%2C%20la%20valeur%20de%20la%20derni%C3%A8re%20instruction%20ex%C3%A9cut%C3%A9e%20est%20renvoy%C3%A9e.%3C%2FP%3E%0A%3CP%3ELa%20fonction%20rollDice%20n'est%20pas%20ex%C3%A9cut%C3%A9e%20lors%20de%20sa%20premi%C3%A8re%20d%C3%A9finition.%20Il%20est%20ex%C3%A9cut%C3%A9%20lorsqu'il%20est%20appel%C3%A9%20avec%20des%20arguments%2C%20comme%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3ErollDice(3%2C6)%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3Ece%20que%20fait%20l'exemple%20dans%20les%20formules%20de%20colonnes.%3C%2FP%3E%3CBR%20%2F%3E%3CH2%20id%3D%22toc-hId-1332204043%22%20id%3D%22toc-hId--1744353187%22%20id%3D%22toc-hId--1744353187%22%3EVoir%20%C3%A9galement%3C%2FH2%3E%0A%3CP%3E%3CA%20href%3D%22http%3A%2F%2Fwww.jmp.com%2Fsupport%2Fhelp%2FAdvanced_Programming_Concepts.shtml%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttp%3A%2F%2Fwww.jmp.com%2Fsupport%2Fhelp%2FAdvanced_Programming_Concepts.shtml%3C%2FA%3E%3C%2FP%3E%0A%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-378442%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%20%3A%20%C3%89crivez%20vos%20propres%20fonctions%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-378442%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fcommunity.jmp.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F982%22%20target%3D%22_blank%22%3E%40Craige_Hales%3C%2FA%3E%3C%2FP%3E%3CP%3EFWIW%2C%20je%20suis%20heureux%20que%20vous%20ayez%20laiss%C3%A9%20les%20valeurs%20par%20d%C3%A9faut%20dans%20votre%20exemple.Je%20suis%20tomb%C3%A9%20sur%20ce%20post%20alors%20que%20je%20cherchais%20des%20informations%20sur%20Function()%2C%20et%20cela%20a%20%C3%A9t%C3%A9%20tr%C3%A8s%20utile%26nbsp%3B!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-378410%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%20%3A%20%C3%89crivez%20vos%20propres%20fonctions%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-378410%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EMerci!%20%3CA%20href%3D%22https%3A%2F%2Fcommunity.jmp.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F8821%22%20target%3D%22_blank%22%3E%40Juda%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EJe%20ferais%20probablement%20une%20r%C3%A8gle%20pour%20une%20valeur%20manquante%20et%20ajouterais%20une%20instruction%20if%20pour%20remplacer%20manquant%20par%20d%C3%A9faut.%3CEM%3E%20rollDice(%20.%20%2C%205%20)%3B%3C%2FEM%3E%20ce%20qui%20signifie%20probablement%20deux%20copies%20de%20la%20valeur%20par%20d%C3%A9faut%20dans%20la%20fonction...%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIl%20existe%20plusieurs%20raisons%20d%E2%80%99utiliser%20des%20valeurs%20par%20d%C3%A9faut%2C%20et%20certaines%20d%E2%80%99entre%20elles%20peuvent%20parfois%20%C3%AAtre%20de%20mauvaises%20raisons.%3C%2FP%3E%3CUL%3E%3CLI%3Erendre%20le%20code%20d'appel%20plus%20court%3C%2FLI%3E%3CLI%3Epouvoir%20modifier%20la%20valeur%20par%20d%C3%A9faut%20plus%20tard%2C%20en%20un%20seul%20endroit%3C%2FLI%3E%3CLI%3Eparce%20que%20la%20fonction%20a%20%C3%A9t%C3%A9%20modifi%C3%A9e%20apr%C3%A8s%20sa%20premi%C3%A8re%20publication%3C%2FLI%3E%3CLI%3E%3F%3C%2FLI%3E%3C%2FUL%3E%3CP%3ELes%20param%C3%A8tres%20par%20d%C3%A9faut%20ne%20sont%20probablement%20pas%20plus%20rapides%20que%20leur%20sp%C3%A9cification%20(estimation%20non%20test%C3%A9e.)%20Cela%20peut%20sembler%20meilleur%20si%20les%20valeurs%20par%20d%C3%A9faut%20correspondent%20%C3%A0%20ce%20qu'un%20r%C3%A9viseur%20de%20code%20raisonnable%20s'attendrait.%20Changer%20la%20valeur%20par%20d%C3%A9faut%20ult%C3%A9rieurement%20pourrait%20casser%20quelque%20chose.%20Ajouter%20un%20nouveau%20param%C3%A8tre%20pour%20un%20nouvel%20appelant%20et%20ne%20pas%20toucher%20aux%20anciens%20appelants%20peut%20%C3%AAtre%20utile.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EJ'aurais%20peut-%C3%AAtre%20souhait%C3%A9%20laisser%20les%20valeurs%20par%20d%C3%A9faut%20en%20dehors%20de%20cet%20exemple%26nbsp%3B%3B%20ils%20ne%20rendent%20pas%20toujours%20le%20code%20plus%20facile%20%C3%A0%20suivre.%20Outils%20%C3%A9lectroportatifs!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-378377%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%20%3A%20%C3%89crivez%20vos%20propres%20fonctions%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-378377%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EMerci%20pour%20cela%2C%3CA%20href%3D%22https%3A%2F%2Fcommunity.jmp.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F982%22%20target%3D%22_blank%22%3E%20%40Craige_Hales%3C%2FA%3E%20.Lors%20de%20l'appel%20de%20la%20fonction%20rollDice%2C%20je%3CSPAN%3E%20Existe-t-il%20un%20moyen%20de%20conserver%20ndice%20%C3%A9gal%20%C3%A0%20sa%20valeur%20par%20d%C3%A9faut%20et%20de%20d%C3%A9finir%20nsides%20sur%20une%20valeur%20autre%20que%20celle%20par%20d%C3%A9faut%20sans%20transmettre%20la%20valeur%20par%20d%C3%A9faut%20de%20ndice%26nbsp%3B%3F%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EPar%20exemple%3A%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3Endice%20%3D%203%2C%20nsides%20%3D%205%20%3D%3D%26gt%3B%20rollDice(%203%2C%205%20)%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3Endice%20%3D%203%2C%20nsides%20%3D%206%20(valeur%20par%20d%C3%A9faut)%20%3D%3D%26gt%3B%20rollDice(%203%20)%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3Endice%20%3D%202%20(valeur%20par%20d%C3%A9faut)%2C%20nsides%20%3D%205%20%3D%3D%26gt%3B%20rollDice(%20%3F%3F%20)%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBien%20s%C3%BBr%2C%20ndice%20pourrait%20%C3%AAtre%20sp%C3%A9cifi%C3%A9%20(%20rollDice(%202%2C%205%20)%20)%2C%20mais%20doit-il%20l'%C3%AAtre%20pour%20autoriser%20l'utilisation%20d'une%20valeur%20autre%20que%20celle%20par%20d%C3%A9faut%20pour%20nsides%26nbsp%3B%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E
Choose Language Hide Translation Bar
Write Your Own Functions

Problem

You have some code that you want to call from more than one place, maybe with different arguments.

Solution

Use a user written function, with parameters, local variables to keep them away from other code, and a return statement to send back an answer.

// this example shows how to write a user written function to roll 
// ndice with nsides

// a user written function is created with an assignment statement
// to a variable that will be the name of the function. rollDice
// seems a good name to explain what this function does.

rollDice = Function( {ndice = 2, nsides = 6}, // parameters
    {i, sum = 0}, // local variables
    For( i = 1, i <= ndice, i++, // loop to add up the rolls
        sum += Random Integer( 1, nsides ) // one roll
    ); // end of the loop
    Return( sum ); // value to return
); // end of the definition

// make table and distributions. the number of dice 
// and the number of sides is chosen to make the
// average roll be 36

dt = New Table( "Untitled",
    Add Rows( 10000 ),
    // each column has a formula that calls the function...
    New Column( "one", Formula( rollDice( 1, 71 ) ) ),
    New Column( "two", Formula( rollDice( 2, 35 ) ) ),
    New Column( "three", Formula( rollDice( 3, 23 ) ) ),
    New Column( "four", Formula( rollDice( 4, 17 ) ) )
);

// check the Std Dev in the 4 reports...
// it gets smaller with more dice

dt << Distribution(
    Continuous Distribution( Column( :one ) ),
    Continuous Distribution( Column( :two ) ),
    Continuous Distribution( Column( :three ) ),
    Continuous Distribution( Column( :four ) ),
    SendToReport(
        Dispatch( {"one"}, "Distrib Histogram", FrameBox, {DispatchSeg( Hist Seg( 1 ), Bin Span( 24, 0 ) )} ),
        Dispatch( {"two"}, "Distrib Histogram", FrameBox, {DispatchSeg( Hist Seg( 1 ), Bin Span( 24, 0 ) )} ),
        Dispatch( {"three"}, "Distrib Histogram", FrameBox, {DispatchSeg( Hist Seg( 1 ), Bin Span( 24, 0 ) )} ),
        Dispatch( {"four"}, "Distrib Histogram", FrameBox, {DispatchSeg( Hist Seg( 1 ), Bin Span( 24, 0 ) )} )
    )
);
More dice, smaller standard deviationMore dice, smaller standard deviation

Discussion

The Function() function returns a function. The first argument to Function() is a list of parameters, and is required. Use {} if you have no parameters. The parameters can have default values as shown above; rollDice() will use two 6-sided dice. rollDice(3) will use three 6-sided dice, and rollDice(1,7) uses one 7-sided die.

The second argument to function is an optional list of local variables, which can have initial values. By using local values, you can have functions call other functions and not worry about reusing "i" in a for-loop. If you don't need to specify locals, just put the function body as the 2nd argument rather than the third.

The final argument (2nd or 3rd) is the function body. It can reference global values if needed. You can use a return statement anywhere in the body to specify a value to send back to the caller. If there is no return statement, the value of the last statement executed is returned.

The function rollDice is not executed when it is first defined. It is executed when it is called with arguments, like

rollDice(3,6)

which the example does in the column formulas.


See Also

http://www.jmp.com/support/help/Advanced_Programming_Concepts.shtml

Comments
Judah

Thanks for this, @Craige_Hales.  When calling the rollDice function, is there a way keep ndice equal to its default value and setting nsides equal to a non-default value without passing in the default value of ndice?

 

For example:

ndice = 3, nsides = 5  ==>  rollDice( 3, 5 )

ndice = 3, nsides = 6 (default value)  ==>  rollDice( 3 )

ndice = 2 (default value), nsides = 5  ==>  rollDice( ?? )

 

Of course, ndice could be specified ( rollDice( 2, 5 ) ), but does it have to be to allow a non-default value for nsides to be used?

Thanks! @Judah

 

I'd probably make a rule for a missing value and add an if statement to replace missing with default. rollDice( . , 5 ); which probably means two copies of the default value in the function...

 

There are several reasons for using default values, and some of them might be bad reasons, sometimes.

  • making calling code shorter
  • being able to change the default later, in one place
  • because the function was change after it was first published
  • ?

The default parameters are probably not any faster than specifying them (untested guess.) It may look better if the default values are what a reasonable code reviewer would expect. Changing the default later might break something. Adding a new parameter for a new caller and not touching the old callers might be useful.

 

I might be wishing I'd left the default values out of this example; they don't always make code easier to follow. Power tools!

Judah

@Craige_Hales

FWIW, I'm glad you left the default values in your example.  I came across this post when I was searching for some info on Function(), and this was quite helpful!

JSL Cookbook

If you’re looking for a code snippet or design pattern that performs a common task for your JSL project, the JSL Cookbook is for you.

This knowledge base contains building blocks of JSL code that you can use to reduce the amount of coding you have to do yourself.

It's also a great place to learn from the experts how to use JSL in new ways, with best practices.