Functions

Functions serve to reuse blocks of code without having to write them again.

Declaration#

They are declared by writing a colon : followed by the function name.

:function_name
  instructions
Indentation

Instructions must have more left margin than the : line.

Basic function#

# Function:
:header
  print "-------------------------"
  print "Welcome to DinoCode 🦖 "
  print "-------------------------"
  
# Call:
header
header
header  # Will be printed 3 times
How does it work?

Functions are like coordinates. Each time you call a function, DinoCode knows exactly where to go and what to execute.

header  # DinoCode jumps to the code inside header

Parameters#

The same function can work with different data. You just need to define the parameters it can receive.

:greet name
  print "Hello $name!"

greet "Ismael"
greet "Jessy"

Return values#

You can use a function to process your data and return a result using return.

:triangle_area base height
  return base * height / 2

print "base 10, height 5 = " triangle_area(10 5)
print "base 20.5, height 5.5 = " triangle_area(20.5 5.5)
Procedures

A function without return is called a procedure because it does not return a value.

Calls in expressions#

DinoCode allows omitting delimiters, but only when the context is clear. This context becomes confusing (even for humans) in certain scenarios.

:sum a b
  return a + b

# Parentheses are not required
sum 10 5

# Parentheses are required: Classic style
print sum(10 5)

# Parentheses are required: Dollar style
print $(sum 10 5)
Inference

To understand why delimiters are required or omitted depending on the context, review the Implicit delimiters section.

Functions as variables#

In DinoCode you can assign functions to variables and then execute them using the new name.

:greet name
  print "Hello $name!"

my_function = greet
my_function "Ismael"