Link to home
Start Free TrialLog in
Avatar of buckeyes33
buckeyes33

asked on

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.
Avatar of not_an_xpert
not_an_xpert

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;
 }
   
Avatar of buckeyes33

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Ajar
Ajar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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??
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?
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

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.
if i put it before the do-while loop then my conditions would not be met and any number would work.  
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
thanks for the help.