Definition
Formalparameter:PointoutthevariablenameandarraynameintheformalparametertableoftheSubandFunctionprocedures.Beforetheprocedureiscalled,thereisnoTheyallocatememory,andtheirroleistoexplainthetypeandshapeoftheindependentvariableandtheroleintheprocess.Theformalparametercanbealegalvariablenameotherthanafixed-lengthstringvariable,oranarraynamewithparentheses.
Actualparameter:TheactualparameteristheparametervaluepassedfromthecallingproceduretothecalledprocedurewhentheSubandFunctionproceduresarecalled.Theactualparameterscanbevariablenames,arraynames,constants,orexpressions.Whenpassingparametersinaprocedurecall,theactualparametersoftheformalparticipationarecombinedbyposition.Thecorrespondingvariablenamesintheformalparameterlistandtheactualparameterlistdonotneedtobethesame,buttheirdatatypes,numberofparameters,andpositionsmustcorrespondone-to-one.
Relationshipbetweenthetwo:
1.Formalparameterscanonlybevariables,andactualparameterscanbeconstants,variablesorexpressions.Inthefunctionbeingdefined,thetypeoftheformalparametermustbespecified.
2.Thenumberofactualparametersshouldbethesame,andthetypesshouldbethesame.Charactertypeandintegertypecanbeusedinterchangeably.
3.Whencallingafunction,iftheactualparameterisanarrayname,thefirstaddressofthearrayispassedtotheformalparameter
4.TheactualparameterispassedtotheformalparameterinonedirectionPass,theformalparametervariabledoesnotoccupymemorywhenthereisnofunctioncall,onlywhenitiscalled.Afterthecallisover,thememorywillbereleased.Whenexecutingacalledfunction,ifthevalueoftheformalparameterchanges,itwillnotchangethevalueoftheactualparameterinthecallingfunction.
5.Theformalparametersarelikethesymbolsintheformula,andtheactualparametersarethespecificvaluesofthesymbols,whichmustbeassignedbeforethecallingprocess;thecallingprocessistorealizethecombinationoftheformalparticipationintheactualparametersandpassthevalueoftheactualparameterThecallispassedtotheformalparameter,whichisequivalenttosubstitutingthevalueintotheformulaforcalculation.
Method
Passingparametersbyvalue
Whenpassingparametersbyvalue,theactualparameterThevalueofthevariableiscopiedtothetemporarystorageunit.Ifthevalueoftheformalparameterischangedduringthecall,theactualparametervariableitselfwillnotbeaffected,thatis,theactualparametervariableremainsunchangedatthevaluebeforethecall.Whenpassingparametersbyvalue,youneedtoaddthe"ByVal"keywordbeforetheparametername.
Passingparametersbyaddress
Whenpassingparametersbyaddress,theaddressoftheactualparametervariableistransferredtothecalledprocess,andtheformalandactualparameterssharememoryThesameaddress.Intheprocessofbeingcalled,oncethevalueoftheformalparameterchanges,thevalueofthecorrespondingactualparameteralsochanges.Iftheactualparameterisaconstantorexpression,VisualBasic6.0willdealwithitinthe"passbyvalue"mode,andthe"ByVal"keywordisnotrequiredtopassbyaddress.
Pressarraytopassparameters
InVB6.0,itisallowedtousearraysasactualparameterstopasstothesub-procedureformIntheparameter,thearraypassmustusetheaddresspasstopasstheparameters.Whenpassingarrayparameters,youshouldpayattentiontothefollowingtwoaspects:
①Writeonlythearraynameintheactualparameterandformalparameterlist,ignoringthedefinitionofdimension,buttheparenthesescannotbeomitted,whenthearrayispassedasaparameterAtthetime,thesystempassesthestartingaddressoftherealparametergrouptotheprocess,sothattheshapeparametergroupalsohasthesamestartingaddressastherealparametergroup.Iftheparameterisamulti-dimensionalarray,eachdimensionisseparatedbyacomma;
②ThelowerboundandupperboundoftherealparametergroupcanbedeterminedthroughtheLboundandUboundfunctionsintheadjustedprocess.
Callafunctionwithparameters
Whencallingafunction,youcanpassvaluestoit.Thesevaluesarecalledparameters.
Theseparameterscanbeusedinfunctions.
Youcansendasmanyparametersasyouwant,separatedbycommas(,):
myFunction(argument1,argument2)
Whenyoudeclareafunction,pleasedeclaretheparameterasavariable:
functionmyFunction(var1,var2){hereisthecodetobeexecuted}
variableTheandparametersmustappearinthesameorder.Thefirstvariableisthegivenvalueofthefirstparameterpassed,andsoon.
Example
Forthefollowingprogram,trytoanalyzetheresultsobtainedwhenpassingparametersbypassingvalues,passingaddresses,andpassingarraysTheprintresult.
PROGRAMSS(input,output);
VAR
A,B:integer;
PROCEDUREP(x,y,z:integer);
beginy:=y+1;z:=z+x;
end;
BEGIN
A:=2;b:=3;
P(A+B,A,A);
writeln('A=',A);
END
Answer
(1)Passvalue:Calculatethevalueoftheactualparameterandpassittotheformalparameter.
WhencallingprocedureP,theformalparameterx=5;y=2;z=2
WhencallingprocedureP,theformalparameterx=5;y=3;z=7
Thisdoesnotsendtheresultbacktothemainprogram,sotheresultisA=2
(2)Transferaddress:theactualparametercalculatestheresult,Sendtheaddresstotheformalparameter.
SetvariableT=A+B(resultis5).Whenexecuting,sendtheaddressesofT,A,andA(setasaddr1,addr2,addr2)totheformalparameters:
x=daar1,y=addr2,z=addr2.
T'saddressaddr1isx→T(5)
A'saddressaddr2isy→A(2)
A'saddressaddr2isz→A(2)
TheexecutionprocessPis:①y↑:=y↑+1;②z↑:=z↑+x↑
So,①isA:=A+1=3
②isA:=A+T=8.Therefore,attheendA=8.
(3)passingarray:equivalenttoexecutingA:=2;B:=3;A:=A+1;A:=A+(A+B)
writeln('A=',A);
So,theresultisA=9.