Beginner question about functions.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Beginner question about functions.

  1. #1
    Join Date
    Aug 2009
    Posts
    6

    Beginner question about functions.

    I'm currently wondering the following:

    I've wrote a couple functions that call other functions, this forces me to add more arguments to the function doing the calling because the function it calls requires the extra arguments.

    I was wondering if there is a way to essentially to get the variables into the function that needs to call another function without adding more arguments.

    I would like to keep the functions I make nice and simple and keep the arguments down to a minimum for future use so I don't have to brush up on a given function because it has a lot of arguments.

    Also I have a general question to any one out there:

    As a beginner to programming, I haven't seen many functions other than my own, so I'm wondering how many arguments would the average function have?

  2. #2
    Join Date
    Jul 2009
    Location
    Genova - Italy
    Posts
    38

    Re: Beginner question about functions.

    I am not really an expert, but I think you could use a scruct.

    You could define a scruct containing all the argouments of the "daughter" functions, and then pass one struct only as argoument.

    Example:
    Code:
    functmother1(int a, int b)
    {
        functdaughter1(int c, int d);
        functdaughter2(int e, int f);
    }
    you could define something like this:
    Code:
    typedef struct {
        int a;
        int b;
        int c;
        int d;
        int e;
        int f;
    } MyStruct ;
    
    functmother1(MyStruct Struct1)
    {
        functdaughter1(int Struct1.c, int Struct1.d);
        functdaughter2(int Struct1.e, int Struct1.f);
    }
    Do you think it could be ok?

  3. #3
    Join Date
    Aug 2009
    Posts
    6

    Re: Beginner question about functions.

    Awesome! Thank you.

    I have never seen scrut before, so I have another question. XD

    Quote Originally Posted by motobizio View Post


    Code:
    functmother1(MyStruct Struct1)
    {
        functdaughter1(int Struct1.c, int Struct1.d);
        functdaughter2(int Struct1.e, int Struct1.f);
    }

    Why did you name the arguments "Struct1.x"? (x being the variable.)

    Also did you mean to say "struct" as opposed to "scruct" at the beginning of your post? lol

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: Beginner question about functions.

    The "dot" index indicates "member-of". If you have a MyStruct object called Struct1, then you get at the members of that using the dot.

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,115

    Re: Beginner question about functions.

    Quote Originally Posted by motobizio View Post
    I am not really an expert, but I think you could use a scruct.

    You could define a scruct containing all the argouments of the "daughter" functions, and then pass one struct only as argoument.

    Example:
    Code:
    functmother1(int a, int b)
    {
        functdaughter1(int c, int d);
        functdaughter2(int e, int f);
    }
    you could define something like this:
    Code:
    typedef struct {
        int a;
        int b;
        int c;
        int d;
        int e;
        int f;
    } MyStruct ;
    
    functmother1(MyStruct Struct1)
    {
        functdaughter1(int Struct1.c, int Struct1.d);
        functdaughter2(int Struct1.e, int Struct1.f);
    }
    Do you think it could be ok?
    Not with that syntax. That doesn't really solve the problem. Everything still needs to be passed into the first function, you're just wrapping it into a struct instead of passing them as separate parameters.

  6. #6
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: Beginner question about functions.

    Quote Originally Posted by ace10414 View Post
    I'm currently wondering the following:

    As a beginner to programming, I haven't seen many functions other than my own, so I'm wondering how many arguments would the average function have?
    They can have as many as necessary. There may be limits imposed by a particular compiler but I'm pretty sure the standard imposes no limit. Many windows API functions have up to 14 parameters. I cant ever remember needing to go much higher than that. Most functions tend to have between 0 and 8 parameters in code that I have written.
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: Beginner question about functions.

    With that in mind, passing a large number of parameters to a function could be an indication of a design flaw. Most functions should fall into one of two categories:

    1) They have extremely limited functionality, and thus need few parameters.
    2) They have only high-level functionality, and thus can have their parameters be objects (structs and classes) which they view opaquely, rather than primitives which they need to do something with.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center