• We are currently performing site maintenance, parts of civfanatics are currently offline, but will come back online in the coming days. For more updates please see here.

C++ functions

CivFan91

Emperor
Joined
Sep 1, 2005
Messages
1,589
Location
USA
OK, I'm trying to make the long jump from Visual Basic to C++. Let's just say that VB made me familiar with the absolute basic fundamentals, but now that I'm getting serious about this I'm stuck.

OK, I want to be able to use functions in my C++ code. Specifically, I want to be able to get at the return value of a function. However, maybe I'm being too specific in my Google searches, because I'm not getting any useful results.

Can someone please tell me: How, in my int main section, can I access the return values of specific functions?
 
There's really nothing to it. :)

SImply set an variable of the appropriate type equal to the function called:
Code:
int function1(){
...
    return value;
}

...


int funcret;

funcret = function1();

or use it directly:
Code:
if ( function1() > 6 ) myvalue = 27;

Note, these are just crude snippets. Bust surely you can see how to use function retuirn codes, now.
 
Function pointers! WOOOOOOOO!!!!!!

Code:
int (*func)(int, int) = myFunc;

typedef int (*hello)(int, int);
hello func2 = func;
 
If you're moving from VB to C++... be prepared for some unpleasant things. Pointers - those are going to be irritating to understand. Strong typing - VB lets you have it easy with data types, C++ doesn't at all. Strings - manipulating strings in C++ is annoying. Very much so without the STL.

BTW, that function value access in VB and C++ is similar, isn't it?

Code:
Function Square (x as Integer) as Integer
Square = x*x
End Function

Sub Main()
x = 5
y = Square(x)
End Sub

vs.

Code:
int square (int x) {
return x*x;
}

int main () {
int x = 5;
int y = square()
}

Good luck with C++. I assume you already know it, but just in case, here's a good and often-used tutorial. Covers the language itself fairly well, with hardly anything about STL or actually doing something functional.

http://www.cplusplus.com/doc/language/tutorial/
 
int square (int x) {
return x*x;
}

int main () {
int x = 5;
int y = square()
}

That's a bit misleading, IMO. Despite the fact that the variables in your function and the main function use the same name, they are not the same, and the function requires an argument to work. It would be better written as

Code:
int square(int input)
{
return input * input;
}

int main( )
{
int x, y;
x = 5;
y = square(x);

return 0;
}
 
There's no need really to write it like that. The variables are not the same, so there's no need to name them differently, either. Both code pieces produce the same result anyway. And I certainly prefer not to use by-reference arguments for math functions. Might be that my style is off, though.
 
Solver said:
There's no need really to write it like that. The variables are not the same, so there's no need to name them differently, either. Both code pieces produce the same result anyway. And I certainly prefer not to use by-reference arguments for math functions. Might be that my style is off, though.
My point is that it can be confusing to someone who's just learning functions.

I didn't actually mean to make that a by-reference argument since he may not be familiar with that either, it's just habit. :) I tend to always use by-reference arguments since they're faster. Just always make 'em constant unless you actually need to modify them.
 
Actually Solver, you forgot to send your value to the function. It should be this:

Code:
int square (int x) {
return x*x;
}

int main () {
int x = 5;
int y = square([color=red]x[/color])
}
 
Back
Top Bottom