Skip to main content
gamedev.net
🔒 Locked

[resolved]

Started by chrisparton1991 7 replies 1.3k views
Original Post
chrisparton1991
chrisparton1991
Hi ppl. I was mucking around with functions and I realised that you can't access an integer/floating point/etc outside of its function :( I know this isn't big news, to you or myself, but is there any workaround to solve my problem? This is my function:

void getTriangleArea(float base, float height, bool output)
{
    float area = (0.5 * base) * height;//formula
    if(output == true)//if you want it outputted
    {
        cout << area << endl;//outputs triangle area
    }
    else//or not
    {}
}



I was trying something like this(assuming my function is in a header file):

#include <iostream>
#include <stdio.h>
#include "myHeader.h"

using namespace std;

int main()
{
getTriangleArea(30, 10, false)
cout << area << endl;

system("pause");
return 0;
}



Of course, that doesn't work and even if it did, it would be useless. Just an example. Can my problem be solved while keeping my function? Or will I have to use some sort of class instead? [Edited by - chrisparton1991 on December 4, 2009 7:18:58 PM]
jeroenb
jeroenb
If you want to output the area in the main program make your program this way:

float getTriangleArea(float base, float height){    return area = (0.5 * base) * height;//formula}


What I did was making it a real function: having a return type (a function without a return type (void) is theoretically not a function but a procedure. Then you can use this function like this:

void main(){   float area = getTriangleArea(30, 10);   cout << area << endl;}
Crafter 2D: the open source 2D game framework ?Github: https://github.com/crafter2d/crafter2d
Twitter: [twitter]crafter_2d[/twitter]
chrisparton1991
chrisparton1991
Thanks for your help, but I can't get it to work how I want it.

I was trying to get it to work in something like this:

#include <iostream>#include <stdio>#include "myHeader.h"getTriangleArea(50, 100, false);float whatever = (area * 2.5);system("pause");return 0;


Once again, just an example ;)

Am I completely missing the point here?
Wan
Wan
The variable area only exists in the scope of the function getTriangleArea.
If you want to be able to access it, you'll have to declare it in a global scope.
This would work for example:

#include <iostream>#include <stdio>#include "myHeader.h"float area = 0.0f;  // declare areavoid getTriangleArea(float base, float height, bool output){    area = (0.5 * base) * height; // don't redeclare    // etc..}int main(){    getTriangleArea(30, 10, false)    cout << area << endl;    // etc..}getTriangleArea(50, 100, false);float whatever = (area * 2.5);


But it would be extremely bad practice. Like previous posters already pointed out, you want the function getTriangleArea to return the area.
jflanglois
jflanglois
Quote:
Original post by chrisparton1991
Thanks for your help, but I can't get it to work how I want it.

I was trying to get it to work in something like this:

*** Source Snippet Removed ***

Once again, just an example ;)

Am I completely missing the point here?

You kind of are. Take a look at Scope and Lifespan. The above should be:
float area = getTriangleArea( 50, 100, false );float whatever = (area * 2.5);

That said, it is possible to do it kind of like you want (edit: WanMaster shows something closer with globals):

struct TriangleArea {  operator()( float base, float height ) {    area = (0.5 * base) * height;  }  float area;};int main() {  TriangleArea ta;  ta( 30, 10 );  cout << ta.area << endl;}
I don't quite see the point of it, though.


jfl.
Zahlman
Zahlman
Quote:
Original post by Anonymous Poster
Did you know threre's a "for beginners" section in these forums?


Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.