• C

sending a variable to another function

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.
LVL 9
buckeyes33Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

not_an_xpertCommented:
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
not_an_xpertCommented:
0
buckeyes33Author Commented:
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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

AjarCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
not_an_xpertCommented:
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
not_an_xpertCommented:
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
buckeyes33Author Commented:
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
not_an_xpertCommented:
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
buckeyes33Author Commented:
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
buckeyes33Author Commented:
if i put it before the do-while loop then my conditions would not be met and any number would work.  
0
not_an_xpertCommented:
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
buckeyes33Author Commented:
thanks for the help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.