Skip to content
MPGHThe Dark Arts
/
RegisterLog in
Forum
Community
What's NewLatest posts across the boardTrendingHottest threads right nowSubscribedThreads you follow
Discussion
GeneralIntroductionsEntertainmentDebate FortFlaming & Rage
Board
News & AnnouncementsMPGH TimesSuggestions & HelpGiveaways
More Sections
Art & Graphic DesignProgrammingHackingCryptocurrency
Hacks & Cheats
Games
ValorantCS2 / CS:GOCall of Duty / WarzoneFortniteApex LegendsEscape From Tarkov
+14 moreLeague of LegendsGTA VMinecraftRustROTMGBattlefieldTroveBattleOnCombat ArmsCrossFireBlackshotRuneScapeDayZDead by Daylight
Resources
Game Hacking TutorialsReverse EngineeringGeneral Game HackingAnti-CheatConsole Game Hacking
Tools
Game Hacking ToolsTrainers & CheatsHack/Release NewsNew
Submit a release →Share your cheat, tool, or config with the community.
AINEW
AI Tools
General & DiscussionPrompt EngineeringLLM JailbreaksHotAI Agents & AutomationLocal / Open Models
AI × Gaming
AI Aimbots & VisionML Anti-CheatGame Bots & Automation
Create
AI Coding / Vibe CodingAI Art & MediaAI Voice & TTS
The AI frontier →Where game hacking meets modern machine learning. Jump in.
Marketplace
Buy & Sell
SellingBuyingTradingUser Services
Trust & Safety
Middleman LoungeMarketplace TalkVouch Copy Profiles
Money
Cryptocurrency TalkCurrency ExchangeWork & Job Offers
Start selling →List accounts, services, and goods. Use the middleman to trade safe.
MPGH The Dark Arts

A community for offensive security research, reverse engineering, and AI.

Community

ForumMarketplaceSearch

Account

RegisterLog in

Legal

Privacy PolicyForum RulesHelp & FAQ
© 2026 MPGH · All rights reserved.Built by the community, for the community. For educational purposes onlyContent is shared for security research and education — we don't condone illegal use. You're responsible for complying with applicable laws. Use at your own risk.
Home › Forum › Programming › C++/C Programming › Problem Learning

Problem Learning

Posts 1–15 of 41 · Page 1 of 3
ilovecookies
ilovecookies
Problem Learning
I'm having a slight problem grasping the entire idea of functions, creating your own functions, and such. Could someone help explain it in a better way? Because the book i'm reading explains it kinda strangely. I'm reading out of the C++ Plus Primer 5th Edition. It doesn't really have any exercises at the end of the chapters, and I was looking for perhaps a good explaination of functions, and maybe a few basic exercises I could do to make sure I understand it.
#1 · 16y ago
B1ackAnge1
B1ackAnge1
A function is an independent piece of code that performs an action and can be called from other parts of your application.

Your program always has a 'main' function that is the core of your program. If you take for instance the basic hello world example that's always floating around everything happens in the main function;
Code:
#include <iostream>
using namespace std;

int main()
{
	cout << "Hello World!" << endl;
        //Don't even bother 'mentioning' system pause or whatever...
        //learn to run it from the command prompt the way it was designed
	return 0;
}
You could break out the part that actually prints out the text into it's own function. Each function has a 'signature' (which is just the basic way it looks/is defined:
RETURNTYPE FunctionName(ArgumentType1 ArgumentName1 etc etc).
Ok so if we think about this for a bit... to print out hello world (simplest way possible) we don't need to return anything (so return type is 'void'), and don't need to pass any arguments to the function so we could define the function like this:
Code:
void PrintHelloWorld()
{
 	cout << "Hello World!" << endl;
}
Now if you also wanted to print out another message in a different circumstance you could make another function
Code:
void PrintHowsItGoing()
{
	cout << "How's it going?" << endl;
}
and you could then call these from within your program like this
Code:
int main()
{
        BOOL myCheck = FALSE;
        //imagine some code here that does something and sets MyCheck to Either TRUE or FALSE;
       if(myCheck == TRUE)
                PrintHelloWorld();
       else
                PrintHowsItGoing();

	//other code here
}
You can also pass parameters to functions and get a value back from a function
Code:
int FunkyNumber(int a, int b)
{
    int c = (a*3) + (b*2);
    return c;
}
This function as you can see returns an 'int' (int in front of the function name) and takes two arguments , namely 'int a' and 'int b'. Then inside the function it does 'something' with them to calculate some kind of funky number and returns that value.
You could call that like this:
Code:
...
int x = FunkyNumber(3,5);
//x now is (3*3) + (5*2) = 19
...


Now a function with 1 line of code is of course trivial and somewhat useless, But imagine if you will that you had to do 'something' that took 500 lines of code. And you had to perform this same operation in 10 different places in your code. You obviously would not want to copy & paste the same code 10 Times (5000 lines of code right there). This would make your code unnecessarily large and would make fixing a bug really hard (you'd have to remember to fix it in all 10 different places!)

So this is why functions are so handy - You can simply call that one 500 line function from everywhere you need it without having to retype everything, and if there's any bugs that require fixing etc, you can simply fix them in one central place.
#2 · edited 16y ago · 16y ago
ilovecookies
ilovecookies
Hey, thanks alot for the great explanation! No less than what i've seen you describe to other people. But now I have one last question regarding functions.

Everytime the function is called, do you have to make a prototype? Or do you only have to make the prototype at the initial use of the function?
#3 · 16y ago
B1ackAnge1
B1ackAnge1
Easiest way to show this is in code.

If you define/implement your function BEFORE the code where you call it, you can just call it without the need for a 'prototype'. For Example:
Code:
void MyFunction() //Function Definition
{
     //DoSomething
}

int main()
{
    ....
    MyFunction();  //Call My Function
    ...
 }
If you have your function defined AFTER the code where you want to call it you usually will need to add a prototype before you call it
Code:
void MyFunction();   //Function ProtoType; Basically saying "Hey you're going to be using a function called 'MyFunction' and this is what the signature looks like, but the actual implementation sits somewhere else in the source.

int main()
{
    ....
    MyFunction();  //Call My Function 
    //If the prototype wasn't defined above it would not know what the    function 'MyFunction' looked like and thus would not compile
    ...
 }

void MyFunction() //Function Definition
{
     //DoSomething
}
#4 · 16y ago
ilovecookies
ilovecookies
Quote Originally Posted by B1ackAnge1 View Post
Easiest way to show this is in code.

If you define/implement your function BEFORE the code where you call it, you can just call it without the need for a 'prototype'. For Example:
Code:
void MyFunction() //Function Definition
{
     //DoSomething
}

int main()
{
    ....
    MyFunction();  //Call My Function
    ...
 }
If you have your function defined AFTER the code where you want to call it you usually will need to add a prototype before you call it
Code:
void MyFunction();   //Function ProtoType; Basically saying "Hey you're going to be using a function called 'MyFunction' and this is what the signature looks like, but the actual implementation sits somewhere else in the source.

int main()
{
    ....
    MyFunction();  //Call My Function 
    //If the prototype wasn't defined above it would not know what the    function 'MyFunction' looked like and thus would not compile
    ...
 }

void MyFunction() //Function Definition
{
     //DoSomething
}
So in the second example you gave, could I still enter an argument for the function?
#5 · 16y ago
B1ackAnge1
B1ackAnge1
Sure, as long as the function prototype and function implementation are written to take that argument. for example:
Code:
void MyFunction(int a, int b);   //Function ProtoType; Basically saying "Hey you're going to be using a function called 'MyFunction' and this is what the signature looks like, but the actual implementation sits somewhere else in the source. 


int main()
{
    ....
    int x = 4;
    int y = 3;
    MyFunction(x,y);  //Call My Function 
    //If the prototype wasn't defined above it would not know what the    function 'MyFunction' looked like and thus would not compile
    ...
 }

void MyFunction(int a, int b) //Function Definition; Now this functions takes 2 integers as arguments
{
     //DoSomething
}
This code below for instance would not work:
Code:
void MyFunction(); 

int main()
{
    ....
    int x = 4;
    int y = 3;
    MyFunction(x,y); //WILL FAIL Since there is no 'MyFunction' that takes 2 integers as arguments
    ...
 }

void MyFunction() //Function Definition; 
{
     //DoSomething
}

This code below Will also not work
Code:
void MyFunction(int a, int b); //Prototype for function with 2 ints, but it's never implemented

int main()
{
    ....
    int x = 4;
    int y = 3;
    MyFunction(x,y); //WILL FAIL Since there is no 'MyFunction' that takes 2 integers as arguments
    ...
 }

void MyFunction() //Function Definition; 
{
     //DoSomething
}
#6 · edited 16y ago · 16y ago
rwkeith
rwkeith
I thought the book explained functions quite well. I did question about the prototypes when I first read the book, but chapter four should give plenty of info about functions.
#7 · 16y ago
LA
lalakijilp
C++ Beginner's Guide

chapter 7 a closer look at functions.
make the mastery check.
if you can make the programs without the first looking at the answers then you understand the basics.
#8 · 16y ago
why06
why06
Damn BA.
Every time I read your posts I think Im learning something for the first time. I already know this stuff and you still impress me!

BTW: you won't mind if I add your explanation to the F.A.Q. ?
#9 · 16y ago
ilovecookies
ilovecookies
Ok, so far this is what I understand of a function. When you need to use a function already defined you don't need to use a prototype, but if you're creating your own function then you need a prototype and an implementation.

In the function header you use void when there is no return value, and should you need a return value you use int or something that will fit your need. Also in the () right after the function you enter arguments if any. If you enter arguments then they also must be described in the prototype and implementation.

Right?
#10 · 16y ago
Hell_Demon
Hell_Demon
Quote Originally Posted by ilovecookies View Post
Ok, so far this is what I understand of a function. When you need to use a function already defined you don't need to use a prototype, but if you're creating your own function then you need a prototype and an implementation.

In the function header you use void when there is no return value, and should you need a return value you use int or something that will fit your need. Also in the () right after the function you enter arguments if any. If you enter arguments then they also must be described in the prototype and implementation.

Right?
Yes, That is correct
#11 · 16y ago
ilovecookies
ilovecookies
Quote Originally Posted by Hell_Demon View Post
Yes, That is correct
Alright, thanks man, i'mma take a swing at making my own function.
#12 · 16y ago
ilovecookies
ilovecookies
Alright, I created my own simple function which was an exercise I found in the book. I wanted me to create a program, which contained a user defined function for converting Furlongs to Feet, well I did it backwards because I couldn't think of anything worth measuring in furlongs. So My program contains a function which converts feet to Furlongs.

It compiled and ran, but I got a couple of warnings kicked back at me by the compiler. So i'm gonna post my code and the errors I received and see if you guys can tell me what i'm doing wrong.

Code:
#include <iostream>
double Ft2Frlng(int);

int main()
{
    using namespace std;
    double Feet;
    cout << "Please enter your height, round to the nearest foot: " << endl;
    cin >> Feet;
    double Furlong = Ft2Frlng(Feet);
    cout << "You are " << Feet << " Feet tall, which equals " << endl;
    cout << Furlong << "Furlongs. Neat huh? And if you're wondering " << endl;
    cout << "what a furlong is, a furlong is a unit of measurment, " << endl;
    cout << "which equals 220 yards, or 660 feet." << endl;
    system("pause");
    return 0;
}

double Ft2Frlng(int ft)
{
       return 0.00151515152 * ft;
}
And here's my errors.

Code:
 
C:\Documents and Settings\**********\Desktop\Furlong Function.cpp In function `int main()':

C:\Documents and Settings\***********\Desktop\Furlong Function.cpp [Warning] passing `double' for converting 1 of `double Ft2Frlng(int)'
I have an idea but i'm not sure. Is it because I was passing Feet, which is the Integer type, to Ft2Frlng which is the double type?
#13 · 16y ago
ZE
zeco
Quote Originally Posted by ilovecookies View Post
Alright, I created my own simple function which was an exercise I found in the book. I wanted me to create a program, which contained a user defined function for converting Furlongs to Feet, well I did it backwards because I couldn't think of anything worth measuring in furlongs. So My program contains a function which converts feet to Furlongs.

It compiled and ran, but I got a couple of warnings kicked back at me by the compiler. So i'm gonna post my code and the errors I received and see if you guys can tell me what i'm doing wrong.

Code:
#include <iostream>
double Ft2Frlng(int);

int main()
{
    using namespace std;
    double Feet;
    cout << "Please enter your height, round to the nearest foot: " << endl;
    cin >> Feet;
    double Furlong = Ft2Frlng(Feet);
    cout << "You are " << Feet << " Feet tall, which equals " << endl;
    cout << Furlong << "Furlongs. Neat huh? And if you're wondering " << endl;
    cout << "what a furlong is, a furlong is a unit of measurment, " << endl;
    cout << "which equals 220 yards, or 660 feet." << endl;
    system("pause");
    return 0;
}

double Ft2Frlng(int ft)
{
       return 0.00151515152 * ft;
}
And here's my errors.

Code:
 
C:\Documents and Settings\**********\Desktop\Furlong Function.cpp In function `int main()':

C:\Documents and Settings\***********\Desktop\Furlong Function.cpp [Warning] passing `double' for converting 1 of `double Ft2Frlng(int)'
I have an idea but i'm not sure. Is it because I was passing Feet, which is the Integer type, to Ft2Frlng which is the double type?
Warnings in VC++ serve to tell the programmer about things that could be potentially bad but the compiler lets you do anyway. I'm not sure about the error though, it seems a bit off from the warnings i usually get. But i think it's complaining about you passing an integer to your function and the possible loss of data from the type conversion
#14 · 16y ago
ilovecookies
ilovecookies
Quote Originally Posted by zeco View Post
Warnings in VC++ serve to tell the programmer about things that could be potentially bad but the compiler lets you do anyway. I'm not sure about the error though, it seems a bit off from the warnings i usually get. But i think it's complaining about you passing an integer to your function and the possible loss of data from the type conversion
Alright, thanks man! I sorta figured it was like that. I just went back through and changed all my integers except for main(); to double, and now i'm not getting the warnings at all.
#15 · 16y ago
Posts 1–15 of 41 · Page 1 of 3

Post a Reply

Similar Threads

  • WPE problem...By styx23 in General Game Hacking
    8Last post 20y ago
  • Problem Wit Hacking ProgramsBy f5awp in General Gaming
    5Last post 20y ago
  • Where could I learn C++? (Beginner, and Advanced stuff)By TsumikiriX in C++/C Programming
    8Last post 20y ago
  • Looking to learn.By SadisticGrin in Hack Requests
    1Last post 20y ago
  • Learn HackingBy Loler in Hack Requests
    2Last post 20y ago

Tags for this Thread

#learning#problem