Skip to main content
GameDev.net gamedev.net
🔒 Locked

function calling = void?

Started by cocopuffs Jun 12, 2010 at 7:25 PM 21 replies 2.3k views
Original Post
cocopuffs
cocopuffs
is void a function calling? if not what is?
right now im new and just want to kno before i move on, and could i use it for a submenu system?

thanks ahead of time and sry for the dumb question
KulSeran
KulSeran
1) your question doesn't make much sense.
2) in c, the keyword 'void' is a type, just like int, float, double, char. 'void' represents "no type". It can be used to indicate a function has no return value, and if you really want to, to indicate a function takes no parameters
"void foo(void) {}"
cocopuffs
cocopuffs
Quote:
Original post by KulSeran
1) your question doesn't make much sense.
2) in c, the keyword 'void' is a type, just like int, float, double, char. 'void' represents "no type". It can be used to indicate a function has no return value, and if you really want to, to indicate a function takes no parameters
"void foo(void) {}"


what i mean is im trying to make a game and i cant make sub-menus and some one mentioned calling function whats that then? and what i mean to go better into detail is, iv bein trying to make a sub menu for one of my menus menus (ex. main menu -> equipment and then -> weapons) i cant seem to get to weapons cuz an if statement can go so far and im just wondering what can i use to make a sub menu system (plz dont make it to complicated im kind of new)

...so the void "type" whats so good about it if it represents a "no type"
oler1s
oler1s
Quote:
what i mean is im trying to make a game and i cant make sub-menus and some one mentioned calling function whats that then?
Have you actually covered the topic of functions in your book? If you haven't, "calling functions" is also going to be a completely alien topic.

If you have, here's an exercise for you. Write a function that adds two integers. Show us code that uses this function to add 5 and 6.

Quote:
so the void "type" whats so good about it if it represents a "no type"
What's the point of a function in the first place?
CodeCriminal
CodeCriminal
Well, to actual programmers "calling function" could be percieved as the act of calling 'a/the' function(s), but in the context of your first post it sounds as though you are talking about the 'return' type of a function. In that case then, 'void' can be used and means that a given function 'foo' does not return a value.

As for your sub-menu system, I have no idea what your talking about, your description is nothing short of poor, lacking information and clarification to appropriatly deduce what your problem is, or may be.

Please elaborate on the following quotations:
. "..if statement can go so far.."
. "..sub menu for one of my menus menus.."

Please could you use a slightly more clearer, more formal approach in your posts, 'cuz' and 'plz' are not words. That may seem a bit nit-picky, however writing with correct spelling and grammer help to convey meaning, thus allowing others to help you with your programming troubles.


Quote:
...so the void "type" whats so good about it if it represents a "no type"


Well, if you had a function that just performed an operation, that did not have a particular result per 'se, the use of void could be deemed appropriate here. In legacy 'C' code, the return type of a function was often an error code, however nowadays C++ provides exception handling and better alternatives to error codes, so this is no longer the case in newer code.

Good luck.
cocopuffs
cocopuffs
Quote:
Original post by oler1s
Quote:
what i mean is im trying to make a game and i cant make sub-menus and some one mentioned calling function whats that then?
Have you actually covered the topic of functions in your book? If you haven't, "calling functions" is also going to be a completely alien topic.

If you have, here's an exercise for you. Write a function that adds two integers. Show us code that uses this function to add 5 and 6.

Quote:
so the void "type" whats so good about it if it represents a "no type"
What's the point of a function in the first place?


i some what have so far im near the array parts i havent gatten to far into it tho and i havent practiced it but i get most of it

idk if this is wrong but is this what u mean?

#include "stdafx.h"
#include "iostream"

using namespace std;

int main()
{
cout << 5 + 6 << endl;
}

cocopuffs
cocopuffs
Quote:
Original post by CodeCriminal
Well, to actual programmers "calling function" could be percieved as the act of calling 'a/the' function(s), but in the context of your first post it sounds as though you are talking about the 'return' type of a function. In that case then, 'void' can be used and means that a given function 'foo' does not return a value.

As for your sub-menu system, I have no idea what your talking about, your description is nothing short of poor, lacking information and clarification to appropriatly deduce what your problem is, or may be.

Please elaborate on the following quotations:
. "..if statement can go so far.."
. "..sub menu for one of my menus menus.."

Please could you use a slightly more clearer, more formal approach in your posts, 'cuz' and 'plz' are not words. That may seem a bit nit-picky, however writing with correct spelling and grammer help to convey meaning, thus allowing others to help you with your programming troubles.


Good luck.


here ill just copy/paste what i have so far.


#include "stdafx.h"
#include "iostream"
#include "ctime"
#include "stdio.h"

int main()
{
using namespace std;


int menu;

do
{
cout << "==============================" << endl;
cout << "|| MAIN MENU ||" << endl;
cout << "==============================" << endl;
cout << "||Please make a selection: ||" << endl;
cout << "||1. create a character ||" << endl;
cout << "||2. shop ||" << endl;
cout << "||3. view stats ||" << endl;
cout << "||4. enter death arena ||" << endl;
cout << "||5. quit game ||" << endl;
cout << "============================== \n" << endl;
cin >> menu;



} while (menu != 1 && menu != 2 &&
menu != 3 && menu != 4 && menu != 5);

switch (menu)
{
case 1:
int nameYourCharacter = 1;


// sub menu character
cout << "" << endl; // space to look better
cout << "==============================" << endl;
cout << "|| CHARACTER MENU ||" << endl;
cout << "==============================" << endl;
cout << "||1. name your character ||" << endl;
cout << "||2. roll for stats ||" << endl;
cout << "||3. back to main menu ||" << endl;
cout << "============================== \n" << endl;
break;


break;

case 2:

//sub menu shop
cout << "" << endl; // space to look better
cout << "==============================" << endl;
cout << "|| SHOP MENU ||" << endl;
cout << "==============================" << endl;
cout << "||1. purchase armor ||" << endl;
cout << "||2. purchase weapon ||" << endl;
cout << "||3. back to main menu ||" << endl;
cout << "==============================" << endl;
break;

case 3:

//sub menu shop
cout << "" << endl; // space to look better
cout << "==============================" << endl;
cout << "|| VIEW STATS ||" << endl;
cout << "==============================" << endl;
cout << "||1. name: ||" << endl;
cout << "||2. level: ||" << endl;
cout << "||3. strength: ||" << endl;
cout << "||4. dexterity: ||" << endl;
cout << "||5. constitution: ||" << endl;
cout << "||6. HP: ||" << endl;
cout << "||7. exp: ||" << endl;
cout << "||8. gold: ||" << endl;
cout << "||9. exit menu ||" << endl;
cout << "==============================" << endl;
break;




}


return 0;
}


what i mean by "if()" is that u can only do if and else, i think this is ok to do but correct me if im wrong, could i nest if statements?

sorry for the grammar im too used to texting.
cocopuffs
cocopuffs
Quote:
Original post by cocopuffs
Quote:
Original post by CodeCriminal
Well, to actual programmers "calling function" could be percieved as the act of calling 'a/the' function(s), but in the context of your first post it sounds as though you are talking about the 'return' type of a function. In that case then, 'void' can be used and means that a given function 'foo' does not return a value.

As for your sub-menu system, I have no idea what your talking about, your description is nothing short of poor, lacking information and clarification to appropriatly deduce what your problem is, or may be.

Please elaborate on the following quotations:
. "..if statement can go so far.."
. "..sub menu for one of my menus menus.."

Please could you use a slightly more clearer, more formal approach in your posts, 'cuz' and 'plz' are not words. That may seem a bit nit-picky, however writing with correct spelling and grammer help to convey meaning, thus allowing others to help you with your programming troubles.


Good luck.


here ill just copy/paste what i have so far.


#include "stdafx.h"
#include "iostream"
#include "ctime"
#include "stdio.h"

int main()
{
using namespace std;


int menu;

do
{
cout << "==============================" << endl;
cout << "|| MAIN MENU ||" << endl;
cout << "==============================" << endl;
cout << "||Please make a selection: ||" << endl;
cout << "||1. create a character ||" << endl;
cout << "||2. shop ||" << endl;
cout << "||3. view stats ||" << endl;
cout << "||4. enter death arena ||" << endl;
cout << "||5. quit game ||" << endl;
cout << "============================== \n" << endl;
cin >> menu;



} while (menu != 1 && menu != 2 &&
menu != 3 && menu != 4 && menu != 5);

switch (menu)
{
case 1:
int nameYourCharacter = 1;


// sub menu character
cout << "" << endl; // space to look better
cout << "==============================" << endl;
cout << "|| CHARACTER MENU ||" << endl;
cout << "==============================" << endl;
cout << "||1. name your character ||" << endl;
cout << "||2. roll for stats ||" << endl;
cout << "||3. back to main menu ||" << endl;
cout << "============================== \n" << endl;
break;


break;

case 2:

//sub menu shop
cout << "" << endl; // space to look better
cout << "==============================" << endl;
cout << "|| SHOP MENU ||" << endl;
cout << "==============================" << endl;
cout << "||1. purchase armor ||" << endl;
cout << "||2. purchase weapon ||" << endl;
cout << "||3. back to main menu ||" << endl;
cout << "==============================" << endl;
break;

case 3:

//sub menu shop
cout << "" << endl; // space to look better
cout << "==============================" << endl;
cout << "|| VIEW STATS ||" << endl;
cout << "==============================" << endl;
cout << "||1. name: ||" << endl;
cout << "||2. level: ||" << endl;
cout << "||3. strength: ||" << endl;
cout << "||4. dexterity: ||" << endl;
cout << "||5. constitution: ||" << endl;
cout << "||6. HP: ||" << endl;
cout << "||7. exp: ||" << endl;
cout << "||8. gold: ||" << endl;
cout << "||9. exit menu ||" << endl;
cout << "==============================" << endl;
break;




}


return 0;
}


what i mean by "if()" is that u can only do if and else, i think this is ok to do but correct me if im wrong, could i nest if statements or switch?
and by what i mean by menus for my menus is this main menu -> equipment and then -> weapons

sorry for the grammar im too used to texting.


oler1s
oler1s
Quote:
i some what have so far im near the array parts i havent gatten to far into it tho and i havent practiced it but i get most of it
What do you mean somewhat? Either you've touched the topic, or you haven't. There's no ambiguity in what a function is.

Quote:
idk if this is wrong but is this what u mean?
No. It doesn't look like you've touched on the topic at all. Thus, you shouldn't worry about functions until you reach that point in your book. Focus on learning what the book shows you, and progress step by step. Don't worry about what someone else knows and would do. You are not him.

Quote:
here ill just copy/paste what i have so far.
As a note, you want to write , , and , not "iostream", "ctime", and "stdio.h".

Your switch/case blocks have a problem. For each case, you have one of two options. You can either have a single statement, or you can have a compound statement. Example:

case 99:    //oneline;    break;


Or:

case  98:{    //line 1;    //line 2;    //etc.;    break;}


Your book should have shown you this distinction. Pay attention to such details. Things like braces and semicolons matter.

Quote:
what i mean by "if()" is that u can only do if and else, i think this is ok to do but correct me if im wrong, could i nest if statements?
Yes.

I can't comment on your code indentation. It's not preserved because you failed to post in CODE or SOURCE tags.
CodeCriminal
CodeCriminal
Yes you can absolutly nest if statements, not only that but you can create if statements that check multiple 'arguments' with the use of logical operators.

Here are some examples:
#include <iostream>int main(int, char**){	int x = 1, y = 0;	// && (Logical AND operator) - evaluate multiple arguments, the if statement will process if the expression evauluates to 'true'	if (x == 1 && y == 0)		std::cout << "Logical AND" << std::endl;	// || (Logical OR operator) - evaluate multiple arguments, the if statement will process if the one of two evaluate to 'true'	if (x == 1 || y == 1)		std::cout << "Logical OR" << std::endl;	// ! (Logical NOT operator) - evaluate argument, will evaluate to true if the expression is false.	if (y != 1)		std::cout << "Logical NOT" << std::endl;	// Nesting if statements	if (x == 1)		if (y == 0)			std::cout << "Nesting without braces" << std::endl;	if (x == 1)	{		if (y == 0)		{			std::cout << "Nesting with braces" << std::endl;		}	}	return 0;}


Try building that code to see the outcome.
cocopuffs
cocopuffs
Quote:
Original post by oler1s
Quote:
i some what have so far im near the array parts i havent gatten to far into it tho and i havent practiced it but i get most of it
What do you mean somewhat? Either you've touched the topic, or you haven't. There's no ambiguity in what a function is.

Quote:
idk if this is wrong but is this what u mean?
No. It doesn't look like you've touched on the topic at all. Thus, you shouldn't worry about functions until you reach that point in your book. Focus on learning what the book shows you, and progress step by step. Don't worry about what someone else knows and would do. You are not him.

Quote:
here ill just copy/paste what i have so far.
As a note, you want to write , , and , not "iostream", "ctime", and "stdio.h".

Your switch/case blocks have a problem. For each case, you have one of two options. You can either have a single statement, or you can have a compound statement. Example:

case 99:    //oneline;    break;


Or:

case  98:{    //line 1;    //line 2;    //etc.;    break;}


Your book should have shown you this distinction. Pay attention to such details. Things like braces and semicolons matter.

Quote:
what i mean by "if()" is that u can only do if and else, i think this is ok to do but correct me if im wrong, could i nest if statements?
Yes.

I can't comment on your code indentation. It's not preserved because you failed to post in CODE or SOURCE tags.


what i ment by your first quote is that i have gotten to functions, i havent gotten too far in functions or practice them, but it doesnt seem to hard, the books i have dont really explain some things good enough, but the rest in my book i get it and i get what arrays do. sorry for not clarifying. thanks for helping me out though and sorry for the questions iv only being usiing c++ for not even a month, but iv learn a lot from this site thanks. anyways sorry for the code, but i did some stuff to it and im trying some stuff out right now with if statments so yea i fixed it,

but anyways can you give me some hints to helping me towards doing submenus?
cocopuffs
cocopuffs
Quote:
Original post by CodeCriminal
Yes you can absolutly nest if statements, not only that but you can create if statements that check multiple 'arguments' with the use of logical operators.

Here are some examples:
*** Source Snippet Removed ***

Try building that code to see the outcome.


so would if statements be better to use nested than switch,for sub menus?
CodeCriminal
CodeCriminal
The switch statement here is fine, and serves its purpose. I would not suggest using a series of if else statements here, let alone nested if statements. They may be needed in the case that you might need to perform some operations prior to other if statements or if 'if statements' first need to meet certain criterion prior to being evaluated. This is not the case with your program because each case does not care for other cases.

I cant think of a good concrete example of thier use right now, and I often dont nest if statements, mainly because I dont need to.
ExcessNeo
ExcessNeo
Quote:
Original post by oler1s
Your switch/case blocks have a problem. For each case, you have one of two options. You can either have a single statement, or you can have a compound statement. Example:

case 99:    //oneline;    break;


Or:

case  98:{    //line 1;    //line 2;    //etc.;    break;}


Your book should have shown you this distinction. Pay attention to such details. Things like braces and semicolons matter.


Actually that's not quite true, a case will continue until it hits a break; this means if you have a few cases which follow the same operation you can write something like:
case 1:case 2:case 3:case 4:    DoSomething();    break;

All 4 cases will invoke DoSomething();
dclyde
dclyde
If you have covered function calling here is a slight clarification on your understanding of functions.

When it was asked for you to provide a function that adds 5 and 6 together and returns the value what you gave us, although technically a function, is not what one would expect.

"main" albeit a function is kind of a special case as it is _the_ function that is used to invoke your program. What we were looking for is a separate function used to do logic for you without writing it inline with the rest of the logic.

What we were expecting was the following:

//Admittedly this function has no real purpose, but we wanted to gauge your knowledge.//The arguments are listed as "void" because it doesn't take any arguments.int AddNumbers( void ){    return 5 + 6;}//A more practical helper functionint AddNumbers( int nArg1, int nArg2 ){    return nArg1 + nArg2;}//How you would call said functionint main( void ){    ...    int nResult = AddNumbers(5, 6); //nResult should be assigned the value of 11    ...}//A function that doesn't return any values (hence a void return type)void PrintName( void ){    cout << "Name";}


In the case of your current task I agree that the switch is fine and that functions aren't entirely necessary. What functions would do, however, is make your switch easier to read.
// Full Sail graduate with a passion for games// This post in no way indicates my being awake when writing it
hasonish
hasonish
I swear to god I've seen this code before, as if somebody else has posted it...*starts rummaging through the forum history*

Ah, found it: here
You had already posted this before, and we actually gave you some pretty good suggestions in the last thread.
I don't see why you didn't take the suggestions, as they were very helpful. =/
--Jason Maurer Budding C++ Programmer
cocopuffs
cocopuffs
Quote:
Original post by hasonish
I swear to god I've seen this code before, as if somebody else has posted it...*starts rummaging through the forum history*

Ah, found it: here
You had already posted this before, and we actually gave you some pretty good suggestions in the last thread.
I don't see why you didn't take the suggestions, as they were very helpful. =/


Sry my friend was the one who posted it for me and then he didn't tell me anything after that so I didn't know he got a respond thanks for the link though, that seemed to help
hasonish
hasonish
Quote:
Original post by cocopuffs
Sry my friend was the one who posted it for me and then he didn't tell me anything after that so I didn't know he got a respond thanks for the link though, that seemed to help

Oh, absolutely. That certainly explains a lot. Glad that I could help though.
--Jason Maurer Budding C++ Programmer

Topic Locked

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

Sign in to reply to this topic.