Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Use of Functions
#28
You just made a good point supporting the use of Subs over Functions!

Try and return an array or structure/UDT with a function (in QB64).

OTOH normally you use a sub to draw something but when reviewing my handy Subs and Functions in my toolbox last night (BTW Subs outnumber Functions maybe 5 to 1 !) I found a Function that draws a Sky for background according to a light percentage from 0 to 100. It's a function because it makes a background image that you can reuse in mainloop for drawing same background to clear screen and update animation action. So it returns the handle to that image which needs to be shared with Main program to use in loop.

Here is quick demo test (not using it in a loop over and over again for a background for an animation) just comparing different light settings:
Code: (Select All)
_Title "DrawSky&(light) test" 'b+ 2025-05-22
Screen _NewImage(800, 600, 32)
_ScreenMove 250, 60
For i = 0 To 100 Step 12.5
    hdl& = DrawSky&(i)
    _PutImage , hdl&, 0
    _Title "DrawSky&(light) test, light at" + Str$(i) + "%"
    _Display
    Sleep
Next

Function DrawSky& (light As Long) ' light = 0 to 100 as percent
    ' needs MidInk~&() Function
    Dim As _Unsigned Long saveColor, c
    Dim As Long i, rtn, saveDest
    Dim r, rn, xx, yy, lite
    lite = 2 * light
    saveDest = _Dest
    saveColor = _DefaultColor(saveDest)

    rtn = _NewImage(_Width, _Height, 32)
    _Dest rtn&
    For i = 0 To _Height - 1
        c = midInk(.75 * lite + 10, .75 * lite + 5, 35 + .75 * lite, 25 + lite, lite, 55 + lite, i / (_Height - 1))
        Line (0, i)-(_Width, i), c
    Next
    'stars only in low lite
    If lite <= 100 Then
        For i = 1 To _Width * _Height / 1500
            rn = Rnd: xx = Rnd * _Width: yy = Rnd * _Height
            If rn < .01 Then
                For r = 0 To 2 Step .5
                    Circle (xx, yy), r, _RGB32(185, 185, 185)
                Next
            ElseIf rn < .2 Then
                Circle (xx, yy), 1, _RGB32(185, 185, 185)
                PSet (xx, yy), _RGB32(185, 185, 185)
            Else
                PSet (xx, yy), _RGB32(185, 185, 185)
            End If
        Next
    End If
    _Dest saveDest
    Color saveColor
    DrawSky& = rtn
End Function

Function midInk~& (r1%, g1%, b1%, r2%, g2%, b2%, fr##)
    midInk~& = _RGB32(r1% + (r2% - r1%) * fr##, g1% + (g2% - g1%) * fr##, b1% + (b2% - b1%) * fr##)
End Function
b = b + ...
Reply


Messages In This Thread
Use of Functions - by PhilOfPerth - 05-20-2025, 11:21 PM
RE: Use of Functions - by bplus - 05-20-2025, 11:24 PM
RE: Use of Functions - by ahenry3068 - 05-20-2025, 11:34 PM
RE: Use of Functions - by PhilOfPerth - 05-21-2025, 12:25 AM
RE: Use of Functions - by bplus - 05-21-2025, 12:39 AM
RE: Use of Functions - by PhilOfPerth - 05-21-2025, 01:33 AM
RE: Use of Functions - by SMcNeill - 05-21-2025, 02:33 AM
RE: Use of Functions - by PhilOfPerth - 05-21-2025, 04:24 AM
RE: Use of Functions - by ahenry3068 - 05-21-2025, 06:41 AM
RE: Use of Functions - by bplus - 05-21-2025, 09:12 AM
RE: Use of Functions - by SMcNeill - 05-21-2025, 09:45 AM
RE: Use of Functions - by bplus - 05-21-2025, 11:23 AM
RE: Use of Functions - by bplus - 05-21-2025, 11:35 AM
RE: Use of Functions - by Kernelpanic - 05-22-2025, 03:00 PM
RE: Use of Functions - by bplus - 05-21-2025, 11:41 AM
RE: Use of Functions - by SMcNeill - 05-21-2025, 11:45 AM
RE: Use of Functions - by bplus - 05-21-2025, 11:55 AM
RE: Use of Functions - by TempodiBasic - 05-21-2025, 12:06 PM
RE: Use of Functions - by mdijkens - 05-21-2025, 12:11 PM
RE: Use of Functions - by TempodiBasic - 05-21-2025, 08:40 PM
RE: Use of Functions - by Dimster - 05-21-2025, 03:34 PM
RE: Use of Functions - by Kernelpanic - 05-21-2025, 06:01 PM
RE: Use of Functions - by CharlieJV - 05-22-2025, 02:37 AM
RE: Use of Functions - by hsiangch_ong - 05-22-2025, 03:30 PM
RE: Use of Functions - by PhilOfPerth - 05-22-2025, 10:22 PM
RE: Use of Functions - by bplus - 05-22-2025, 11:37 PM
RE: Use of Functions - by madscijr - 05-23-2025, 12:34 AM
RE: Use of Functions - by bplus - 05-23-2025, 10:29 AM
RE: Use of Functions - by bplus - 05-23-2025, 10:49 AM
RE: Use of Functions - by bplus - 05-23-2025, 11:22 AM
RE: Use of Functions - by PhilOfPerth - 05-23-2025, 10:16 PM
RE: Use of Functions - by Pete - 05-23-2025, 10:55 PM
RE: Use of Functions - by madscijr - 05-24-2025, 03:10 AM
RE: Use of Functions - by OldMoses - 05-25-2025, 06:05 PM
RE: Use of Functions - by eoredson - 05-26-2025, 08:59 PM
RE: Use of Functions - by PhilOfPerth - 05-28-2025, 03:32 AM
RE: Use of Functions - by Kernelpanic - 05-26-2025, 11:47 PM
RE: Use of Functions - by Pete - 05-27-2025, 11:25 PM
RE: Use of Functions - by bplus - 05-28-2025, 06:17 AM
RE: Use of Functions - by PhilOfPerth - 05-28-2025, 09:11 AM
RE: Use of Functions - by bplus - 05-28-2025, 09:17 AM
RE: Use of Functions - by PhilOfPerth - 05-28-2025, 09:22 AM
RE: Use of Functions - by SMcNeill - 05-28-2025, 09:53 AM
RE: Use of Functions - by PhilOfPerth - 05-28-2025, 10:59 AM
RE: Use of Functions - by Kernelpanic - 05-28-2025, 05:08 PM
RE: Use of Functions - by TempodiBasic - 05-28-2025, 10:18 PM
RE: Use of Functions - by Pete - 05-29-2025, 12:08 AM
RE: Use of Functions - by Pete - 05-29-2025, 12:22 AM
RE: Use of Functions - by PhilOfPerth - 05-29-2025, 02:02 AM
RE: Use of Functions - by PhilOfPerth - 05-29-2025, 02:42 AM
RE: Use of Functions - by SMcNeill - 05-29-2025, 02:38 AM
RE: Use of Functions - by Pete - 05-29-2025, 06:52 PM
RE: Use of Functions - by Kernelpanic - 05-29-2025, 08:21 PM
RE: Use of Functions - by TempodiBasic - 05-30-2025, 01:49 PM



Users browsing this thread: 2 Guest(s)