Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

What is better and why? Functions

Discussion in 'Scripting' started by FreshTillDeath0, Feb 22, 2013.

  1. FreshTillDeath0

    FreshTillDeath0

    Joined:
    Feb 18, 2013
    Posts:
    69
    Hi i was just wondering what are the advantages and disadvantages of using functions like the ones shown in the example:

    Code (csharp):
    1.  
    2. function Update () {
    3. MousePositions();
    4.    
    5. }
    6.  
    7. function MousePositions(){
    8.  
    9. if(currentCameraPos < maxCameraPos){
    10.         if (Input.GetAxis("Mouse ScrollWheel") < 0)
    11.         {
    12.         transform.Translate(Vector3(0,0 ,-1 * mouseSpeed));
    13.         currentCameraPos++;
    14.         }
    15.      }  
    16.        
    17.     if(currentCameraPos > minCameraPos) {  
    18.         if (Input.GetAxis("Mouse ScrollWheel") > 0)
    19.         {
    20.         transform.Translate(Vector3( 0,0,1 * mouseSpeed));
    21.         currentCameraPos--;      
    22.         }
    23.     }
    24.  
    25. }
    26.  
    Instead of:

    Code (csharp):
    1.  
    2.  
    3. function Update () {
    4.  
    5.     if(currentCameraPos < maxCameraPos){
    6.         if (Input.GetAxis("Mouse ScrollWheel") < 0)
    7.         {
    8.         transform.Translate(Vector3(0,0 ,-1 * mouseSpeed));
    9.         currentCameraPos++;
    10.         }
    11.      }  
    12.        
    13.     if(currentCameraPos > minCameraPos) {  
    14.         if (Input.GetAxis("Mouse ScrollWheel") > 0)
    15.         {
    16.         transform.Translate(Vector3( 0,0,1 * mouseSpeed));
    17.         currentCameraPos--;      
    18.         }
    19.     }
    20. }
    21.  
    Thanks :p
     
  2. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,245
    Just makes your code easier to read when you start to get complex scripts.

    It can get really ugly if you put everything in your update.

    Also sometimes there are other benefits but in your example it is about how you set it out.
     
  3. thommoboy

    thommoboy

    Joined:
    Jul 20, 2012
    Posts:
    59
    I was going to say that too
    it really has no effect on the speed of the script just the readability
     
  4. kingcharizard

    kingcharizard

    Joined:
    Jun 30, 2011
    Posts:
    1,137
    Cleaner code, easier to read, and easier to understand what things do..
     
  5. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,509
    Easier to maintain, less likely to write bugs, easier to modify...

    In the above example it's no big deal, but imagine you have to do something in lots of places in your code - that's where making a function helps. Say I need to check if my cursor is in a box on the screen, but I need to do so in two dozen different places. I could copy the code into those two dozen places, or I could write a function and call that function from those two dozen places. Then...

    - If I write a function and then need to change the code in the future, I only have to change it in one place and everything that calls it is also updated. If I copied it two dozen times, I'd need to update it in two dozen places.

    - On a related note, if it's a function then everything that uses it does so consistently. If it's copied then it's super easy to introduce changes in some copies but not into others.

    - It's pretty hard to accidentally break a function call when modifying nearby code. Comparatively, it's pretty easy to break a bunch of copied statements.

    - If related lines of code are "wrapped up" in a function call you can't accidentally split them apart. If they're just a bunch of lines you can.