Solved

Calling a function

Posted on 2003-11-23
7
551 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
[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
  • 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
Independent Software Vendors: 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!

 
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 125 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

[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.

Question has a verified solution.

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

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 tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

617 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