Thread: How to pass an argument into a function?

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    18

    Question 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.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    18
    Eh, that didn't clarify much for me.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You could hardly give a more obscure presentation of your problem. Why don't you start over.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    37
    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?

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    37
    Quote Originally Posted by claudiu View Post
    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.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    18
    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?

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    37
    Sounds like you should read that tutorial common tater posted or another tutorial or a book that explains functions.

  9. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    37
    And read the string tutorial as well

  10. #10
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  11. #11
    Registered User
    Join Date
    Nov 2010
    Posts
    18
    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.

  12. #12
    Registered User
    Join Date
    Oct 2010
    Posts
    37
    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.

  13. #13
    Registered User
    Join Date
    Oct 2010
    Posts
    37
    Tutorials are also a good way to find out what C is capable and not capable of performing, if that is the problem.

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mykolg View Post
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to List Iterator As Function Argument
    By bengreenwood in forum C++ Programming
    Replies: 8
    Last Post: 06-17-2009, 05:30 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  5. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM