Skip to content

Built in Functions

Dylan Tuttle edited this page Aug 24, 2022 · 2 revisions

The soup compiler comes with a set of functions built-in for your convenience. Use them, abuse them, you can even redefine them if you'd like. But you should know that if you redefine a built-in function (or equivalently create a function with the same name as one of the built-in functions), you will not be able to call the built-in function from anywhere in the code, because the compiler will assume you're referring to your redefined version.

exit

exit(int exit_code)

soup comes with a function to immediately halt execution of a program and exit with a given exit code. This can be used to write a function that throws errors or simply exit a program early, reporting either a successful or unsuccessful execution.

Consider the following examples:

// This program has performed wonderfully!
// It's time to exit gracefully,
// tell the OS that this execution was successful:
exit(0);
// Oh no, something has gone wrong during the running of this program!
// Tell the OS that this execution was unsuccessful:
exit(1);

Typically, an exit code of 0 indicates that a program executed successfully, and an exit code of any value except zero indicates that a program executed unsuccessfully in some way. The exact meanings of the different exit values are different for many architectures, but on most UNIX systems, here's what some of the error exit codes mean: Exit codes with special meanings.

printf

printf(string string_to_print, ...)

soup comes with a convenient and versatile format printing function. printf is the only permitted variadic function in soup (that is, a function that can accept a variable number of arguments) to allow for optional formatting in the string being printed.

printf accepts a string literal and up to 5 integers as additional optional arguments, which are inserted in order into the string at each occurrence of a set of curly braces {}. Only integers can be passed into printf.

Consider the following examples and their corresponding console output indicated by >>>:

printf("Hello, world!\n");
>>> Hello, world!
printf("x = {}\n", 1);
>>> x = 1
printf("The first five natural numbers are {}, {}, {}, {}, {}\n", 1, 2, 3, 4, 5);
>>> The first five natural numbers are 1, 2, 3, 4, 5
x = 99;
printf("Hopefully x equals 99: {}\n", x);
>>> Hopefully x equals 99: 99
Clone this wiki locally