Jump to content

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
https://linustechtips.com/topic/1527684-extreme-c-beginner-question/
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.

MAIN: 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 - Antec HCG Platinum - Manta - Silent Wings Pro 4's enjoyer

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

Current build on PCPartPicker

 

 

HTPC: Ryzen 7 2700X - BeQuiet! Shadow Rock 3 - STRIX X570-F - 3200MHz 32GB Corsair Dominator - 250GB Exceria boot - 500GB SN730 - 1TB Sandisk 3D - 4TB HDD - Limited Edition Vega 64 - Corsair RM750x 80+ Gold - North - Alphacool Apex Stealth Metal - BeQuiet! Light Wings

SetupHisense 55E7NQ - Hisense HS205G

HTPC on PCPartPicker

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 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 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 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 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 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 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 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

×