The Sababa Programming Language

Functions

You've already seen some of Sababa's built-in functions! If you've read the preceding chapters, you would have seen the print, def, and list functions in use. This chapter will cover how to create your own functions. The penultimate chapter will be dedicated to built-in functions. But for now, the structure of declaring a function in Sababa.

  • def{func_name} (\ {func_parameters} {actual_func} )

A few things to say about this one. Firstly it's similar to variable declaration.

Secondly, the parameters/arguments and the function itself must be between (\ ).

DO NOT, I REPEAT DO NOT confuse \ for /.
/ is for division, we are using \ which is supposed to represent a lowercase lambda (greek letter which represents functional programming), because functions have some relation to lambda calculus. Yikes! It's not as daunting as it sound, you'll see that functions can be relatively simple further down. I'm assuming that languages such as Python and Ruby which have lambda functions, call it lambda because of its relation to functions. Anyways, back to business. An example of a function declaration in Sababa is as follows:

sababa> def{addNum} (\ {x y} {+ x y})

=> ()

The above function, will add whichever two numbers are given to it in the parameters. We called the function addNum, we said it will take the arguments x and y, and finally we said it will add the two. All in one line!

Calling Functions

Now that we have our function, we'll want to use it. In order to call the previous function we've created called addNum we'll do as follows:

sababa> addNum 10 20

=> 30

sababa> addNum 80 50

=> 130

As predicted, we get the sum of the arguments.

The format for calling a function is:

  • func_name param1 param2

assuming the function even takes parameters. You seperate parameters with a space.

Function Inheritance

Another cool way to manipulate functions in Sababa is using another function as a paramter for a new function. This may take a second to wrap your head around, but it's similar to say child and parent classes when doing OOP (Object Oriented Programming).

I'll just demonstrate with an example:

sababa> def{addNum} (\ {x y} {+ x y})

=> ()

sababa> addNum 10

=> (\ {y} {+ x y})

sababa> def{addNum2} (addNum 10)

=> ()

sababa> addNum2 50

=> 60

We created addNum. I then showed that entering only one parameter when you need two tells you the you are lacking a parameter. We created a second function called addNum2, which inhereted its functionality from addNum. We then combined them and ran them with 1 parameter each.

This can be a powerful tool, the example above was just an example, I hope it conveyed what I was trying to. You can actually do some really cool things with this.