Thread: How do you pass variables between functions?

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    6

    How do you pass variables between functions?

    Hi.

    This is what I have so far

    Code:
    #include <stdio.h>
    
    int num1;
    int num2;
    int sum;
          
    int main()
          
    {
          
              scanf("%d", &num1);
              scanf("%d", &num2);
          
              printf("Number 1=%d\n", num1);
              printf("Number 2=%d\n", num2);
          
              addnumbers(int &num1, int &num2);
          
              return 0;
           
    }
          
    int addnumbers()
           
    {
          
              sum = num1 + num2;
          
              printf("sum is %d", sum);
          
              return 0;
           
    }
    I tried to compile it using "gcc calc.c -o calc"

    These are the errors I receive. I really need help understanding what they mean and how to fix the issues.
    Code:
    calc.c: In function ‘main’:
    calc.c:21:5: warning: implicit declaration of function ‘addnumbers’ [-Wimplicit-function-declaration]
         addnumbers(&num1,&num2);
         ^~~~~~~~~~
    Thank you for your time.
    Last edited by tman904; 01-21-2020 at 06:11 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The warning is because you didn't declare the function addnumbers before calling it in the main function. What you need to do is to either forward declare the function (i.e., declare it before main without the function body), or more simply in this case, move the function definition (implementation) to before the main function.

    The first error is simply bad syntax. You need to refer to your learning material as to how to declare and call functions.

    The second error is because you have an extra semi-colon on the line where you start the definition of addnumbers.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2019
    Posts
    6
    Thank you. I saw the semicolon after I first posted. As for the syntax I'll keep working at it. I'm coming from programming mostly in python and shell programming and I'm not used to the huge differences is syntax etc.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It might be helpful to avoid global variables: only declare variables within functions. Then you find out how you can provide addnumbers with two int parameters such that the main function can call it by passing two int arguments.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > How do you pass variables between functions?
    Erm, how did you manage to call printf and scanf then?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    uh...

    Code:
    #include <stdio.h>
    
    int add( int number1, int number2 ) {
      return ( number1 + number2 );
    };
    
    int main( int args, char *argv[] ) {
      printf( "%i" , add( 1, 2 ) );
      return 0;
    };
    "without goto we would be wtf'd"

  7. #7
    Registered User
    Join Date
    Mar 2019
    Posts
    6
    Code:
    #include <stdio.h>
    
    int addnumbers(int num1, int num2)
    
    {
     
        int sum;
    
        sum = num1 + num2;
    
        printf("sum is %d\n", sum);
    
        return 0;
    
    }
    
    int main() 
    
    {
    
        int num1;
        int num2;
    
        scanf("%d", &num1);
        scanf("%d", &num2);
    
        addnumbers(num1, num2); 
    
        return 0;
    
    }
    Is this not suppose to work? Or is there many different ways to code this in order to add the two numbers? I ask since it compiles with no errors/warnings and runs correctly.

    I have a question about
    int addnumbers(int num1, int num2)

    Does this mean the function addnumbers is expecting two variables num1 and num2?

    Also when addnumbers completes what does the return 0 do? Does it return execution to main() or is my program exiting in a way that's bad?
    Last edited by tman904; 01-25-2020 at 06:47 AM.

  8. #8
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338

    Cool

    compiles with no errors/warnings and runs correctly.




    This might help...
    C - Functions - Tutorialspoint
    Last edited by Structure; 01-25-2020 at 08:37 AM.
    "without goto we would be wtf'd"

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Does this mean the function addnumbers is expecting two variables num1 and num2?
    As far as the function is concerned these are what they're called.

    The names of your variables where you call the function is immaterial. So long as they're int's, everything is good.
    For example, addnumbers(3,4); is valid, and there are no named variables at all.

    > Also when addnumbers completes what does the return 0 do?
    In this case, nothing special, because you don't pay attention to the return result in main anyway.
    This is bad form if nothing else.

    Consider:
    Code:
    #include <stdio.h>
    
    // do one thing, and do it well.
    int addnumbers(int num1, int num2)
    {
        int sum;
        sum = num1 + num2;
        return sum;
    }
    
    int main() 
    {
        int num1;
        int num2;
    
        scanf("%d", &num1);
        scanf("%d", &num2);
    
        int sum = addnumbers(num1, num2); 
        printf("sum is %d\n", sum);
    
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to pass external variables to compiled exe-file
    By atztek in forum C Programming
    Replies: 7
    Last Post: 12-08-2015, 02:25 PM
  2. Pass variables between functions
    By Lewis Smith in forum C Programming
    Replies: 9
    Last Post: 06-29-2014, 08:40 AM
  3. I need to find a different way to pass variables
    By sampleandhold in forum C Programming
    Replies: 6
    Last Post: 03-05-2012, 03:45 PM
  4. Replies: 4
    Last Post: 02-14-2012, 07:45 PM
  5. Pass variables to sigaction
    By memcpy in forum C Programming
    Replies: 0
    Last Post: 12-16-2011, 05:29 PM

Tags for this Thread