Solved

Calling a function

Posted on 2003-11-23
7
549 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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 and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

792 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