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?
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?
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
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
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.
Re: Beginner question about functions.
Quote:
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.
Re: Beginner question about functions.
Quote:
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.
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.