0

I have a problem with this simple program because it doesnt give me the true outcome. I just want to sum two arguments in the first function and then use the outcome in the second one. It will be nice to have overall outcome in the main function. Also I would like to ask the same question with arrays.

#include <stdio.h>
#include <stdlib.h>

int sum()
{
int a=2;
int b=3;
int s=a+b;
printf("sum=%d\n",s);
return s;
}

int sum2(int s)
{
int c=5;
int d=c+s;

}


int main(int s,int d)
{
sum();

printf("sum=%d\n",s);

printf("sum2=%d\n",d);

getchar();
return 0;
}
1
  • seems like you are not using sum2 in the pgm.. Commented Aug 27, 2011 at 12:30

4 Answers 4

3

There are many problems with this code:

  • int main(int s, int d) won't do what you think. Command-line arguments to your program come in string format. So you would need to use int main(int argc, char *argv[]).
  • The variables s and d in main() are completely independent to the variables in sum() and sum2(). So changing their values in those functions will not affect the original variables.
  • You're not even calling the second function!

You can do things like this:

int sum(int a, int b)
{
    return a+b;
}

int sum2(int c)
{
    return c+5;
}

int main(void)
{
    int x = 2;
    int y = 3;
    int z = sum(x,y);
    int w = sum2(z);
    printf("z = %d\n", z);
    printf("w = %d\n", w);
}
Sign up to request clarification or add additional context in comments.

Comments

1

First of all, s is a local variable inside sum( ). Hence it cannot be available outside the function.

int sum() {   
   // ..  
   int s = a+b; // local variable, hence local scope  
  // .. 
 } 

Also, secondly, int main(int s,int d) won't work since in command line arguments come in String format. So can't use a int there

Comments

1

I won't tell you the answer (lol but others have) but I'll give you these clues to figuring it out.

Ask your self which functions are returning data and which ones aren't.

Clue: the function needs a return to return some data.

Then ask yourself which functions' returns are actually being used.

Clue: to collect the data returned from a function you need to assign the result to a variable like so

  int i;
  i = somefunct();

Comments

0

You can't access the value of variable 's' outside the function sum() since it is out of scope. You'll have to return the value to your main() function. Also your main function parameters are incorrect. You need something more like this:

#include <stdio.h>
#include <stdlib.h>

int sum(int a, int b)
{
    int s=a+b;
    printf("sum=%d\n",s);
    return s;
}

int sum2(int c, int sum)
{
    return c+sum;
}


int main(int argc, char *argv[])
{
    int val1 = sum(2, 3);
    printf("sum=%d\n",val1);

    int val2 = sum2(5, val1);
    printf("sum2=%d\n", val2);

    getchar();
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.