We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Add

If you're familiar with Python, you'll be familiar with how Python objects can have an __add__ method. It allows developers to specify custom logic for different types of objects. For example, adding two integers we want to behave like this:

3 + 5
# 8

But with an array, we probably want it to work like this:

[3] + [5]
# [3, 5]

Assignment

This is the final lesson on objects, and it's a big one. Take your time – you'll be using a lot of the functions that you wrote in past chapters to complete this one.

Complete the snek_add function. It accepts two pointers to snek_object_t objects ("a" and "b") and returns a pointer to a new snek_object_t object (the result of the sum):

We won't implement subtract, multiply, or divide but each of these would follow the same pattern. This is how arithmetic operations work in Sneklang!