(Would this go here or the General Help section?) I figure I might as well start this thread, since I am a programming beginner, and, well, I need some help. I don't really get how to make functions in Python. If someone could explain them and how they work, that would be cool. Quick note: this thread isn't just for this one question. It's for everyone with programming questions. Just try not to have too many questions and not enough answers.
The basic syntax for functions in Python is as follows: Code: def function_name(arg1, arg2): #do something return x The "def" is how you tell Python that you're declaring a function, so you always include it when making a function. Next, "function_name" is whatever name you want to give your function -- say, "generate_level" or "distance." Then you list arguments to be passed to the function in the parentheses, separated by a comma. Finally, if you have a value you want the function to return, put "return" followed by the value at the end of the function. Note that Python groups code by line indentation. This means that all code in the function must be indented using the same number of tabs or spaces. A simple program that uses a function could look something like this: Code: def mult(a, b): return a * b print(mult(3, 5)) This will display "15" when executed. Does that answer your question?
^ Yes, that makes sense. I was wondering what those parentheses were for. The function I am trying to program right now is a function that converts a name into a number (Example: converting "Colin" into "6"). Currently, there are 5 arguments in the parentheses. That seems like a bit much to me. Is there a maximum amount of arguments that can go into one function?
Can't you pass an array of strings in as the parameter arguments instead (I've only done a bit of python, I'm mainly .net) to make it a little neater, although depends on what you are doing with the arguments I guess. I found Python once I got my head around it to be a bit of a neat language.
All right, I just programmed the function that I mentioned to you guys earlier (and another one), and I'm feeling great! Thanks for kick-starting my motivation. You guys are awesome. We should let the next questioner have their turn.
There is probably some ridiculous upper limit (i.e. several thousand arguments) where the compiler or interpretor will break, but you're not going to hit it unless you are trying to. 5 arguments isn't very many in the scheme of things and is pretty commonplace - things like the functions that are used in the Windows operating system libraries, or the DirectX libraries have functions with 20+ arguments, where each argument is some unpleasant structure that itself is made up of several dozen variables.
Right, and I'm sorry, Direct3D is just annoying... I prefer X and OpenGL (even if X is stupid). The software that I maintain for my job has function calls generally taking 0-15 arguments.
The DirectX libraries are certainly annoying you are right. They are okay once you get used to them though (as long as you don't need to use a function you haven't seen yet... although most are reasonably similar in what they need passed to them to work). I haven't had much experience with OpenGL, but I don't think I'd rate it as much better. It has been a while since I used it though so I've probably forgotten what it was like.
why would you guys learn both DirectX and openGL ? did one made u gave up so moved to the other ? did u use them for other than academic purposes ?
DirectX is Microsoft-only, so if you want to write cross-platform stuff OpenGL is useful, but DX has some advantages with Windows. As for the limit on the argument number... There is a hardware limit, dependent on the concrete CPU architecture, it's practically unlimited for 32/64 bit x86, but back in the day of DOS it was actually fairly restrictive (passed arguments/pointers+return address+registers for all the nested/recursive calls needed to fit in a 64 kb stack...)
For most programming languages the parentheses are variables that are used in the function. for example in php Code: <?php function get_name($name) { return "Hello $name how are you today?"; } echo get_name("Geek"); ?> for example the code above would say "Hello Geek how are you today?"