Skip to main content

Functions

The control flow execution in Boson starts form the start function, which has to be defined in the .b file. The language currently the following features in functions:

  • Function Call
  • Function Arguments
  • Return Value

Start

# This is a comment
# The execution starts here
fn start() void {
return;
}

Declaring a function


# Defining a function
# Writing a return statement at the end of the function is mandatory
fn m1() void {
return;
}

# This is a comment
# The execution starts here
fn start() void {
return;
}

Returning a value


# Defining a function
# Writing a return statement at the end of the function is mandatory
fn m1() void {
return;
}

# A function which returns a value of type int
fn m2() int {
return 10;
}

# A function which returns a value of type double
fn m3() double {
return 10.1;
}

# This is a comment
# The execution starts here
fn start() void {
int i = m2(); # i is initilized with 10
double j = m3(); # j is initilized with 10.1
return;
}

Passing Arguments

fn getConstant() double {
return 10.999;
}

fn doSomeProcessing(double v1,double v2) double {
return v1 + v2; # Sum of the two variable
}

fn start() void {
double j = doSomeProcessing(getConstant(),11.0);
return;
}

Recursion

consume fn printint(int i) void;


# Recursively computing fib number
fn fib(int n) int {
if((n == 0) | (n == 1)) { return 1; }
else { return fib(n - 1) + fib(n - 2); }
return -1;
}

fn start() void {
printint(fib(10));
return;
}
note

Look at the Structural Constructs section for more on conditionals