# Does execution of same function in a contract uses different amount of gas each time it's executed

1-Does execution of same function in a contract uses different amount of gas each time it's executed?
2-Also if same function (for eg, transfer() function) is written in two different contracts, will it uses different amount of gas?
3-Mainly on what factors gas consumed for a transaction depends?

1 - yes

2 - yes

3 - The answer to the first two questions is yes because the gas consumed by a function depends on the number of computations done in that function. And that could depend on the input. For example you could have:

``````function loop(uint number){

while(number!=0){
number--;
}

}
``````

It is pretty obvious that if your input is 1000 there will be a lot more computations done thane when the input is 1.

• Thanks. I was concerned about if I have same functions in 2 different contracts and I pass same input parameters so that everything remains same. Can the gas consumed differ? I mean if gas used is contract dependent or time depended? I know of course it depends on amount of calculation performed. But if amount of calculation performed is same , still the gas used may differ or not? – Prashant Prabhakar Singh Aug 1 '16 at 8:58
• If they have the same input and the same code then they should consume the same amount of gas – dragosb Aug 1 '16 at 9:02
• I have created a crypto-currency by following this. While exmining the gas used for each transaction, I noticed that functions like `transfer(..)` , `transferFrom(...)` consume same amount of gas irrespective of amount transferred or account which is as per expectation. But every time I call `approveAndCall(..)` the gas used varies. Also the variation in gas used is around 33% which is quite large. Any explanations why this is happening? – Prashant Prabhakar Singh Aug 4 '16 at 11:07
• in `approveAndCall(..)` what you get different amounts even if you have the same parameters in two different calls? if you do vary some parameters...which one ? – dragosb Aug 4 '16 at 13:24
• Even for same parameters I am getting different amount of gas consumed. But I think even for different calls it should be same. For eg, whether I approve 100 coins to a account or 500, the amount of calculation performed each time should not differ. Like in transfer whether I transfer 50 coins or 500, the gas used is same. So same should be the case with `approveAndCall(..)`. Other functions such as `mintToken(...)` `changeReward()` also use variable gas in different calls. – Prashant Prabhakar Singh Aug 5 '16 at 4:53

When I was trying to call transfer function with same parameters on OMG contract, I saw first call is consuming more gas, but following calls consume a lesser amount than the first but the same amount in all.

Finally I found that as at first call, the destination address didn't exists in contract, adding that to the address list resulted in different execution routine with more gas. But after first transfer, following calls all had same gas amount because of same execution routine.