Предаване на параметри

Определение

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,andtheactualparametersarethespecificvalues​​ofthesymbols,whichmustbeassignedbeforethecallingprocess;thecallingprocessistorealizethecombinationoftheformalparticipationintheactualparametersandpassthevalueoftheactualparameterThecallispassedtotheformalparameter,whichisequivalenttosubstitutingthevalueintotheformulaforcalculation.

Метод

Предаване на параметри по стойност

Whenpassingparametersbyvalue,theactualparameterThevalueofthevariableiscopiedtothetemporarystorageunit.Ifthevalueoftheformalparameterischangedduringthecall,theactualparametervariableitselfwillnotbeaffected,thatis,theactualparametervariableremainsunchangedatthevaluebeforethecall.Whenpassingparametersbyvalue,youneedtoaddthe"ByVal"keywordbeforetheparametername.

Предаване на параметри по адрес

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;

②Долната граница и горната граница на групата реални параметри могат да бъдат определени чрез Lbound и Ubound функции в коригирания процес.

Функция Calla с параметри

Whencallingafunction,youcanpassvalues​​toit.Thesevalues​​arecalledparameters.

Тези параметри могат да се използват във функции.

Можете да изпратите много параметри, които искате, разделени със запетаи (,):

myFunction(argument1,argument2)

Whenyoudeclareafunction,pleasedeclaretheparameterasavariable:

functionmyFunction(var1,var2){тук е кодът за изпълнение}

variableTheandparametersmustappearinthesameorder.Thefirstvariableisthegivenvalueofthefirstparameterpassed,andsoon.

Пример

Forthefollowingprogram,trytoanalyzetheresultsobtainedwhenpassingparametersbypassingvalues,passingaddresses,andpassingarraysTheprintresult.

PROGRAMSS(вход,изход);

VAR

A,B:цяло число;

PROCEDUREP(x,y,z:цяло число);

начало:=y+1;z:=z+x;

край;

НАЧАЛО

A:=2;b:=3;

P(A+B,A,A);

writeln('A=',A);

КРАЙ

Отговор

(1)Passvalue:Calculatethevalueoftheactualparameterandpassittotheformalparameter.

При извикване на процедура P, формалният параметърx=5;y=2;z=2

При извикване на процедура P, формалният параметърx=5;y=3;z=7

Thisdoesnotsendtheresultbacktothemainprogram,sotheresultisA=2

(2)Transferaddress:theactualparametercalculatestheresult,Sendtheaddresstotheformalparameter.

SetvariableT=A+B(resultis5). При изпълнение изпратете адресите на T,A иA(setasaddr1,addr2,addr2) към формалните параметри:

x=daar1,y=addr2,z=addr2.

T'addressaddr1isx→T(5)

A'saddressaddr2isy→A(2)

A'addressaddr2isz→A(2)

Процесът на изпълнениеPis:①y↑:=y↑+1;②z↑:=z↑+x↑

И така, ①е A:=A+1=3

②е A:=A+T=8. Следователно в края A=8.

(3)passingarray:equivalenttoexecutingA:=2;B:=3;A:=A+1;A:=A+(A+B)

writeln('A=',A);

И така, резултатът е A=9.

Related Articles
TOP