Thread: User-defined function

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    127

    User-defined function

    Why it doesn't work properly when I put the int into function and function2?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void function ()
    {
        int a, b, c;
        printf("Input 3 intergers: ");
        scanf("%d %d %d",&a, &b, &c);
    } 
    
    void function2 ()
    {
        int a, b, c;
        printf("%d %d %d\n",a, b, c);
    }
    
    int main(void)
    {
        function ();
        function2 ();
        system("pause");
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The variables a,b, and c in function() are not the same variables a,b, and c as in function2().

    You need to explicitly pass arguments between functions, if that is what you intend to do. Just putting two sets of variables with the same name, in two different functions, does not pass values of variables between those functions.

    However you're trying to learn C (be it from a textbook, an online tutorial, or in a teacher) you need to do a bit more work to understand what is actually required. I've seen some rotten textbooks, tutorials, help files, and even bad teachers in my time .... but all of them get this sort of thing right, because it is pretty elementary to C. If you're not understanding yet, then you need to spend more time going through the materials you have, or listening to what your teacher teaches.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    5
    Hi ulti-killer,

    here you can see one way it would work.
    C Tutorial – Functions and Global/Local variables | CodingUnit Programming Tutorials

    But using global variables isn't a good thing. For small programs you can use them but it's better to avoid using global variables whenever you can.

  4. #4
    Registered User
    Join Date
    Jun 2012
    Posts
    127
    I use global variables to pass values of variables between those functions. If global variables isn't a good thing, is there a better solution?

    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    int a, b, c;
    void function ()
    {
    	printf("Input 3 intergers: ");
    	scanf("%d %d %d",&a, &b, &c);
    } 
    
    
    void function2 ()
    {
    	printf("%d %d %d\n",a, b, c);
    }
    
    
    int main(void)
    {
    	function ();
    	function2 ();
    	system("pause");
    }

  5. #5
    Registered User
    Join Date
    Apr 2008
    Location
    Australia
    Posts
    55
    In this instance better solution would be to pass the variables, for example;

    Code:
    void fee(int &a, int &b, int &c) // note pass by reference not value.
    {
      a = 1;
      b = 2;
      c = 3;
    }
    
    void fii(int a, int b, int c)
    {
       printf("%d %d %d\n",a, b, c);
    }
    
    int main()
    {
     int a,b, c;
     fee(a, b, c);
     fii(a,b, c);
       
     return 0;
    }

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Tropod, your fee() function is C++, not C. C does not support pass by reference like that. Try converting your code to C.

    And, whatever you do, add #include <stdio.h> to the top of the file, so you can be sure printf() will work correctly.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. User defined function problem
    By 337higgin in forum C Programming
    Replies: 9
    Last Post: 10-03-2011, 04:01 PM
  2. User defined cos function
    By zfite in forum C Programming
    Replies: 11
    Last Post: 04-03-2011, 02:40 AM
  3. Squareroot User Defined Function
    By Air in forum C Programming
    Replies: 2
    Last Post: 01-25-2009, 05:21 PM
  4. Replies: 14
    Last Post: 03-02-2008, 01:27 PM
  5. Calling a user-defined function in C++
    By brianptodd in forum C++ Programming
    Replies: 3
    Last Post: 10-08-2002, 12:09 PM