Solved

sending a variable to another function

Posted on 2003-10-26
12
252 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
 
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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

615 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