-
August 26th, 2009, 01:56 AM
#1
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?
-
August 26th, 2009, 02:39 AM
#2
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?
-
August 26th, 2009, 10:51 AM
#3
Re: Beginner question about functions.
Awesome! Thank you.
I have never seen scrut before, so I have another question. XD
Originally Posted by motobizio
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
-
August 26th, 2009, 11:05 AM
#4
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.
-
August 26th, 2009, 12:44 PM
#5
Re: Beginner question about functions.
Originally Posted by motobizio
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.
-
August 26th, 2009, 12:54 PM
#6
Re: Beginner question about functions.
Originally Posted by ace10414
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.
-
August 26th, 2009, 01:12 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|