How to Use the Void Function

Void in computer jargon means to not do something. When void is used in a function statement it tells the function to not return a variable, something that a function often does with its return statement. The use of void functions is common in many popular computer languages including JavaScript, ActionScript and C++. In each of these languages, void is an operator in the function that informs the language compiler that the function will not return a variable when called. Void functions are used in most computer programs and to redirect link functions.

Things You'll Need

  • Text editor
  • Internet browser
Show More

Instructions

  1. ActionScript Function Void

    • 1

      Prefix the void operator with a colon in the ActionScript function statement to inform the compiler that the function will not return a value to the calling routine. Construct the void function such that it follows the syntax (without a return statement):

      function <functionName> (arg1:type; arg2:type, ....): void

      { // function instructions;

      }

    • 2

      Use ActionScript 3 to print a message with code that uses a void function. Use the code below to display the text stored in in the "text1" string variable with the void function named "printMessage."

      var text1:" TextField = new TextField();

      text1.text = "you just used your first ActionScript 3 void function";

      printMessage(text1);

      function printMessage(t1:string): void

      (

      trace (t1);

      )

    • 3

      Use an ActionScript 3 void function that rotates a graphic named myStar one degree every time the animation displays a new animation frame (animations are often set to display anywhere from 12 to 40 frames per second).

      stage.addEventListener(Event.ENTER_FRAME, rotatestar);

      function rotatestar(e:Event):void

      {

      myStar.rotation = 1 + myStar.rotation

      }

    JavaScript Void Function

    • 4

      Use the void function in Javascript to create a dummy link, a link that, when clicked, will not redirect the user to another page, but instead does nothing.

      <a href="javascript: void(0)">Dummy Link</a>

    • 5

      Use a Javascript void function in your HTML file to display a message in an alert box when a link is clicked instead of redirecting the user to different page. Use the Javascript "redirected link" code to create a link labeled "Get your student ID pin number" that will display the message "You have been assigned a student id pin number of 1202."

      <a href="javascript: void(DisplayNumber=1202);alert('You have been assigned a student id pin number of '+ DisplayNumber)">Get your student ID pin number </a>

    • 6

      Use a Javascript void function to display the time on a page when a link is clicked instead of redirecting the the user to a different page. Use the code below in your HTML file to create a link labeled "Check the Time" that will display the current day of the week, month, year, hour, minute and second in the time zone you are in.

      <a href="javascript: void(now=new Date());alert('The time is ' + now)">

      Check the Time</a>

Learnify Hub © www.0685.com All Rights Reserved