# How to pass an argument into a function?

• 11-21-2010
mykolg
How to pass an argument into a function?
I was wanting to pass the following into a function, as to reduce file size. It's going to be in a for() statement, therefore I'm confused as how to do so with C. Wasn't sure if a string would work.

Code:

```aIn[i]%10 aIn[i]/10)%10 aIn[i]/100)%10```
Can I just pass the statement in there or do I have to store it to a string? I can see this being very hand for future use, so I would greatly appreciate this concept.
• 11-21-2010
CommonTater
• 11-21-2010
mykolg
Eh, that didn't clarify much for me.
• 11-21-2010
claudiu
You could hardly give a more obscure presentation of your problem. Why don't you start over.
• 11-21-2010
CProgramming11
Okay, it looks like you're trying to pass three integers: one is the remainder of the value stored at a[i] when divided by 10, one the remainder of of a[i]/10 when divided by ten and the third is the remainder of a[i] divided by 100 when divided by 10. So three integers. You need to pass these three integers into your function?

So call your function with the arguments

function (a[i]%10,a[i]/10%10,a[i]/100%10)

and your function will have to be defined with integer parameters

void function (int x, int y, int z)

What does this have to do with strings?
• 11-21-2010
CProgramming11
Quote:

Originally Posted by claudiu
You could hardly give a more obscure presentation of your problem. Why don't you start over.

I think his English is not so good, he's probably struggling to communicate what his problem is.
• 11-21-2010
mykolg
CProgramming11

No, I don't believe that will work because i is changing inside the the for loop, within the function. Therefore the value in not pre determine until it's inside the functions for-loop.

Code:

```function (int aIn[], int aOut[]){         int i, j;         int index=0;         for(j=0; j<=9; j++){                 for(i=0;i<ARRAY_SIZE;i++){                         if((aIn[i]%10) == j){                                 aOut[index] = aIn[i];                                 index++;                         }                 }         } }```
right where aIn[i]%10 is used, I want to be able to change that each time I call this function, aIn[i]%10, aIn[i]/10)%10, aIn[i]/100)%10, respectively. So, I was thinking that assigning ex.
Code:

```char *input = "aIn[i]%10"; function(input, aR, sR);```
Does that help?
• 11-22-2010
CProgramming11
Sounds like you should read that tutorial common tater posted or another tutorial or a book that explains functions.
• 11-22-2010
CProgramming11
And read the string tutorial as well
• 11-22-2010
claudiu
I think what you are trying to do is code reflection (generating variable names and expressions) at run-time. This is not a feature that the C language supports.

You have to think of other ways to improve your program, and most likely you may have to change the current organization of data in order to get better space complexity.
• 11-22-2010
mykolg
CProgramming11
I feel like you are being rather short with your remarks and aren't really helping me understand this better. I think I'm trying to do something that might be impossible and those tutorials are very basic. Therefore if there is something that you know that can be applied to my question, intuatively, then feel free to comment. But just making remarks to read this and that (which I have done prior to posting) isn't answering my question.

claudiu
I was thinking that it was impossible, I appreciate your response. I was trying to be creative and minimize it in that fashion. I might have to find some other way around this. Thank you.
• 11-22-2010
CProgramming11
Okay I know it might not be the most helpful advice, but you sound confused. You're referring to arrays of integers (or at least they appear that way as you're performing arithmetic operations on them) as string. Then you say you want to pass those values into a function. I told you how to pass them, but that wasn't what you wanted. I just assumed you didn't know what was going on or didn't really know what you want so I suggested the tutorials.
• 11-22-2010
CProgramming11
Tutorials are also a good way to find out what C is capable and not capable of performing, if that is the problem.
• 11-22-2010
CommonTater
Quote:

Originally Posted by mykolg
CProgramming11
I feel like you are being rather short with your remarks and aren't really helping me understand this better. I think I'm trying to do something that might be impossible and those tutorials are very basic. Therefore if there is something that you know that can be applied to my question, intuatively, then feel free to comment. But just making remarks to read this and that (which I have done prior to posting) isn't answering my question.

claudiu
I was thinking that it was impossible, I appreciate your response. I was trying to be creative and minimize it in that fashion. I might have to find some other way around this. Thank you.

So, am I correct in understanding that you want to actually change the behaviour of a function by passing in little bits of code for it to crunch away on?

C can't do that...

However; if I'm understaning you correctly you could do this...

Code:

```// number cruncher int  function (int p)   {  int z;         z = p % 10;     // do other stuff       return z;  }```
Ok now to get the effect you want you would call it like this...
Code:

```int q[3]; int a = 1000; // first call q[0] = function (a); // second call q[1] = function (a / 10); // third call q[2] = function (a / 100);```
The thing is that you can pass in numbers and pointers... you can't hand it code to execute. You can do some of the math in the parenthese like I showed you, but you can't ask it to self-modify.