Solved

Calling a function

Posted on 2003-11-23
7
548 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

809 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