actually when i was doing classic I wrote this sorry its a bit messy but it will help if you digest it slowly
in each block of code in each paragraph the rem lines is what u unremark when u want to test that function.
avoid unremarking the ` lines so its obvious what to unremark with both types of remark statements
` FUNCTIONS hopefully demistified.
` indi
` dec 2001 for darkbasic 1
` functions are basically the grouping of commands
` to create a new command specific for your needs
` a function usually returns a result from the paramters
` you feed it but not always as u can have functions
` that dont accept any parameters or return a result
` functions can take parameters for use in your code
` maximum paramters 255
` parameter usage sperated by commas
`eg
` function myfunction(parameter,parameter)
` functions can contain local variables and arrays
` anything declared inside the function is local
` eg
` myint = 5
` dim myarray(1)
` if these were made in the function
` then they would be local to that function only
` if we declared them at the top of our code before the function
` they would be global values.
` arrays entering a function via code in the function to change that
` array are global and return a result without interfearing with
` the result variable
` code must start directly under the next line of the function
` being created
` PRINTME 1 FUNCTION
` lets look at the most humblest of functions
` the printme function
`un remark the rem lines
`------------------------------------------
` using the function
rem printme()
` helper code
rem print "press a key to end"
rem suspend for key
rem end
` the function
rem function printme()
rem print "hello world this is inside my function"
rem endfunction
`------------------------------------------
` this function does nothing except
` print "hello world this is inside my function" to the screen
` this function doesnt return a value
` this function doesnt accept any paramters
` its the simplest kind of function
` PRINTME 2 FUNCTION
` lets look at a new function that accepts paramters
` the printme2 function
`un remark the rem lines
`------------------------------------------
` using the function and feeding out parameter we declared in the function
` called myparameter a value of 456
rem printme(456)
` helper code
rem print "press a key to end"
rem suspend for key
rem end
` the function
rem function printme(myparameter)
rem print "hello world this is inside my function"
rem print myparameter
rem endfunction
`------------------------------------------
` this function does nothing except
` print "hello world this is inside my function" to the screen
` print the value of myparameter to the screen eg 456
` this function doesnt return a value
` this function accepts one parameter
` its the simplest kind of function
` PRINTME 3 FUNCTION
` lets look at a new function that accepts parameters and returns a value
` the printme3 function
`un remark the rem lines
`------------------------------------------
` using the function and feeding out parameter we declared in the function
` called myparameter a value of 456
rem returnedvalue = printme(50,55)
rem print returnedvalue
` helper code
rem print "press a key to end"
rem suspend for key
rem end
` the function
rem function printme(param1,param2)
rem print "hello world this is inside my function"
rem param3 = (param1 + param2)
rem endfunction param3
`------------------------------------------
` print "hello world this is inside my function" to the screen
` this function accepts 2 parameters and returns a value
` they share the same variable data
` param3 's value gets handed over to returnedvalue
` even though they are 2 different variables
` PRINTME 4 FUNCTION
` lets look at a new function that accepts different kinds
` of paramters and returns one value
` the printme4 function
`un remark the rem lines
`------------------------------------------
` using the function and feeding out parameter we declared in the function
` called myparameter a value of 456
rem returnedvalue = printme(50,55,"fred",45.54)
rem print returnedvalue
` helper code
rem print "press a key to end"
rem suspend for key
rem end
` the function
rem function printme(param1,param2,paramstring$,param4#)
rem print "hello world this is inside my function"
rem print paramstring$
rem print param4#
rem param3 = (param1 + param2)
rem endfunction param3
`------------------------------------------
` this function prints the usuall hello world stuff
` this function accepts 4 parameters and returns a value from the first 2
` re param3 and returned value variables
` they share the same variable data
` even though they are 2 different variables
` param3 's value gets handed over to returnedvalue
` PRINTME 5 FUNCTION
` lets look at a new function that accepts different kinds
` of paramters and returns one value but stores a new local variable
` called mynewvariable
` the printme6 function
`un remark the rem lines
`------------------------------------------
` using the function and feeding out parameter we declared in the function
` called myparameter a value of 456
rem returnedvalue = printme(50,55,"fred",45.54)
rem print returnedvalue
rem print mynewvariable
` helper code
rem print "press a key to end"
rem suspend for key
rem end
` the function
rem function printme(param1,param2,paramstring$,param4#)
rem print "hello world this is inside my function"
rem print paramstring$
rem print param4#
rem param3 = (param1 + param2)
rem mynewvariable = 5040
rem print mynewvariable
rem endfunction param3
`------------------------------------------
` this function prints the usuall hello world stuff
` this function accepts 4 parameters and returns a value from the first 2
` param3 's value gets handed over to returnedvalue
` they share the same variable data
` this function also has a local variable called mynewvariable
` notice when the function prints the variable compared
` to printing it outside of the function
` PRINTME 6 FUNCTION
` lets look at a new function that accepts different kinds
` of paramters and returns one value but stores a new local variable
` called mylocalvariable and make a new variable array called mylocalarray(1)
` the printme 6 function
`un remark the rem lines
`------------------------------------------
` using the function and feeding out parameter we declared in the function
` called myparameter a value of 456
rem returnedvalue = printme(50,55,"fred",45.54)
rem print "returnedvalue = ",returnedvalue
rem print "mylocalvariable",mylocalvariable
` helper code
rem print "press a key to end"
rem suspend for key
rem end
` the function
rem function printme(param1,param2,paramstring$,param4#)
rem print "hello world this is inside my function"
rem print "paramstring$ = ",paramstring$
rem print "param4# = ",param4#
rem param3 = (param1 + param2)
rem mylocalvariable = 5040
rem print "mylocalvariable =",mylocalvariable
rem dim mylocalarray(1) :mylocalarray(1)=2000
rem print "mylocalarray(1) = ",mylocalarray(1)
rem endfunction param3
`------------------------------------------
` this function prints the usuall hello world stuff
` this function accepts 4 parameters and returns a value from the first 2
` param3 's value gets handed over to returnedvalue
` this function also has a local variable called mynewvariable
` and an array called mylocalarray(1)
` notice when the function prints the variable compared
` to printing it outside of the function
` added a little cosmetic visual output of the intended variables
` PRINTME 7 FUNCTION
` lets look at a new function that accepts different kinds
` of paramters and returns one value but stores a new local variable
` called mylocalvariable and make a new variable array called mylocalarray(1)
` this function also manipulates a global array
` the printme 7 function
`un remark the rem lines
`------------------------------------------
` declaring the global array outside of the function
rem dim myglobalarray(1)
rem myglobalarray(1)= 40023
rem print "myglobalarray before the function = ",myglobalarray(1)
` using the function and feeding out parameter we declared in the function
` called myparameter a value of 456
rem returnedvalue = printme(50,55,"fred",45.54)
rem print "returnedvalue = ",returnedvalue
rem print "mylocalvariable",mylocalvariable
rem print "myglobalarray after the function = ",myglobalarray(1)
` helper code
rem print "press a key to end"
rem suspend for key
rem end
` the function
rem function printme(param1,param2,paramstring$,param4#)
rem print "hello world this is inside my function"
rem print "paramstring$ = ",paramstring$
rem print "param4# = ",param4#
rem param3 = (param1 + param2)
rem mylocalvariable = 5040
rem print "mylocalvariable =",mylocalvariable
rem dim mylocalarray(1) :mylocalarray(1)=2000
rem print "mylocalarray(1) = ",mylocalarray(1)
rem dim myglobalarray(1) =( dim myglobalarray(1) + 10000 )
rem print " myglobalarray being altered by and inside the function = ",myglobalarray(1)
rem endfunction param3
`------------------------------------------
` this function prints the usuall hello world stuff
` this function accepts 4 parameters and returns a value from the first 2
` param3 's value gets handed over to returnedvalue
` this function also has a local variable called mynewvariable
` and an array called mylocalarray(1)
` notice when the function prints the variable compared
` to printing it outside of the function
` added a little cosmetic visual ouput of the intended variables
` ok now to think about what u just read....
` Look at the 2 functions that has no info regarding what they do below
` If u can read what it does then u have learnt more about functions
` then u realised.
rem dim myglob(1)
rem myglob(1)= 40023
rem print "myglob before the function = ",myglob(1)
rem returnedvalue = printme(50,55,"fred",45.54)
rem print "returnedvalue = ",returnedvalue
rem print "mylocalvariable",mylocalvariable
rem print "mylocalarray(1) = ",mylocalarray(1)
rem print "myglob after the function = ",myglob(1)
rem print "press a key to end"
rem suspend for key
rem end
rem function printme(param1,param2,paramstring$,param4#)
rem print "hello world this is inside my function"
rem print "paramstring$ = ",paramstring$
rem print "param4# = ",param4#
rem param3 = (param1 + param2)
rem mylocalvariable = 5040
rem print "mylocalvariable =",mylocalvariable
rem dim mylocalarray(1)
rem mylocalarray(1)=2000
rem print "mylocalarray(1) = ",mylocalarray(1)
rem myglob(1) =(myglob(1) + 10000 )
rem print "myglobalarray being altered by and inside the function = ",myglob(1)
rem makebox(255,0,0,300,300,350,350)
rem print "closing function"
rem endfunction param3
rem function makebox(r,g,b,x1,x2,y1,y2)
rem ink rgb(r,g,b),1
rem box x1,x2,y1,y2
rem endfunction
` the #include command should be studied as this command allows
` you to store your functions in another file for making a lib rary
` of your most cherished and most used functions
` One thing is didnt mention is that u can nest functions in functions but
` be aware of the local state and global state of your variables
`