// top level
int stock1 = 0;
// first way - pass by value and get return value
stock1 = alterValueOne(stock1);
// second way - pass a pointer to the variable
alterValueTwo(&stock1);
// functions
int alterValueOne (int input)
{
int temp = input + 7;
return temp;
}
void alterValueTwo (int& input)
{
*input = *input + 7;
}
Moschops
Practically a Posting Shark
889 posts since Sep 2008
Reputation Points: 297
Solved Threads: 170
Skill Endorsements: 5
Did this compile? This is the C forum.
void alterValueTwo (int& input)
mvmalderen
Posting Maven
2,612 posts since Feb 2009
Reputation Points: 2,221
Solved Threads: 280
Skill Endorsements: 36
Oop. Typo from sloppy copying of the example usage. Meant to be a pointer, as used inside the function itself. Read as
void alterValueTwo (int* input)
Moschops
Practically a Posting Shark
889 posts since Sep 2008
Reputation Points: 297
Solved Threads: 170
Skill Endorsements: 5
For the pointer should I declare the stock variables as them as global variables?
Almost certainly not. Create them in the main function, and then pass pointers to them into the functions that need to change them.
and how do i declare more than one variable into the function?
void someFunction(int* firstVariable, double* secondVariable, char* thirdVariable, etc etc....)
Moschops
Practically a Posting Shark
889 posts since Sep 2008
Reputation Points: 297
Solved Threads: 170
Skill Endorsements: 5
are the other languages any easier than c?
Depends on which "other" languages you're thinking about, but in general, yes. C is a simple language, but it's not easy because it doesn't hold your hand. You need to think about and manage a lot of low level things to write good C.
deceptikon
Challenge Accepted
3,454 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 57
I heard Java's syntax is similar to C
It's derived from C, yes. Java as a language is easier than C, in my opinion, but as soon as you throw in the gigantic standard library it gets harder.
deceptikon
Challenge Accepted
3,454 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 57