1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

The Programming Beginner Help Thread

Discussion in 'Entertainment and Technology' started by An Gentleman, Feb 23, 2014.

  1. An Gentleman

    Full Member

    Joined:
    Aug 20, 2013
    Messages:
    1,673
    Likes Received:
    0
    Location:
    Cali
    Gender:
    Male
    Gender Pronoun:
    He
    Sexual Orientation:
    Bisexual
    Out Status:
    A few people
    (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.
     
    #1 An Gentleman, Feb 23, 2014
    Last edited: Feb 23, 2014
  2. mbanema

    Full Member

    Joined:
    Jan 11, 2014
    Messages:
    1,485
    Likes Received:
    30
    Location:
    MA
    Gender:
    Male
    Gender Pronoun:
    He
    Sexual Orientation:
    Gay
    Out Status:
    A few people
    Unfortunately I can't help you with Python. I'm pretty good with HTML/CSS/JS/C# though.
     
  3. Tetraquark

    Tetraquark Guest

    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?
     
  4. An Gentleman

    Full Member

    Joined:
    Aug 20, 2013
    Messages:
    1,673
    Likes Received:
    0
    Location:
    Cali
    Gender:
    Male
    Gender Pronoun:
    He
    Sexual Orientation:
    Bisexual
    Out Status:
    A few people
    ^ 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?
     
  5. 696d676179

    Regular Member

    Joined:
    Feb 13, 2014
    Messages:
    7
    Likes Received:
    0
    Gender:
    Male
    Sexual Orientation:
    Gay
    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.
     
  6. Pret Allez

    Full Member

    Joined:
    Apr 19, 2012
    Messages:
    6,785
    Likes Received:
    67
    Location:
    Seattle, WA
    Gender:
    Female (trans*)
    Gender Pronoun:
    She
    Sexual Orientation:
    Bisexual
    Out Status:
    Some people
    Not that I am aware of. A function may take any number of arguments.
     
  7. An Gentleman

    Full Member

    Joined:
    Aug 20, 2013
    Messages:
    1,673
    Likes Received:
    0
    Location:
    Cali
    Gender:
    Male
    Gender Pronoun:
    He
    Sexual Orientation:
    Bisexual
    Out Status:
    A few people
    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.
     
  8. GreenSkies

    GreenSkies Guest

    Codeacademy has a really great tutorial on Python for beginners. You might want to check that out.
     
  9. Ridiculous

    Full Member

    Joined:
    Dec 8, 2010
    Messages:
    3,583
    Likes Received:
    1
    Location:
    New Zealand
    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.
     
  10. Pret Allez

    Full Member

    Joined:
    Apr 19, 2012
    Messages:
    6,785
    Likes Received:
    67
    Location:
    Seattle, WA
    Gender:
    Female (trans*)
    Gender Pronoun:
    She
    Sexual Orientation:
    Bisexual
    Out Status:
    Some people
    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.
     
  11. Ridiculous

    Full Member

    Joined:
    Dec 8, 2010
    Messages:
    3,583
    Likes Received:
    1
    Location:
    New Zealand
    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.
     
  12. Nikky DoUrden

    Full Member

    Joined:
    Aug 25, 2013
    Messages:
    1,305
    Likes Received:
    0
    Location:
    Mediterranean Sea
    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 ?
     
  13. Ridiculous

    Full Member

    Joined:
    Dec 8, 2010
    Messages:
    3,583
    Likes Received:
    1
    Location:
    New Zealand
    I did the same project using both just to see which one was easiest to work with.
     
  14. WeirdnessMagnet

    Regular Member

    Joined:
    Dec 26, 2011
    Messages:
    479
    Likes Received:
    0
    Location:
    Klein sexuality bottle
    Gender:
    Genderqueer
    Gender Pronoun:
    Other
    Sexual Orientation:
    Bisexual
    Out Status:
    Some people
    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...)
     
    #14 WeirdnessMagnet, Feb 25, 2014
    Last edited: Feb 25, 2014
  15. Nikky DoUrden

    Full Member

    Joined:
    Aug 25, 2013
    Messages:
    1,305
    Likes Received:
    0
    Location:
    Mediterranean Sea
    O.O

    /bow

    openGL made my head spin as it is ... :icon_bigg
     
  16. Geek

    Full Member

    Joined:
    Aug 6, 2013
    Messages:
    372
    Likes Received:
    3
    Location:
    Hawaii
    Gender:
    Male
    Gender Pronoun:
    He
    Sexual Orientation:
    Questioning
    Out Status:
    All but family

    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?"