Solved

functions??

Posted on 2004-04-29
17
261 Views
Last Modified: 2010-04-02
hi all,

im wondering how can i pass the returned value from a function to another function?? i have this code wich is meant to pass the result value to the last function but its not working!

#include<stdio.h>
#include<conio.h>

int t,j,n,a,b,addit;
char ch;

main()
{
      int add(int,int);
      int sub(int);

        int showresult(int);

      
      
            clrscr();

            printf("Enter first number: ");
            scanf("%d",&a);
            printf("\nEnter second number: ");
            scanf("%d",&b);

                addit=add(a,b);
              int showresult();


      return 0;
}


      

      

      //function add
      int add(int x, int y)
      {
            int resultadd,first,second;
            resultadd=x+y;
             
      return resultadd;
      }

      int showresult(int add)
      {        int ress;
              ress=int add(int xx,int vv);
            

            printf("%d final answer is: ",ress);

        }
0
Comment
Question by:msaa
17 Comments
 
LVL 6

Expert Comment

by:mrdtn
ID: 10954850
1.  In your last function, what are xx and vv?  They are not set and are not passed in.  They are probably initialized to zero by the linker and you always get a zero result.

            int ress;
            ress=int add(int xx,int vv);

2.  showresult takes one argument.  When you called it, you didn't pass one in.  I think you meant to pass in the result of your call to add, i.e. the variable "addit".  Get rid of ress and the call to add from within the showresult and just print the passed in argument, "add".

--

mrdtn
0
 
LVL 4

Accepted Solution

by:
PerryDK earned 20 total points
ID: 10955636

OUTPUT:
//****************************
Enter first number: 6

Enter second number: 4
Adding the two numbers...
Final Answer is 10

Subtracting the two numbers...
Final Answer is 2
//****************************


Is this the output your trying to get...if so here is the code that you want to have.  I'm having a hard time following what you are doing in your code.

If you have questions about it please ask.

#include<stdio.h>
#include<conio.h>

int add(int a, int b)
{
  return a + b;
}
int sub(int a, int b)
{
  return a - b;
}
void showresult(int result)
{
  printf("Final Answer is %d\n\n", result);
}

main()
{
  clrscr();

  printf("Enter first number: ");
  scanf("%d",&a);
  printf("\nEnter second number: ");
  scanf("%d",&b);

  int sum = add(a,b);
  printf("Adding the two numbers...\n");
  showresult(sum);

  int difference = sub(a, b);
  printf("Subtracting the two numbers...\n");
  showresult(difference);

  return 0;
}
0
 
LVL 2

Expert Comment

by:Avik77
ID: 10956528
>> int showresult();
ur compiler should have pointed out this to u.
change it to :
showresult(addit);

>>
int showresult(int add)
     {        int ress;
             ress=int add(int xx,int vv);
         

          printf("%d final answer is: ",ress);

        }

>> int ress;
>> ress=int add(int xx,int vv);
I simply can't understand, u are passing the sum already and again u r calling the add function...just delete the two lines ... Moreover ur showresult should simply display the result .... not required to return anything.... make the return type void.

void showresult(int add)
     {        
          printf("%d final answer is: ",add);
      }

Rightly pointed out by in the previous posts...

Avik.
 

     
0
 

Expert Comment

by:rajeshskuri
ID: 10956908
Hi,
I doubt u r programm has not compiled properly, because of these statements
        int showresult(); /* The showresult invokation */
         ress=int add(int xx,int vv);  /* undefined variables xx and vv */
 So change u r programm.



0
 
LVL 12

Expert Comment

by:stefan73
ID: 10957005
Hi msaa,
You're mixing up prototypes and invocation:

>             int showresult();

Should be just
showresult();

>ress=int add(int xx,int vv);
Should be just ... something else.

Put your protos at the beginning of the source, not in any function (do they have a limited declaration scope when defined in a function? Dunno).

Invocations don't use types, types are implicitly defined by the passed variables or constants.

Cheers,
Stefan
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10958073
Hi,

my little suggestions to add to those before:

>int t,j,n,a,b,addit;
You have addit as a global variable:

Then,you dont need to pass it to your showresult function.addit will be accessible there.

If you dont have addit as a global var.,then you should pass it as  others have pointed out.

Also,since sub() has only one argument,i assume you'll be subtracting the passed value from addit(this can have problems though,for example,calling sub() before add())

your calling and declarations are a bit messed up.correct them as others have already said.

Last,
>printf("%d final answer is: ",addit);

printf("final answer is: %d",addit);

Looks better this way. :~)

0
 

Author Comment

by:msaa
ID: 10963597
Thanks PerryDK, for your nice solution.. well  I've just started function I dont know whats really going on.. thats why im mixin' up ;)

I wonder how can I convert this program to give an option if you want to add or subtract.. by using the getch and loop
0
 
LVL 4

Expert Comment

by:PerryDK
ID: 10965926
#include<stdio.h>
#include<conio.h>

int add(int a, int b)
{
  return a + b;
}
int sub(int a, int b)
{
  return a - b;
}
void showresult(int result)
{
  printf("Final Answer is %d\n\n", result);
}

main()
{
  int option;

  clrscr();

  do
  {
    printf("Menu: \n");
    printf("  1 : addition \n");
    printf("  2 : subtraction \n");
    printf("  3 : quit \n\n");

    scanf("%d", &option);

    if(option == 1 || option == 2)
    {
      int a, b;

      printf("Enter first number: ");
      scanf("%d",&a);
      printf("\nEnter second number: ");
      scanf("%d",&b);

      if(option == 1)
      {
        int sum = add(a,b);
        printf("Adding the two numbers...\n");
        showresult(sum);
      }
      else if(option == 2)
      {
        int difference = sub(a, b);
        printf("Subtracting the two numbers...\n");
        showresult(difference);
      }
    }
    else if(option != 3)
    {
      printf("Invalid option try again\n\n");
    }
  } while(option != 3);

  return 0;
}
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:msaa
ID: 10968460
Thank you very much thats really what i wanted :)
0
 

Author Comment

by:msaa
ID: 10968612
I have one question please.. I would like to understand this part more:
------------------------
  int sum = add(a,b);
-------------------------

is it creating a new function?? what is < int sum ??
0
 
LVL 4

Expert Comment

by:PerryDK
ID: 10968991
You don't create a "new" function.  You provide either a function prototype and then define the actually function with implementation details later.  Or you simply define the actual function with implementation details.   You can think of the a function protoytype as a forwar declaration something that the compiler knows so that you can still make function calls before the compiler gets to compiling the implemention of your code.


//This is a function prototype.  The function is not created
//it is simply being defined.  Hey mr. compiler when ever you see add
//I want to pass in two ints and I expect an int to be returned
int add(int a, int b);

//This is a function definition.  This is where what the function does is implemented.
//The function only has one line of code that returs the sum of two integers;
int add(int a, int b)
{
  return a + b;
}


//this is a function call.  a and b are passed as parameters to the function called add.  The result that is returned from the function called add is stored in the variable called sum.
int sum, a, b;
sum = add(a, b);

0
 

Author Comment

by:msaa
ID: 10969091
Thanks i got it.. but the strange thing is that I couldnt find this declaration in the code: "int sum, a, b;"!!?
0
 
LVL 4

Expert Comment

by:PerryDK
ID: 10969103
a, b were declared here

   if(option == 1 || option == 2)
    {
      int a, b;


sum was declared here
     if(option == 1)
      {
        int sum = add(a,b);
        printf("Adding the two numbers...\n");
        showresult(sum);
      }
0
 

Author Comment

by:msaa
ID: 10969151
thanks walla :)
0
 

Author Comment

by:msaa
ID: 10969518
Just one question came into my mind now whilest working on another program.. if I have a function which has two internal variables how can we make the function return the two values of the variables?? is that possible or do I have to make a seperate function for each variable
0
 

Author Comment

by:msaa
ID: 10969573
another question which is, if I have many functions in a program and I want to pass a specified value from one function to another.. how can I distinguish between them?
0
 
LVL 4

Expert Comment

by:PerryDK
ID: 10969737
Allrigth last question...next time you'll have ask a question and get me more points :)

"if I have a function which has two internal variables how can we make the function return the two values of the variables?? is that possible or do I have to make a seperate function for each variable"

A function can return one or no values.  The type procededed before the function name.
int foo(); //this function returns an int
char foo(); //this function returns a char
void foo(); //this function returns nothing

If you want to return more than one value from a function you will have to do so with a structure.
example

OUTPUT:
sum.a = 30
sum.b = 6
sum.c = c


PROGRAM:
#include<stdio.h>

typedef struct
{
  int a;
  int b;
  char c;
} MY_STRUCT;

MY_STRUCT add(MY_STRUCT ms1, MY_STRUCT ms2)
{
  MY_STRUCT result;
  result.a = ms1.a + ms2.a;
  result.b = ms1.b + ms2.b;
  result.c = ms1.c + ms2.c;

  return result;
}

int main()
{
  MY_STRUCT ms1, ms2;
  MY_STRUCT sum;

  ms1.a = 10;
  ms1.b = 5;
  ms1.c = 'a';

  ms2.a = 20;
  ms2.b = 1;
  ms2.c = 2;

  sum = add(ms1, ms2);
  printf("sum.a = %d\n", sum.a);
  printf("sum.b = %d\n", sum.b);
  printf("sum.c = %c\n", sum.c);
  return 0;
}


Your other option is to pass two pointers to a function and have what the value to which the pointers point to change.  Your probably not ready for pointers at this point in time.

If your using C++ you may also pass the two variables by reference and have the values returned back to you as reference parameters.  As your in the C group I won't get into that either.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now