Solved

sending a variable to another function

Posted on 2003-10-26
12
250 Views
Last Modified: 2010-04-15
Is there a way to send a variable to another function while calling a function?  I know that is very little detail but i don't know how to explain it any better.
0
Comment
Question by:buckeyes33
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
12 Comments
 

Expert Comment

by:not_an_xpert
ID: 9624725
is u wish to send the copy of the variable. as in the variable's value wud not be modified in the function, then

//function declarations
int fun1();
int fun2(int param);

int fun1()
{
    int a = 68;
    int rslt = 0 ;

     rslt = fun2(a);//NOTE: the way a's value  is passed
}
int fun2(int param)//NOTE the signature of the function
{

//sum junk operation.. but note the way param is bein accessed
  if (param)  
     {
        param = param * 2;
        return param;
      }
  return 0;
 }

if u wish to manipulate the variable's contents by sending it to another function, u need to pass its address as a parameter to the function. and in the called function, dereference it.
for eg:

//function declarations
int fun1();
int fun2(int *param);

int fun1()
{
    int a = 68;
    int rslt = 0 ;

     rslt = fun2(&a);//NOTE: passing address of a . see &
}
int fun2(int *param)//NOTE the signature of the function
{

//sum junk operation.. but note the way param is bein accessed
   if (*param) //usage: dereferencing the address
     {
       *param = *param *2;//dereferencing again
         return 1;
      }
  return 0;
 }
   
0
 

Expert Comment

by:not_an_xpert
ID: 9624743
0
 
LVL 9

Author Comment

by:buckeyes33
ID: 9625153
i don't know if that is what i am looking for.  It might be but i am not sure.

here is the part of my code that is relevant


   do
   {
      scanf("%d", &pat_choice);

        if (pat_choice >= 1 && pat_choice <=6)
        {
         menu_choice(int pat_choice);  /*this needs to send the variable pat_choice to the menu_choice function*/
         printf("Enter the size of the pattern: \n\n");
           do
           {
              scanf("%d", &size);
              if (size >= 1 && size <=9)
              /* i need something here to pass the value of size to function menu choice
           }while(size >=1 && size <=9);
        }
        else
        {
        exit (0);
        }
   while(pat_choice < 1 && pat_choice > 7);




so basically i am trying to pass the values of  
int size and int pat_choice to the variable menu_choice

I know that i am hard coding so don't worry about that if you think it is a mistake.

I would think that you would put
menu_choice(int size);     but i don't think that would work b/c there would be two parameters which i beleive there can only be one parameter.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Accepted Solution

by:
Ajar earned 250 total points
ID: 9625189
hey .. I think that you want to pass two values to the function menu_choice();
If this is what you want then read further.

//have two variables in the funciton definition like
void  menu_choice(int  pat_choice, int size)
{
   // DO SOMETHING with  pat_choice and size.
}
//Now in your do loop call the function only once like

  do
   {
      scanf("%d", &pat_choice);

        if (pat_choice >= 1 && pat_choice <=6)
        {
       //ERASE THIS LINE!!  menu_choice(int pat_choice);  /*this needs to send the variable pat_choice to the menu_choice function*/
         printf("Enter the size of the pattern: \n\n");
           do
           {
              scanf("%d", &size);
              if (size >= 1 && size <=9)
                   menu_choice(pat_choice,size);   // CALL THE FUNCTION HERE.
              /* i need something here to pass the value of size to function menu choice
           }while(size >=1 && size <=9);
        }
        else
        {
        exit (0);
        }
   while(pat_choice < 1 && pat_choice > 7);


 
0
 

Expert Comment

by:not_an_xpert
ID: 9625191
if u wish to call menu_choice with with pat_choice as value

all u need to dis is
replace
menu_choice(int pat_choice);  /*this needs to send the variable pat_choice to the menu_choice function*/
in ur code to
menu_choice(pat_choice);
similarly
menu_choice(size);
coz ur function menu_choice takes int as a parameter
and whaddyya mean by
>menu_choice(int size);     but i don't think that would work b/c there would >be two parameters which i beleive there can only be one parameter.

menu_choice(int size);
this means the function declaration.... which takes a paramter of type int
0
 

Expert Comment

by:not_an_xpert
ID: 9625195
or do u want menu_choice to take 2 parameters pat_choice and size?
then u need to
declare
menu_choice(int choice, int size);
and in the defn
menu_choice(int choice, int size)
{


}

and what abt the return type of this function menu_choice??
0
 
LVL 9

Author Comment

by:buckeyes33
ID: 9625223
i want menu_choice to take two variables.  but where would i put the declaring statement?

would i need it to go after my send do-while loop?
0
 

Expert Comment

by:not_an_xpert
ID: 9625235
the declaring statement  of the function can be a global one like how ive defined fun2 in my post  earlier
or b4 ur do-while

void menu_choice(int choice, int size);
paste the above statement in d 2 options i ve mentioned

0
 
LVL 9

Author Comment

by:buckeyes33
ID: 9625237
nevermind my last comment Ajar got what i am thinking. I will try that tommorow when i get the rest of my code writen.  Thanks for the help.  hopefully it works.  We will see.  It is a little late here lol.  My my how time flies when you are stumped by code.
0
 
LVL 9

Author Comment

by:buckeyes33
ID: 9625245
if i put it before the do-while loop then my conditions would not be met and any number would work.  
0
 

Expert Comment

by:not_an_xpert
ID: 9625264
a function declaration is not gonna harm the way ur code works.
its jus a notification u can say that a func named menu_choice exists
with void return type takin 2 ints as paramter.
u need 2 b sure where u have 2 call d function
which i presume lookin at ur code
after u accept size frm user
so u put
menu_choice(pat_choice, size);l
there
0
 
LVL 9

Author Comment

by:buckeyes33
ID: 9630199
thanks for the help.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and reading files 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.

756 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