• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.
    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C++ Void without classes in C++

NathanBelomy

New Coder
So I'm looking through some KDE code and I see void used without a class. I'm new to this kinda stuff, but from the examples I saw, in classes thier were public and private. But they had void under private.

Can someone explain what void does. From what I'm guessing it does is express something. But why use void to express something is the question.
 
D

Deleted member 205

Guest
In C, C++ and Java, Methods, Classes and Functions all share something in common with Variables. It's that they have a Type.

With Methods, Classes and Functions, you have the same Types that you would get for a Variable. That is:
  • int
  • double
  • float
  • boolean
  • string
  • char
These are all the Types you can get with Classes, Methods and Functions. These Types are the same that you use with Variables.

But, the only Type that you don't see being used in Variables and that is: void

void is generally a special one. When you create a Class, Method or Function, you specify it's Type. And when you do specify it's Type, it knows to return a value of that Type when it stops running. So, let's say we have a Function. int Addition(), in this case. Addition() knows that as soon as it's done running it's course and when it's called for, it will return an Integer value.

But, if I declared Addition() to be of the Type: void, I'm essentially saying: "When I call for you and/or when you're done running your course, you do not return any value of any Type whatsoever." That's generally how void works. It doesn't return anything at all.

Hope this answers your question.
 

NathanBelomy

New Coder
I get that void doesn't return a value. But can't you just express a variable as a function. I guess you use void when you are within a function and wish to just express something. I guess I'll figure it out, once I start programming. I just don't see why I ever need to use void.
 
D

Deleted member 205

Guest
NathanBelomy said:
But can't you just express a variable as a function
I generally don't get what you mean by this. Are you happening to be referring to C and C++'s #define Preprocessor?

NathanBelomy said:
I just don't see why I ever need to use void.
The only time you'd really use void is when you don't really know what to define the Type of a Function, Class or Method as or you don't want the Class, Function or Method to return anything. That's really only when you'd use void.
 

NathanBelomy

New Coder
Oh define, is to define a function. As typically you use internal functions in C++...
I also learned this and const. So my understanding is that this defines something for recall from this.
const is a variable that that seems similar this this, in that you declare objects around it.
 
D

Deleted member 205

Guest
NathanBelomy said:
Oh define, is to define a function. As typically you use internal functions in C++...
#define is also used to define Constants("const" for short). So, you're able to define both Function-Macros and Constants.

As for the "As you typically use internal Functions in C++...". By this, you're referring to Functions/Methods being used inside a File and not being shared with any other File across a Program. One thing that is typically done is that there is a Header-File for a Program-File. In this Header, this is where Functions and that are stored. And when they're stored in the Header, they can be shared across the entire Program. While this doesn't need to be done all the time, it's common practice when working with C and C++.

NathanBelomy said:
I also learned this and const.
NathanBelomy said:
const is a variable that that seems similar this this, in that you declare objects around it.
One thing to note, is that Constants cannot be changed. Once you declare them, they cannot be changed while the Program is running via any sort of calculations. If you want to change a Constant value then you need to go back and change it yourself.

And const is not a Variable. It's a Type, that you use to tell the Program: "Hey. This is a Constant value and as a result, the initialized value is final". This is the same as saying to Program when declaring a Variable as the Type, int: "Hey. This Variable is an Integer. So, it's only a whole number and isn't a Decimal".
 

NathanBelomy

New Coder
Thanks for the clarification.

This code is strange to me
Starts off as *~
Then it says .*
Then it declares files like this .*.file in the build/ directory.

What I'm mostly confused about is the opener, *~ and then next like .*
You can back up files by typing *.file? Or does the opening line have to be *~

 
D

Deleted member 205

Guest
Oh ok, thanks for the clarification. I found http://coursera.org is part of UC Santa Cruz and offers free class on C and C++
While courses may be helpful, they don't teach you much in my opinion. Consider picking-up a Book on one or both of the Languages. The C Programming Language by Brian W. Kernighan and Dennis Ritchie and The C++ Programming Language by Bjarne Stroustrup are essentially the Bibles for both Languages. Consider picking them up as they were authored by the Creators of both Languages(Except Brian Kernighan, who just co-authored alongside the Creator of C, Dennis Ritchie).

Good luck and a Happy New Year.
 
Top Bottom