Click here to Skip to main content
15,899,020 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone I tried making by code here to take ask for a base and then ask for an exponent. But I have to use a void function for it. I tried to use a pass by reference but I'm not entirely sure if I did that quite right. So when I try an error saying
VB
a11p1.c:3:31: error: unknown type name 'a'
a11p1.c:16:30: error: unknown type name 'a'

I'm just assuming that I get this error because I'm using a void function but I really just don't know. So could anyone try and help me fix this program, and if someone could explain to me the point of even having a void function.

XML
#include <stdio.h>
#include <math.h>
void power(double x, double b,a);

int main()
{
int x,b,a;
printf("insert base: ");
scanf("%d",&x);
printf("insert power: ");
scanf("%d",&b);
power(x,b,&a);
printf("anser is %d\n",a);
return 0;
}
void power(double x,double b,a)
{
a=pow(x,b)
}
Posted

In addition to Solution 2:

Void function without a pointer parameters make no sense at all, because you would not be able to return anything. Such void function still can be used, but only for its side effect, such as with puts. But in your case, it makes no sense at all. Let's fix you syntax and try
C++
void power(double x,double b, double a) // MAKES
{                                       // NO
    a = pow(x,b)                        // SENSE!
}

Look, what happens: first, the calls passes some value to a, it is copied on stack and then ignored. Instead, new value is assigned to a, which is later discarded. All data and, value and return address are put on stack, and the call ends, and other functions are called, and the stack memory is reused. All the power :-) goes nowhere.

Note that you cannot afford to keep ignorance on how stack works. All this mechanism of calls and returns, as well as local variables and all function parameters, all that is based on the stack mechanism, which is cast in the architecture of all those CPUs. You need to learn it before you move any further.

Now, next variant, which will work:
C++
void power(double x, double b, double *a) //Ugly
{
    *a = pow(x,b); // Will work, but why?
}
// the call:
double a; // no need to initialize,
          // the value will be ignored
power (2, 3, &a); // a will be 2³
Will it work? Yes. Is it good enough? No! This is what can really make sense:
C++
double power(double x, double b)
{
    return pow(x,b); 
}
But of course, it makes real sense if you not just rename pow(double, double); which is the only semantic meaning of this code sample, but do something really useful. :-)

—SA
 
Share this answer
 
v4
Comments
nv3 18-Apr-15 2:46am    
Very well done!
Sergey Alexandrovich Kryukov 18-Apr-15 18:42pm    
Thank you.
—SA
Your problem is not only with void declarator, but mainly with pointers... :-)
Let start from what you seems to want: it seems that you want create a function that computes the power of x to exponent b and give back the result in the variable a.
Infact you correctly pass the pointer to the variable a:
C++
power(x,b,&a);
printf("anser is %d\n",a);

But you omit to declare it as a pointer and even to handle it as a pointer-
The correct function declaration and programming is:
C++
void power(double x,double b, double *a)    //a is a pointer to double!
{
    *a = pow(x,b);  // the result must be assigned to the pointed value!
}


About the reason for existence of void functions is to allow for functions that simply have nothing to return... :-D

P.S. The reason for which the compiler complains is what already reported in the solution 1: in functions declarations you cannot declare variables of same type dividing them with a comma as you make for variables declaration.
I.e.:
C++
double x, y, z;    //This is allowed x,y and z are all doubles
void power(double x, double b,a);  //This is an error!
 
Share this answer
 
v4
Comments
Sascha Lefèvre 17-Apr-15 15:21pm    
+5
I suspect that the error is here:

C++
void power(double x,double b,a)
                             ^
 
Share this answer
 
Comments
ZurdoDev 17-Apr-15 14:28pm    
I second your suspicion. +5 ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900