Jump to content

[SOLVED] quickie regarding functions and if statements


Lodius2000

Recommended Posts

...and you can do anything at zombocom, but, i have a call to a function inside and if statement and the function is defined later in the same if statement, and i am getting a call to undefined function error.

 

 

bu before i go and do all sorts of reworking of code , i would like to know if that is the problem

 

thanks

I have found that the best thing to do is to define functions in an include file that is loaded before you need to use it. To the best of my knowledge there is not any noticeable loss in performance by loading a declared function that is not used.

 

I typically have a standard include file that holds generic functions that may be used site-wide. I will then have several more include files that are specific for parts of the site. I load each of them dynamically by reading the dir where I store includes and then loading what it finds. Makes it nice because I can include files just by placing them in that directory.

 

Nate

I agree that you should bring out your function definitions and simply call them when you need them.

 

You write a function when you're doing something multiple times in multiple places. Don't just write a function because you can. If you're doing something 1 time only in 1 case, don't even write the function. Just leave the code in.

A function definition inside a conditional statement will only be defined when the condition is true and the code in that conditional statement is executed. In this case, a call to the function before the function definition won't work.

 

If your function definition is not within a conditional statement, then it will be found during the parse phase and the definition and a call to it can be in any order in the logic.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.