Are calling functions expensive?

Hello - most of my scripts are segmented into different functions - a lot of it is for easier access/organization later on and some of it is for checking variables at different times.


    InvokeRepeating("Movement",0.0, 0.25); //called 4 times a second.

    InvokeRepeating("CheckIfDoingSomethingElse", 0.0, 2.0); // this one 
//is called once every two seconds since it's less of a priority.

However I also use functions for organization and ease of access.

function Update(){


//////////////////////////call Death again at different points/reasons in script


///////Sometimes just skip death and call



function Death(){


function Remove(){



function Respawn(){}   /// called from different script.

I’m working on a mobile platform so speed is key for me.

Is calling custom functions within a script or called from outside a script slowing down my game? I do this A LOT. Should I ditch the Death() function and just copy paste the code twice in my scripts?

Short version: Don’t duplicate code! Keeping everything nice and tidy into separate functions is actually good coding style (thumbs up for that :slight_smile: ).

In terms of performance: A function call usually means moving the instruction pointer to a different position, so basically 2 instructions (JMP to function, JMP to return) + a little bit of overhead for memory allocation etc (VERY simplified version, but you get the point). Seeing that even a weak android phone is already able to process more than a billion instructions per second you really don’t have to worry about that :slight_smile:

Tip: Things that do take time on a phone are memory operations. If you can, try reusing objects instead of deleting and re-instantiating them again.

Calling a function means starting a subroutine. If you are in the main and you call Fct, the processor will jump to a location in memory pointed at by Fct. The main function is now on hold until that function has returned.

In order to get back to the main,the processor needs to know where the program was before the call. To do so, when calling a function some automatic operations are done. Those are the storage of the program counter, the value in some of the registers and others depending on the processor to be stored in a stack. This stack is filled up at the call of the function and emptied at the returned of the function.

So calling a function has a little overhead but it is negligible as it is in the range of 5 to 10 instructions. If we consider your processor to be a 5 cycle pipeline with no superscalar (the ability of starting an instruction when another is not done yet)
you get 25 to 50 cycles overhead per function per frame. Your computer can probably do 1 to 3 billions of them per second.

You could think that if your function has less than three commands (arbitrary choice) and is not regularly called all over the program, use commands in the main instead.

On the other hand, you lose the main idea of the function which is to be reusable and make maintenance easier.

Now, you may get different views as programming is a little like philosophy, two thoughts may differ but both would be right in the end.