News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

PlayBASIC Tutorial: Intro To Functions - (2018-07-06)

Started by kevin, July 06, 2018, 12:56:44 AM

Previous topic - Next topic

kevin

PlayBASIC Tutorial: Intro To Functions - (2018-07-06)

Hi welcome to our Intro to Functions tutorial.   In this tutorial we start out with some revision of Gosub / Return statements, which are used to create a simple sub routine.  Sub routines are changes in  program flow,  allowing the programmer to execute a chunk of code that's external to the section they may be writing, then upon completion the control returns to the caller.    This is the same basic model that functions introduction to our programming, except functions are more formalized.  Meaning they have some strict rules about syntax and intro a new concepts such as scope changes, which sub routines don't have.  
   
 The types of functions shown in video are very simple, initially we start by taking a sub routine that prints rows of text and convert that to function.  Through this process we encounter our first problem which is variable scope and look at ways to solve it, such as making our variable global, or better yet passing a variable into the function as a parameter .  Later in the video, we create our own custom distance function, as well building a function that does some simple string manipulation    

 NOTE: This video was  recorded alive with only a few changes for length..


Video:



Example Code:

PlayBASIC Code: [Select]
         print ProcessString( "-------Hello World-------")
print ProcessString( " 1234 " )


Dist# = CustomDistance( 100,100,700,300)

print dist#


for lp =0 to 10
Print_Lines_function( lp )
next
print "--------------------------------------------------"

sync
waitkey

end




function Print_Lines_function( LoopCounter )
print "--------------------------------------------------"
print "Loop Counter="+str$(LoopCounter)
endfunction



Function CustomDistance( X1#,Y1# , X2#,Y2#)

DX# = abs(X1#-X2#)
DY# = abs(Y1#-Y2#)

DistResult# = sqrt( DX# * DX# + Dy# * DY#)

EndFunction DistResult#



Function ProcessString( MyString$)

MyString$ = trim$(MyString$,"-")

MyString$ = Replace$( MyString$," ","~" )

EndFunction MyString$





Related Tutorials:

PlayBASIC Tutorial: Intro to GOTO & GOSUB statements (Beginners)
https://www.youtube.com/watch?v=_PKU8LvS9QM

PlayBASIC LIve: Creating a library (XML parser) - PlayList
https://www.youtube.com/playlist?list=PL_dvm0gvzzIWrpVrpksXdFSKl9hlzgjzX


PlayBASIC Tutorial PlayList
https://www.youtube.com/watch?v=SFlhErOP2PM&list=PL_dvm0gvzzIUZBDh_EQKKsdr7LC5W1HUm


Visit the PlayBASIC home page bellow

Learn to code at https://PlayBASIC.com