Thread: void function in c -> c++

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    27

    void function in c -> c++

    I have some code which looks like the following:



    Code:
    void x(int a) {
    
    do something with a
    
    }
    
    void y(int b) {
    
    return x(50);
    
    }
    How do I port to c++?

    Regards
    /RP

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Wait, I'm not allowed to use void functions in C++? Oh no my life, ruined!

  3. #3
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Quote Originally Posted by Tonto
    Wait, I'm not allowed to use void functions in C++? Oh no my life, ruined!
    ROFL
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    27
    Funny guy ey.....I urgently need help on this.
    So don't laugh about silly stuff, give some help instead.

    And if your'e so geeky that you can't find anything else to laugh about get a life.

    I'm just curious if this is the same thing as doing:

    Code:
    void x(int a) {
    
    do some stuff....
    
    }
    
    void y(int b) {
    
    x(50);
    
    }
    Regards,
    /RP

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    In regards to your original question:

    Code:
    /////////
    // Test suite
    //////////
    void x(int a) 
    { 
    	a += 50; 
    }
    void y(int b) 
    { 
    	return x(50); 
    }
    int main(int argc, char* argv[])
    {
    	y(10);
    }
    Let's see, I build the project and woops!

    Quote Originally Posted by VC6.0++
    --------------------Configuration: test - Win32 Debug--------------------
    Compiling...
    main.cpp
    C:\Nerd\Tests\main.cpp(10) : error C2562: 'y' : 'void' function returning a value
    C:\Nerd\Tests\main.cpp(8) : see declaration of 'y'
    Error executing cl.exe.

    test.exe - 1 error(s), 0 warning(s)
    Yeah, you can't return a value, it's a void function specifying it won't return a value. However, from your updated post:

    Code:
    /////////
    // Test suite
    //////////
    void x(int a) 
    { 
    	a += 50; 
    }
    void y(int b) 
    { 
    	x(50); 
    }
    int main(int argc, char* argv[])
    {
    	y(10);
    	return 0;
    }
    Works fine. No errors there. Here's a short thing about functional programming and void: http://www.mtsu.edu/~csci117/manual/lab8/lab8.html and another small reference: http://www.fredosaurus.com/notes-cpp...uncstruct.html and all of this is perfectly acceptable in both C and C++. We're just elitist ..........es around here, sorry. A quick google search on void functions does solve our issue quite nicely however!

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    27
    yes but this is why I ask....it's a c snippet which I would like to port to c++
    could I just replace the return x(50) with x(50) and that will do the same?

  7. #7
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Quote Originally Posted by seizmic
    Funny guy ey.....I urgently need help on this.
    So don't laugh about silly stuff, give some help instead.

    And if your'e so geeky that you can't find anything else to laugh about get a life.

    I'm just curious if this is the same thing as doing:

    Code:
    void x(int a) {
    
    do some stuff....
    
    }
    
    void y(int b) {
    
    x(50);
    
    }
    Regards,
    /RP
    Mate I'm a self confessed geek. Read the sig.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    27
    are you like not very bright or?

  9. #9
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    >> could I just replace the return x(50) with x(50) and that will do the same?

    The same? No of course not. Then you are not returning a value anymore. But seeing as you can't do that kind of stuff in 'void' functions in the first place, it doesn't matter. So you could say yes it is the same, but I mean, all it seems you want to do is call the function, you can not return a value. That's just how void functions work, and why they exist. Also try reading the articles I provided in my above post. I picked them out because I felt they would answer many of the questions you might have, including the one you just asked. You also might like reading this more general howstuffworks article on C functions:

    http://computer.howstuffworks.com/c12.htm

    It does a good job at bringing the this important concept of programming down to basic terms, including 'void' functions.

  10. #10
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Oh god not again... started an 'argument' with someone possessing the mental intelligence of a 12 year old

    I'm going to be a good boy this time mods
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  11. #11
    Registered User
    Join Date
    Aug 2005
    Posts
    27
    seems like ahluka has to much time on his hands.....why not do something constructive with it...like go dr_wn himself or something like that

  12. #12
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Amateur remarks
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  13. #13
    Registered User
    Join Date
    Aug 2005
    Posts
    27
    The thought of low level programming excites me. <- hmmm I wonder if you even know what low level is

  14. #14
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    I was wondering if you'd start questioning my programming abilities. Seeing as programming has been my sole hobby for the past 5 years...

    Low level programming would be a reasonable description for any compiled (not interpreted - well not any I know of) language other than VB, C#, J# or Java (Java IMO BTW).

    C/C++ are reasonably low level languages, but my favourite LLL of all time iiiiiiiiiis... Assembly! I do prefer Intel syntax but obviously there are times when you just need to write in AT&T syntax (GCC inline asm for example). Well not that syntax makes any difference anyway. Not the most practical LLL in the world, but short of programming in 'raw' machine code...

    Don't you agree?

    *edit*

    Oh yes and I'm currently programming in assembly for my electronics coursework. Working with binary literals all the time is a pain though.
    Last edited by cboard_member; 08-19-2005 at 05:48 AM.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  15. #15
    Registered User
    Join Date
    Aug 2005
    Posts
    27
    I guess that would be in the eye of the beholder or in this case the programmer.....so therefore I think your'e wrong.

    From a c,c++,java programmers view -> assembly is LLL

    From an assembly programmers view -> c,c++,java are typically HLL.

    From an machine code programmer (if any such exist nowadays) -> assembly is HLL

    This gives a contradiction in terms what machinecode and assembly programmers think they are dealing with.

    I'm currently also involved in assembly programming of the ARM processor.
    Mainly working with srecords.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  4. Replies: 5
    Last Post: 02-08-2003, 07:42 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM