Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Calling a function

Posted on 2003-11-23
7
Medium Priority
?
553 Views
Last Modified: 2010-04-15
When I want to call a function. Do you have to send a parameter to it or can you just leave it blank.  It is void function so there will be nothing returned.  
0
Comment
Question by:buckeyes33
  • 3
  • 2
  • 2
7 Comments
 
LVL 24

Expert Comment

by:shivsa
ID: 9806847
it depends on the declration of functions.

if the function is declared to have 2 parameter as input, it should be supplied 2 parameters.
void returning take care of what is expected to return back from function.
0
 
LVL 4

Expert Comment

by:n_fortynine
ID: 9806854
It depends on the so-called parameter list of your function.
For example:
void foo(void);  //equivalent to void foo( ).
Call: foo();       //legal
       foo(someParam); //illegal. foo does not take any parameter.

void foo1(int x); //or any number of parameters.
Call: foo1(); //illegal.
       int a = 0;
       foo1(a); //legal.
       char ch = 'a';
       foo1(ch); //warning. usually type casting is required for parameters whose types don't match that specified in the
                     //parameter list.
       foo1(a, ch); //error. foo1 does not take 2 parameters.

So my advise is, look up the prototype of your function. What types of parameters does it take? And how many?

0
 
LVL 24

Expert Comment

by:shivsa
ID: 9806857
void swap(double& x, double& y)
{                                                         /* exchange the values of x and y */
double temp = x;
x = y;
y = temp;
}


int main(){
 double a = 22.2, b = 44.4;
swap(a,b);
}

here function is void swap( para1, para2)

then here it need 2 input parameters and it will return nothing.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 9

Author Comment

by:buckeyes33
ID: 9806868
I did not make much sense before.  
I calling function X.  function X is going to do all calculations needed and then print results.  This is without using the parameter that is would be sent to function X.

Is there a need to send a parameter to function X since it would not be needed.  
0
 
LVL 4

Accepted Solution

by:
n_fortynine earned 500 total points
ID: 9806945
If your function does not NEED the parameter, but its prototype DEMANDS on, you'll still need to suplly one to it. Even a dummy one.

Another way is to make the function taking in default parameters:
For example:
void foo(int x);
Call: foo(); //error.
       int a = 0;
       foo(a); //OK.

void foo1(int x = 0);
Call: foo1(); //OK now that you don't supply one, if foo1 needs to use x, x will have a value set at 0.
       int a = 2;
       foo1(a); //OK. Now foo1 will use x = 2.
0
 
LVL 4

Expert Comment

by:n_fortynine
ID: 9806949
>>DEMANDS on
should read... DEMANDS one.
0
 
LVL 9

Author Comment

by:buckeyes33
ID: 9806978
ok thanks.
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

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…
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 while-loops in the C programming language.

885 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