NASutils Knowledge BaseUnofficialPolski

BASIC · lesson 16 of 30 · Data, functions and the command lineGive a value back

return hands a value to the caller; local keeps a variable inside the sub.

Programming on the NAS › BASIC in 30 lessons

16-functions.yab Download 16-functions.yab

rem return gives a value back; local keeps a variable inside the sub
print human$(900)
print human$(52428800)
print human$(8001563222016)
print percent(93, 120), " percent"

sub percent(part, whole)
  return int(part / whole * 100)
end sub

sub human$(bytes)
  local unit
  unit = 0
  while bytes >= 1024 and unit < 4
    bytes = bytes / 1024
    unit = unit + 1
  wend
  return str$(bytes, "%.1f") + " " + mid$("B  KiBMiBGiBTiB", unit * 3 + 1, 3)
end sub

Terminal

$ yabasic 16-functions.yab
900.0 B  
50.0 MiB
7.3 TiB
77 percent

Exercise

Write max(a, b), which returns the higher of two numbers.