[SOLVED] How to create a function that returns a result?

I want to create a function that returns a result. I want to run the function from the update. Below is an example of what I have in mind, but I don’t know how to make this exactly. Who can help me with this?

// update
Example.prototype.update = function(dt) {
    if (this.randomFunction[result] === true) {
        // code that need a true result from the randomFunction
    }
};
// function
Example.prototype.randomFunction = function() {
    if (this.randomCheck > 0) {
        return true;
    }
    else {
        return false;
    }
};

so there are no static type variables in js, you don’t have name the return value. Your code should work :anguished: or not ?

you don’t have to compare True is true and false is false:

if (this.randomFunction(result) ) { // do something

but you’ve to name the parameter:

Example.prototype.randomFunction = function(result) {

or leaf it out in the call:

if (this.randomFunction() ) { // do something

Example.prototype.randomFunction = function() { // check

Wow, It works! So I almost got it right! Thank you very much @Gurki!

How did you mean this?

I was wondering what the square brackets [ ] in your function call should do?

I’m no coder so maybe, there’s a function call like that

I have no idea. It was just something I thought it should be. How about to get a value?

var Example = pc.createScript('Example');

// initialize
Example.prototype.initialize = function() {
    this.value = 2;
};

// update
Example.prototype.update = function(dt) {
    if (this.randomFunction(> 1)) {
        // code that need a value more then 1 from the randomFunction
    }
};

// function
Example.prototype.randomFunction = function() {
    return this.value;
};

Hi @Albertos,

If you’re always returning a value, you can effectively treat your function as a variable in itself. It would look like (using your example):

var Example = pc.createScript('Example');

// initialize
Example.prototype.initialize = function() {
    this.value = 2;
};

// update
Example.prototype.update = function(dt) {
    if (this.randomFunction() > 1) {
        // code that need a value more then 1 from the randomFunction
    }
};

// function
Example.prototype.randomFunction = function() {
    return this.value;
};

compare like this

Great! it’s working. Thank you @eproasim and @Gurki!

If the function is more complex the result is the whole function instead of just the result that I try to return in the function. Any idea?

In order to evaulate the function and get the result, you have to use brackets () after the function name. If you are trying to output the result to console, then:

console.log(this.randomFunction())

A more readable way is to save the result into a variable and use that instead:

var result = this.randomFunction();
console.log(result);

But the console result is the function itself. I want to get the value that I try to return inside the function.

If you can show how you are trying to print the result to console, I can advice. Otherwise, you must check that you are executing the function and that it actually returns stuff.

It’s working again. I think there was a problem in my function causing the result is invalid and therefore the console showed the function itself?

No, it cannot. If the function has no return statement, e.g. it doesn’t return anything, then it will by default return undefined:

function five() {
  var a = 5;
}
console.log(five()) // undefined

If has a return, then that value is shown:

function five() {
  var a = 5;
  return a;
}
console.log(five());  // 5

If you don’t use () then the function is not executed and it will simply return the function as as a String:

function five() {
  var a = 5;
  return a;
}
console.log(five);  // function five() {
                    //  var a = 5;
                    //  return a;
                    // }
1 Like

I can’t look back anymore, but it looks like I accidentally did the last one in the console.log(). At least that was indeed the result of my console.log().

If you did console.log(this.randomFunction), that would print out the function as you are referencing the function and not executing it.