Core Features

Functions

Introduction to functions in Cambo.

Functions are a fundamental feature in the procedural programming, acting as the primary building blocks used to organize code into logical and reusable code. This allows developers break complex code into smaller and simpler logics.

Simple functions

syntax
return_type function_name(parameter_list){
  # code ...
}
  • return_type can any, void, primitive types, or user-defined types, pointers, etc.
  • function_name must be any valid identifier beside main, see main.
  • parameter_list is optional, can contain none or more parameters.

Example: In this example, we're going to create a sum function to perform addition on two integers.

int sum(int a, int b){
  return a + b;
}

int main(string args[]){
  int result = add(1, 2); # result = 3
  return 0;
}
A value must be returned from a function unless its return type is void, otherwise it will result in compilation error.

Default value parameters

syntax
return_type function_name(type parameter_name = value){

}
  • A default value must be a constant, which is known at compile time.
  • A parameter with default value will become optional, can be skipped when calling.
  • All parameters with default values must be defined at the end of the parameter list.

Example: In this example below, we're going to create a scale function to muliply a number by a factor.

float scale(float a, float factor = 1.0){
  return a * factor;
}

int main(){

  float r1 = scale(3.0);       # r1 = 3.0 * 1.0 = 3.0
  float r2 = scale(3.0, 1.5);  # r2 = 3.0 * 1.5 = 4.5

  return 0;
}

default keyword

default is a keyword used to instruct the compiler to substitute a parameter's default value at the call site.

Example: In this code below, we have a function print_text with 3 parameters, two of which has their own default values.

void print_text(string text, float opacity = 1.0, int size = 12){
  # implementation ...
}

opacity and size have a default value, meaning they can be skipped when calling print_text. However, it's not possible to skip opacity if size need a new value rather than its default one. In a case that you want to keep opacity as default but manually looking for its default value can be a troublesome. This is where the default keyword comes to save the day.

int main(string args[]){

  print_text("hello, world!\n");
  print_text("hello, world!", default, 15);

  return 0;
}
Passing the default keyword removes the need to look for the actual default value of a parameter, as the compiler will substitute it during compilation.
default can not be passed into an argument that does not have a default value.
Copyright © 2026