Jump to content

Extreme C++ Beginner Question

Blown Circuit

I'm just beginning to learn C++ and I've run across a block type following the website Geeksforgeeks that I can't really find information on.

<

void func()
{
    //this variable is local to the function func()    
    //and can not be accessed outside this function
    int age = 18;
    cout << age;
}

>

I'm just trying to figure out how void functions. What does it mean?

 

Link to comment
Share on other sites

Link to post
Share on other sites

Like an enclosed conveyor belt with only one entrance that being func(). Inside the enclosure your int age is hiding. It being a void means it doesn't matter how this conveyor belt ends.

Desktop: Ryzen 7 5800X3D - Kraken X62 Rev 2 - STRIX X470-I - 3600MHz 32GB Kingston Fury - 250GB 970 Evo boot - 2x 500GB 860 Evo - 1TB P3 - 4TB HDD - RX6800 - RMx 750 W 80+ Gold - Manta - Silent Wings Pro 4's enjoyer

SetupZowie XL2740 27.0" 240hz - Roccat Burt Pro Corsair K70 LUX browns - PC38X - Mackie CR5X's

Current build on PCPartPicker

 

Link to comment
Share on other sites

Link to post
Share on other sites

45 minutes ago, venomtail said:

Like an enclosed conveyor belt with only one entrance that being func(). Inside the enclosure your int age is hiding. It being a void means it doesn't matter how this conveyor belt ends.

I have no idea what you just said lol  I think I may have found something though.  What I found basically said that it's global.

Link to comment
Share on other sites

Link to post
Share on other sites

This code
<

#include <iostream>
using namespace std;

void func()
{
    //this variable is local to the function func()    
    //and can not be accessed outside this function
    int age = 18;
    cout << age;
}

int main (){
    
    cout << "age is ";
    func();
    
    return 0;
}

>

is supposedly supposed to be local, yet I can access func() in the main block.

Google's telling me it's universal, but Geeksforgeeks is telling me otherwise.

Link to comment
Share on other sites

Link to post
Share on other sites

Void just means that the function returns no value when called. To illustrate, let's imagine you're writing a function that returns a value, like a mathematical function. If you write a function to add two integer numbers together, you declare its return type as int. The add function here gets called and returns the value of both the values added up, so the main function can display it

int add(int a, int b){
    return (a + b);
}

int main() {
    std::cout << add(1, 2);
    return 0;
}

 

If you want to turn the add function into a void, you just have to do the output directly in the function, which then returns nothing else for the rest of the program.

void add(int a, int b){
    std::cout << (a + b);
}

int main() {
    add(1, 2);
    return 0;
}

 

And now a word from our sponsor: 💩

-.-. --- --- .-.. --..-- / -.-- --- ..- / -.- -. --- .-- / -- --- .-. ... . / -.-. --- -.. .

ᑐᑌᑐᑢ

Spoiler

    ▄██████                                                      ▄██▀

  ▄█▀   ███                                                      ██

▄██     ███                                                      ██

███   ▄████  ▄█▀  ▀██▄    ▄████▄     ▄████▄     ▄████▄     ▄████▄██   ▄████▄

███████████ ███     ███ ▄██▀ ▀███▄ ▄██▀ ▀███▄ ▄██▀ ▀███▄ ▄██▀ ▀████ ▄██▀ ▀███▄

████▀   ███ ▀██▄   ▄██▀ ███    ███ ███        ███    ███ ███    ███ ███    ███

 ██▄    ███ ▄ ▀██▄██▀    ███▄ ▄██   ███▄ ▄██   ███▄ ▄███  ███▄ ▄███▄ ███▄ ▄██

  ▀█▄    ▀█ ██▄ ▀█▀     ▄ ▀████▀     ▀████▀     ▀████▀▀██▄ ▀████▀▀██▄ ▀████▀

       ▄█ ▄▄      ▄█▄  █▀            █▄                   ▄██  ▄▀

       ▀  ██      ███                ██                    ▄█

          ██      ███   ▄   ▄████▄   ██▄████▄     ▄████▄   ██   ▄

          ██      ███ ▄██ ▄██▀ ▀███▄ ███▀ ▀███▄ ▄██▀ ▀███▄ ██ ▄██

          ██     ███▀  ▄█ ███    ███ ███    ███ ███    ███ ██  ▄█

        █▄██  ▄▄██▀    ██  ███▄ ▄███▄ ███▄ ▄██   ███▄ ▄██  ██  ██

        ▀███████▀    ▄████▄ ▀████▀▀██▄ ▀████▀     ▀████▀ ▄█████████▄

 

Link to comment
Share on other sites

Link to post
Share on other sites

In both math and computer science a function is a black box where you put things and take things.

 

for example,

f(x)=x+1 -> f(5)=5+1 -> f(5)=6

 

5-->[x+1]->6

 

in C/C++, answers to functions (6 in my example) also need to have a type, like variables (int, float, whatever) -  because how else would a computer store them?

 

int f(int x){
	return x+1;
}

//f = 6
    

 

void is a special type that is used to store nothing - like a complete plain of nonexistnce. Why is that? Because some functions do not need to answer anything, they just need to do the things that are inside them.

Yes, I had an account here before. Do not ask me about something related to current political events in the part of the planet I live in - I wouldn't answer that for my own sake and safety. Feel free to address me with any other kind of questions.

Link to comment
Share on other sites

Link to post
Share on other sites

"void func()" just mean your function doesn't return anything with "return". 

2 hours ago, Nvidiot said:

is supposedly supposed to be local, yet I can access func() in the main block.

The comment refers to the variable "age" in your example. Read up "local, static and global variables in C++" for more info on how to use variables in C++.

 

In my opinion, C++ is a fast, powerful and precise language, that is ugly difficult and occasionally tedious to write. Depending on what you want to do, learning other languages like python might be easier and give more impressive results for less effort on your side.

 

LTT forum is not a good place to get programming advice. There are good programmers (better then me) in forum but it just isn't made for specific programming questions, it doesn't even have things like color formatting for code. I advise specialized forums like stack overflow.

 

For learning C++, there are many resources, I'll get some flak for this suggestion, but if it's for hobby, you can chat with GPT and it will often do a decent job of answering elementary programming questions on C++, because it is trained on sites like this.

Link to comment
Share on other sites

Link to post
Share on other sites

13 hours ago, 05032-Mendicant-Bias said:

"void func()" just mean your function doesn't return anything with "return". 

The comment refers to the variable "age" in your example. Read up "local, static and global variables in C++" for more info on how to use variables in C++.

 

In my opinion, C++ is a fast, powerful and precise language, that is ugly difficult and occasionally tedious to write. Depending on what you want to do, learning other languages like python might be easier and give more impressive results for less effort on your side.

 

LTT forum is not a good place to get programming advice. There are good programmers (better then me) in forum but it just isn't made for specific programming questions, it doesn't even have things like color formatting for code. I advise specialized forums like stack overflow.

 

For learning C++, there are many resources, I'll get some flak for this suggestion, but if it's for hobby, you can chat with GPT and it will often do a decent job of answering elementary programming questions on C++, because it is trained on sites like this.

Thanks, LearnC++ is pretty easy to follow. More than most free C++ material out the for beginners anyway. I was using geektogeeks, but it jump cuts and inserts elements I hadn't learned yet (like the void function that I was having trouble with understanding of which the code came from geektogeeks).  I am learning it as a hobby, but more so to prove to myself that I still have some type of working memory left in my head.  I'm hoping this'll help.

Link to comment
Share on other sites

Link to post
Share on other sites

It just means no output. It just does stuff and ends unlike none void which it does stuff and spits out stuff.

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

"no output" is a bit misleading; "no return" is better. If a method prints stuff to the command line, it produces an "output"; if it alters another class' members that are sent to the GUI, that could also be considered "output".

 

The "return", on the other hand, is what the caller of your method can expect to get back from that method after the method's internal code is executed and no exceptions have been triggered. C++ needs all methods to define the data type of what is expected to be returned, even if the method does not return anything at all. In that case, you need to set that method's return type to "void". The syntax rules for C++ demand that the return type of a method is preceeding the method's name in the definition (and declaration) of that method, that is why void is written before the func(). If func() would be expected to return an integer, you'd write int func(){...}, etc.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×