Trying to understand functions

I am a spaghetti coder. I know I could keep scripting in one (Sub/End Sub) but I am trying to learn why I may need functions within the script and how it may help, or if there is good reason to try and find potential functions within the sub. I have tried to create a functions enclosing the math but I am missing something as they are overlooked/skipped when debugged.
In a nutshell would finding and creating functions within the attached script help or is it acceptable without functions.
Just trying to learn
Thanks-Mike

indent preformatted text by 4 spacesOption Explicit

'Script written by
'Script copyrighted by
'Script version Sunday, September 06, 2015 8:17:28 AM

Call LastFretDistance()

Sub LastFretDistance()

Dim scaleLength : scaleLength = Rhino.GetReal("Enter scaleLength ")
Dim numberFrets : numberFrets = Rhino.GetReal("Enter numberFrets")
Dim lastFretDistance : lastFretDistance = scaleLength - (scaleLength / (2 ^ (numberFrets / 12))) 
Dim Yend : Yend = 2 'Y Work in progress 2" for gui representation. Taper math next
Dim Zend : Zend = 0 'Work in Progress
Dim Xstart : Xstart = 0 'G54 zero
Dim Ystart : Ystart = 0 'G54 zero
Dim Zstart : Zstart = 0 'G54 zero
Dim fretSlot : fretSlot = 0 'Fret location counter
'Var fret @ fretSlot #0 == nut location and will need To be offset by half the mill kerf In the opposite direction of last fret
Dim currPoint : currPoint = scaleLength - fret ' resets scale length math for the next fret during loop
Dim fret : fret = 0 'X recursive location of the next fret by adding Var nextPoint to itself X location during loop
Dim nextPoint : nextPoint = 0 'Math for each frets Location X Position during loop
Dim divMath : divMath = 17.817'Scale length constant (fret 1 =(scale / 17.817) & (fret 2 = (scale - fret 1)/ 18.817)etc..
Rhino.MessageBox("Your G54 location will be = " & "X" & Xstart & " Y" & Ystart & " Z" & Zstart) 'Verify G54 location

Dim nutToLast : nutToLast = Rhino.AddLine(Array(fret, Ystart, Zstart), Array(fret, Yend, Zend))

Do Until fretSlot = numberFrets
	
	nextPoint = currPoint / divMath
	nutToLast = Rhino.AddLine(Array(fret, yStart, zStart), Array(fret, yEnd, zEnd))
	fret = fret + nextPoint
	currPoint = scaleLength - fret ' Math for each frets Location X Position
	fretSlot = fretSlot + 1
	Rhino.Print(fret) 
	Rhino.Print("Fret" & "#" & fretSlot)
	
Loop

'nutToLast = Rhino.AddLine(Array(fret, yStart, zStart), Array(fret, yEnd, zEnd))'Last fret

End Sub

Hi Mike,

In general, you use Functions and Subroutines to encompass code that you are going to use more than once. In doing this, you don’t have to keep re-writing the same code over and over. Plus, you end up with modular code that you might be able to user in another script.

Functions and Subroutines can also be just to organize your code. If you have a large function, it can often be broken up into smaller pieces, which make it easier to read, understand, and maintain.

– Dale

Thanks

Thanks again Dale,
Like creating a custom library of useful things to reach for.

I am reading that functions can pass values and subs do not. That is where I get a bit confused as it seems like subs do convert and pass value when I am converting variable values via math within a sub and getting my desired result. Sorry for the most likely silly questions, just trying to wrap my mind around the concept of a function.-Mike

Both functions and subs can accept values as arguments (input) but that the difference in vb is that a function can return (output) a value while a sub cannot. Otherwise, they are functionally pretty much identical.

Note that in languages like Python, this difference does not exist, there are no “subs” and a function definition (def) always returns something - even if it’s just None (Null) by default if you don’t ask it to do anything else. You are not obliged to do anything with the return, of course… (same in vb).

–Mitch

1 Like

Thanks again for your time

Ok, I understand now.

Function PI(Radius)

	PI = Radius * (4 * Atn(1))
	
End Function

I can call this in new file with below command and it will not work if I set above up as a Sub

Rhino.Print(PI(7.0))

Thanks-Mike

You got it! Functions return values, Subs don’t…

1 Like