Announcement

Collapse
No announcement yet.

public private variables

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • public private variables

    I am not sure I am creating a function properly. What is the proper syntax for creating and then calling a function??

    And second how do you create a public variable? Do you
    declare it in the function premain??/

    Thanks,

    John

  • #2
    Hope this helps...

    Creating a function is simple...

    below main (normally how I do it), just create a new function..

    PHP Code:
    function main() {
      ....
    }  
    // end of main

    function mynewfunction() {
      ....
    }  
    //  end of my new function 
    Now, how you use the function and what it is designed to do are up to you.. Remember, all functions are typically designed to RETURN something.. But they don't HAVE to RETURN anything if you don't want them to.

    PHP Code:
    var status "FLAT";  //  this variable is created in GLOBAL scope

    function main() {
      ....
       if (
    time_to_buy) {
         
    status mynewfunction(1);
       }
       if (
    time_to_sell) {
         
    status mynewfunction(-1);
       }
       if (
    time_to_EXIT) {
         
    status mynewfunction(0);
       }

      return 
    status;
    }  
    // end of main

    function mynewfunction(Direction) {
        if (
    Direction 0) { return "LONG"; }
        if (
    Direction 0) { return "SHORT"; }
        if (
    Direction == 0) { return "FLAT"; }
    }  
    //  end of my new function 
    The example above illustrates a couple of things..

    a.. Variable Scope (for Global variables). By declaring a variable (status) outside the MAIN function, you create a global variable that can be used ANYWHERE within your code. In other words, its SCOPE (think of this as EXPOSURE) is GLOBAL (think of this as throughout the entire code).

    b.. We're also showing you how to call and return values from a function. The "mynewfunction" function is designed to change the "status" variable. Notice we are using a "return" statement within "mynewfunction". This causes "mynewfunction" to send the returned value back to the calling function "main". Also, notice in "main" where we have to capture the returned value by using "status = mynewfunction();"

    Another way to do this is without returning anything...

    PHP Code:
    var status "FLAT";  //  this variable is created in GLOBAL scope

    function main() {
      ....
       if (
    time_to_buy) {
         
    mynewfunction(1);
       }
       if (
    time_to_sell) {
         
    mynewfunction(-1);
       }
       if (
    time_to_EXIT) {
         
    mynewfunction(0);
       }

      return 
    status;
    }  
    // end of main

    function mynewfunction(Direction) {
        if (
    Direction 0) { status "LONG"; }
        if (
    Direction 0) { status "SHORT"; }
        if (
    Direction == 0) { status "FLAT"; }
    }  
    //  end of my new function 
    Here is an example of NOT USING RETURN. Can you see the differences??

    BTW, if I declare a variable within a function, it is only available for use WITHIN that function.

    PHP Code:
    function mynewfunction(Direction) {
      var 
    mynewvariable;

        if (
    Direction 0) { status "LONG"; }
        if (
    Direction 0) { status "SHORT"; }
        if (
    Direction == 0) { status "FLAT"; }
    }  
    //  end of my new function 
    the variable "mynewvariable" is only available (or has SCOPE) WITHIN the function. This is because it is declaired withing the function. When you do this, you are declairing variables that are needed to execute the function.

    Variables declaired within "main" operate the same way. They are available (or have SCOPE) only within the main function.

    Hope this helps.
    Brad Matheny
    eSignal Solution Provider since 2000

    Comment

    Working...
    X