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

Functions

In C, functions specify the types for their arguments and return value.

float add(int x, int y) {
    return (float)(x + y);
}
  • The first type, float is the return type.
  • add is the name of the function.
  • int x, int y are the parameters to the function, and their types are specified.
  • x + y adds the two arguments together.
  • (float) casts the result to a float.
    • We'll talk more about what cast means later, and the rules for casting to and from certain types.
    • The simple version is that it instructs C to convert the result of x + y to a float value.

Here's how you would call this function:

int main() {
    float result = add(10, 5);
    printf("result: %f\n", result);
    // result: 15.000000
    return 0;
}

It's nice that C functions enforce returning the same type from all return statements, isn't it? In Python, it can be a pain to realize that a function returns different types depending on the path it took.

Assignment

Write a max_sneklang_memory function in the space provided. It should:

Tip

Multiplication is done with the * operator, just like most other languages.