UDF

This is a discussion on UDF within the C Programming forums, part of the General Programming Boards category; How to create UDF for nexted loop code?...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    55

    UDF

    How to create UDF for nexted loop code?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Which UDF are you referring to? http://en.wikipedia.org/wiki/UDF

    User Defined Field? User Defined Function? Something else?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    55
    User defined function.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Well . . . are you asking how to create a function, and call it from within a loop?

    Once you've created a function, you can call it from anywhere, nested for loops or otherwise. So perhaps you should read up on creating functions . . . .

    Perhaps you have, say, a 2D array which you're iterating through, and you want to pass one element to a function, and you're not sure of the syntax?

    [edit] If so, here it is:
    Code:
    void function(char c);  /* function prototype */
    
    /* ... */
    
    char data[WIDTH][HEIGHT];
    int x, y;
    
    for(x = 0; x < WIDTH; x ++) {
        for(y = 0; y < HEIGHT; y ++) {
            function(data[x][y]);
        }
    }
    
    /* ... */
    
    void function(char c) {
    
    }
    [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    55
    ok i just want the following code in a function coz i need same thing in other parts of my program. So, i just want to call that function at other parts of the program.
    Code:
    for (k=0; k<N; k++)
    	{
    		for (j=k+1; j<N+1; j++)
    		{
    			dx = fabs(x[k] - x[j]);
    			if (dx > L/2.0){ dx -=L;}
    			dy = fabs(y[k] - y[j]);
    			if (dy > L/2.0){ dy -=L;}
    			dz = fabs(z[k] - z[j]);
    			if (dz > L/2.0){ dz -=L;}
    		}
    	}

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    There are a few things you have to keep in mind when creating a function:

    Firstly, variables do not automatically appear inside a function from other functions (unless the variables are global -- but don't do that). You need to pass them in as parameters. Also, changes to variables made in the function are not typically reflected in the calling function. You need to pass pointers if you want the calling function to see changes made in the function.

    You can also pass information back to the calling function by way of a return value -- but you only get one return value.

    So, here's how you might do this. First, decide on the return type. I'll use void for now, indicating no return type.
    Code:
    void function(void) {
    
    }
    Now pass in the parameters. It's likely you'll need to pass in z.
    Code:
    void function(whatever z[N+1]) {
    
    }
    [If you want to pass in more variables, put a comma after the last variable, and include a type for each variable. For example,
    Code:
    void function(int one, int two, char three) {
    }
    ]
    Then you'll probably need to declare dx, dy, and dy as local variables inside this function.

    BTW, if z has N elements, then z[j] is overflowing the array. If it has N+1 elements, you're safe.

    Finally, I should note that your code is actually does absolutely nothing, except calculate dx, dy, and dz -- but that only once. Because the values of dz etc are overwritten in each loop, you don't need a loop at all if you don't want it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    55
    i basically need loops for the calculation of dx dy and dz. It's bit different then normal caculation.
    Anyway thanx for support. Can you suggest me some websites of UDF? I want to learn it from basics.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    There's a tutorial on this site about functions, but it's not too extensive. http://www.cprogramming.com/tutorial/c/lesson4.html

    The best place to look is probably in a book (no pun intended). You might be able to find an extensive function tutorial online, but a book will probably go into greater depth.

    Hint: if you're searching for information, don't search for "UDF" -- I'd never heard of it before. You'll probably find a lot more by looking for "functions" or "declaring functions in c" or something.

    How are dx, dy, and dz "different"? The fact remains that you're doing an assignment over and over again in the loop, effectively overwriting the values of those variables, as far as I can see.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    55
    It's different in the sense that i want to caculate dx for sites 0&1, 0&2, 0&3... and 1&2, 1&3.... and so on. Here i am trying to caulate intersites distance and correct them according to certain alogrithm.

    Can you suggest me a book then?

    thank you.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    It's been a long time since I looked at a C book -- but you might try the Books Recommendations sticky. C Book Recommendations

    Here's another tutorial (scroll down to "10. Functions"), but it uses bad practises such as implicit int and void main(). http://www.physics.drexel.edu/studen...eral/C_basics/
    But at least it's quite complete.

    Here's a less formal one: http://visualcplus.blogspot.com/2006...ions-in-c.html

    Just keep searching, or buy a C book (I don't know which one, but don't pick something that says "in 24 hours" or "easy visual" or stuff like that . . . .)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. UDF to calculate sine
    By n3cr0_l0rd in forum C Programming
    Replies: 5
    Last Post: 02-26-2009, 07:02 AM
  2. Writing a UDF in C++
    By kripatel in forum Linux Programming
    Replies: 0
    Last Post: 03-22-2008, 04:14 PM
  3. Creating a UDF to create random numbers
    By rgmills in forum C++ Programming
    Replies: 5
    Last Post: 11-15-2005, 10:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21