PDA

View Full Version : Help with Void Syntax




tulipano
Jul 8, 2012, 06:13 PM
I am a beginner in coding in objective-C and don't understand why there is an error in this code. I know this is a little obnoxious, but like I said, I am new and want to give everyone the full code so I don't miss something. It is saying that I am using an undeclared identifier in the last part of my code, in the void(s), "theBudget", "dollars", and "foreignCurrency".

#include <stdio.h>

typedef struct {
float exchangeRate;
double budget;
double exchangeTransaction;
} budget;
budget vacationBudgetEurope;
budget vacationBudgetEngland;


void spendDollars(budget *theBudget, double dollars);
void chargeForeignCurrency (budget *theBudget, double foreignCurrency);

int main(int argc, const char * argv[])
{

// Decalartion of Variables
vacationBudgetEurope.exchangeRate = 1.2500;
vacationBudgetEurope.budget = 1000.00;
double numberDollarsEurope = 100;
double numberEuros = 100;

//More declaration, just grouped by similarity
vacationBudgetEngland.exchangeRate = 1.5000;
vacationBudgetEngland.budget = 2000.00;
double numberDollarsEngland = 100;
double numberPounds = 100;


spendDollars (&vacationBudgetEurope, numberDollarsEurope);
printf("Converting %.2f US dollars into euros leaves $%.2f", numberDollarsEurope, vacationBudgetEurope.budget);
chargeForeignCurrency(&vacationBudgetEurope, numberEuros);
printf("Charging %.2f euros leaves $%.2f", numberEuros, vacationBudgetEurope.budget);
spendDollars(&vacationBudgetEngland, numberDollarsEngland);
printf("Converting %.2f US dollars into pounds leaves $%.2f", numberPounds, vacationBudgetEngland.budget);
chargeForeignCurrency(&vacationBudgetEngland, numberPounds);
printf("Charging %.2f pounds leaves $%.2f", numberPounds, vacationBudgetEngland.budget);

void spendDollars(budget *theBudget, double dollars);
{
theBudget->budget -= dollars;
}

void chargeForeignCurrency(budget *theBudget, double foreignCurrency);
{
theBudget->exchangeTransaction = foreignCurrency*theBudget->exchangeRate;
theBudget->budget -= theBudget->exchangeTransaction;
}


return 0;
}



Sydde
Jul 8, 2012, 06:24 PM
Note that a semicolon ends a statement. That is your problem: your routines have no code.

chown33
Jul 8, 2012, 06:27 PM
Those things you call "void(s)" are actually called functions. More specifically, "functions returning void", which means "functions returning nothing".

You can't define functions within functions, which is what you've done. The bodies of the attempted function definitions are enclosed within the { } block of the main function. This is not allowed in C, nor in Objective-C.

Also, you've botched the actual function definitions. As one example, you have this:
void spendDollars(budget *theBudget, double dollars); // misplaced semicolon
{
theBudget->budget -= dollars;
}

The semicolon, which I've hilited in red and added a comment to, is misplaced. By placing it there, you've turned what should be a function definition into a function declaration followed by an unattached code block. Remove the semicolon and you'll have a function definition. That is, the code will then define the function spendDollars to be the immediately following code block.

You will also have to move the function definitions outside of main's function definition block.

If that doesn't make sense, then you should review whatever it is you read while learning how to define functions. If you copied it from a book or tutorial, you didn't copy it correctly.


And please use code tags (http://forums.macrumors.com/showthread.php?t=747660).

jared_kipe
Jul 8, 2012, 06:30 PM
void chargeForeignCurrency(budget *theBudget, double foreignCurrency);
{
theBudget->exchangeTransaction = foreignCurrency*theBudget->exchangeRate;
theBudget->budget -= theBudget->exchangeTransaction;
}


First of all, you're trying to define a function inside another function.
If you wrote a function declaration properly you would know that. (as in without the semi colon.

So basically what you wrote was just.

chargeForeignCurrency(budget *theBudget, double foreignCurrency);

theBudget->exchangeTransaction = foreignCurrency*theBudget->exchangeRate;
theBudget->budget -= theBudget->exchangeTransaction;


All you need to do is move the functions outside of the main code block and drop those semi colons.

Also, you're doing some weird things here. As in building semi-opaque data types (budget) that you modify on your own (e.g. vacationBudgetEurope.exchangeRate = 1.2500;) but then use abstraction to do things like 'spendDollars()'. Very weird.
To make matters worse, spendDollars() doesn't even do any bounds checking to make sure the budget doesn't underflow (become less than 0) or any other error checking which makes me wonder why you bothered with a function in the first place.

----------



EDIT:
Those things you call "void(s)" are actually called functions. More specifically, "functions returning void", which means "functions returning nothing".

You can't define functions within functions, which is what you've done. The bodies of the attempted function definitions are enclosed within the { } block of the main function. This is not allowed in C, nor in Objective-C.

Also, you've botched the actual function definitions. As one example, you have this:
void spendDollars(budget *theBudget, double dollars); // misplaced semicolon
{
theBudget->budget -= dollars;
}

The semicolon, which I've hilited in red and added a comment to, is misplaced. By placing it there, you've turned what should be a function definition into a function declaration followed by an unattached code block. Remove the semicolon and you'll have a function definition. That is, the code will then define the function spendDollars to be the immediately following code block.

You will also have to move the function definitions outside of main's function definition block.

If that doesn't make sense, then you should review whatever it is you read while learning how to define functions. If you copied it from a book or tutorial, you didn't copy it correctly.


And please use code tags (http://forums.macrumors.com/showthread.php?t=747660).

We should work in shifts or something. :cool:

chown33
Jul 8, 2012, 06:38 PM
We should work in shifts or something. :cool:

Like this (http://en.wikipedia.org/wiki/Lucha_libre)?

jared_kipe
Jul 8, 2012, 07:14 PM
Like this (http://en.wikipedia.org/wiki/Lucha_libre)?

Lol totally.

:mad::mad::mad::mad:

tulipano
Jul 8, 2012, 07:15 PM
I appreciate all of the help...and this isn't my code, I'm reading a book o Objective-C and this was the code they told me to write.

chown33
Jul 8, 2012, 07:30 PM
I appreciate all of the help...and this isn't my code, I'm reading a book o Objective-C and this was the code they told me to write.

Exactly which book? Title, author, edition number?

Which page or chapter?

jared_kipe
Jul 8, 2012, 07:52 PM
I appreciate all of the help...and this isn't my code, I'm reading a book o Objective-C and this was the code they told me to write.

I doubt that, either A: you didn't copy it exactly or B: you pieced it together from different pages, and it wasn't clear where exactly the pieces were supposed to fit together. Probably a combination of the two.

tulipano
Jul 9, 2012, 05:02 PM
Jared, you are correct. I am reading it on iBooks and it was on different pages so I didn't notice I put the functions returning void within the main function. Xcode was suggesting putting the semicolon in the incorrect place, so that is why I put it there, due to the suggestion. Like I stated before, I just started coding, just trying to learn the basics...

----------

Another question for you guys, what does the -> symbol even do?

Littleodie914
Jul 9, 2012, 07:17 PM
Jared, you are correct. I am reading it on iBooks and it was on different pages so I didn't notice I put the functions returning void within the main function. Xcode was suggesting putting the semicolon in the incorrect place, so that is why I put it there, due to the suggestion. Like I stated before, I just started coding, just trying to learn the basics...

----------

Another question for you guys, what does the -> symbol even do?No worries - don't take any negative comments too seriously, many who are good at something (and some who are bad) will often too-easily forget that they once were just starting out. :)

The a->b operator is a shorthand for (*a).b:


// These two lines are the same.
int bValue = (*a).b;
int bValue = a->b;


So it's essentially taking a pointer to an object:


MyObject *someObject = new MyObject();


And then dereferencing it:


MyObject *someObject = new MyObject();
MyObject obj = *someObject;


And then accessing a property of that object:


MyObject *someObject = new MyObject();
MyObject obj = *someObject;
obj.b; // This is the same as (*someObject).b, and as someObject->b


It's also used to call methods on the underlying object referenced by a pointer:


MyObject *someObject = new MyObject();
MyObject obj = *someObject;
// These three lines are the same
someObject->doSomething();
(*someObject).doSomething();
obj.doSomething();

tulipano
Jul 9, 2012, 07:45 PM
Not going to lie, that made no sense to me...I just don't understand the language enough yet...teaching myself through a book isn't the best idea, and you can see the struggle...so i beg of you to explain to me a pointer...and if you could look at my second thread it would be of great appreciation

lee1210
Jul 9, 2012, 10:16 PM
A pointer is a variable that, instead of holding a number or character, holds a memory address. What this means is that you can store where in memory a piece of data is stored. This gives you flexibility in many ways, but one of the most common is allowing a variable to be modified when it's passed to a function or storing the address of memory you allocate while your program is running. It can also be very helpful when you need to pass around a very large structure. A pointer will be much smaller, so it's cheaper to move around and still allows access to the structure you need.

Now for an ever-popular metaphor. Your house has an address. You can very easily write this address down and give it to someone, and they can find your house. It would be much harder to move your house or copy it and give it to someone. A small sheet of paper that an address can be written on is like a pointer variable. It can't hold a house, but it can hold the address to a house so you can find it when you need to. You can change the address on the paper, too, if you need to get somewhere else.

In essence, like every kind of variable a pointer holds a number. A pointer happens to be interpreted as an address to a position in memory. Pointers also have a "flavor" which tells you what you'll find at the address. It could be something simple like an int, or it could be a huge, complex structure.

This may not make sense right away, but hopefully it plants a seed that grows as you have more time programming.

-Lee

tulipano
Jul 10, 2012, 05:46 PM
okay, that makes sense. But what is the point of a pointer? (no pun intended)

lee1210
Jul 10, 2012, 09:20 PM
okay, that makes sense. But what is the point of a pointer? (no pun intended)

"the most common is allowing a variable to be modified when it's passed to a function or storing the address of memory you allocate while your program is running. It can also be very helpful when you need to pass around a very large structure. A pointer will be much smaller, so it's cheaper to move around and still allows access to the structure you need."

In Objective-C, all objects live on the heap, so you can only access them via pointers.

In short, the point is to allow programmers to directly store the addresses of variables, functions, blocks of memory (including arrays), and in Obj-C, Objects. If you are completely new to programming, the usefullness may not be immediately apparent. As you program more, you will run into situations where you need pointers (unless you switch to a language like Java that is strictly pass-by-value and obscures object pointers from the programmer). Once you need them to solve a problem what they're good for will be more obvious.

-Lee

tulipano
Jul 11, 2012, 06:27 AM
Okay, thanks.