CS116 Week 5 Lecture
CS116 Week 5 Lecture
InVisualBasic,proceduresarestatementsorcollectionsofstatementsthatwill
performtasks(e.g.eventhandlers).Generalpurposeproceduresdonotrelyon
events.Functions,likeprocedures,consistofstatements(orcollectionsof
statements)whichperformtasksbutalsoreturnvaluestotheexecutionpoint.
Collectivelybothfunctionsandproceduresarereferredtoas
methods.
Modularizingcodeisawaytobreakdowncodeintomoremanageablegroupsor
sectionsthatsavetimeandeffortaswellasavoidredundancy.Ratherthan
repeatingprocedures,theprocedurecanbewrittenonetimeand"called"when
needed.
Notethewordssubandendsub.Alsonotehow,unlikeinmorespecificprocedures
likeeventhandlers,thereisnoprefixinfrontofthecontrol.Generalpurpose
proceduresarenotassociatedwithcontrols.
ExecutingProcedures
Nowthatwehaveageneralpurposeprocedure,howdoweexecuteit?Writinga
procedurecallwilltriggerexecution.Whenweneedtheprocedure,itwillbecalled
usingthenamedeclaredfortheprocedure:
Oronecansimplycalltheprocedureusingthecallkeyword.
CallDisplayMessage
Proceduresaredeclaredusing:
Proceduresarenamedusingscalecasing,inotherwordscapitalizingeverywordin
thename.Parametersreceivevaluespassedintoprocedures.Therearealsovalues
calledarguments.Argumentscanbepassedbyvalue(acopyispassedtothe
procedure)andbyreference(allowingtheprocedureaccesstotheoriginalargument
aswellistheabilitytomakechanges).
Functions,aspreviouslydiscussed,returnvaluestothepointintheprogramfor
whichtheywerecalled.Afunctiondeclarationfollowsthebasicformat:
Inbothproceduresandfunctions,accesscanbespecifiedusingtheAccessSpecifier
aheadofthefirstlineusingkeywordslikePublic,Protected,Private,andmore.
ThedefaultisPublicifnootherspecifierischosen.