Thread: Pointer to functions, program adding two numbers

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    23

    Pointer to functions, program adding two numbers

    Hey peoples, this program is supposed to add two numbers n1 and n2 and output it to res

    Code:
    #include <stdio.h>
    #include <stlib.h>
    
    int addNumbers(int n1, int n2, int res);
    
    int main(void)
    {
       int number1;
       int number2;
       int result = 0;
    
       printf("Please enter the first number: ");
       scanf("%d", &number1);
       printf("Please enter the second number: ");
       scanf("%d", &number2);
    
       addNumbers(number1, number2, result);
       printf("The result = %d\n", result);
    
       return 0;
    }
    
    int addNumbers(int n1, int n2, int res)
    {
       return res = n1 + n2
    }
    I've checked the code with another example and it seems all is correct? however, res always ends up in equaling 0? am i missing a line of code, or is my program incorrect?

    Supposedly there are two ways of solving a program like this:
    1. Fix this by using pass by pointer variable instead of pass by value.
    2. Change the addNumbers function so that it returns the result instead of passing it back via the pointer variable.

    Also what do you guys use to code in c, I am using putty and i find it annoying to use.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Since you throw the result away, instead of storing it somewhere, that seems like everything's going to plan.

    If you want to keep the return value, you should assign it to something. Note that changing res inside the function addNumbers doesn't do jack to result in your main, since the function only has a local copy.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    23
    ok then but ow would you store result? since you cannot have any interaction with it, i tried using a scanf but that's just assigning an int to result and then throwing out the number of result with doing the calculation for number1 and number2.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    where_you_want_the_answer_to_go = function_that_returns_an_answer(things_that_go_into_the_function);

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    23
    awesome! thanks for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  4. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 08:45 PM
  5. Replies: 11
    Last Post: 11-12-2001, 05:25 PM