Solved

sending a variable to another function

Posted on 2003-10-26
12
245 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
  • 6
  • 5
12 Comments
 

Expert Comment

by:not_an_xpert
Comment Utility
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
Comment Utility
0
 
LVL 9

Author Comment

by:buckeyes33
Comment Utility
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
 
LVL 6

Accepted Solution

by:
Ajar earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 9

Author Comment

by:buckeyes33
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
thanks for the help.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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 how to use strings and some functions related to them in the C programming language.
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.

728 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

14 Experts available now in Live!

Get 1:1 Help Now