help with passing variables to different functions

This is a discussion on help with passing variables to different functions within the C++ Programming forums, part of the General Programming Boards category; I just want to send an integer to the printout() function to print it but it just seems to get ...

  1. #1
    Trainee Geek Draylath's Avatar
    Join Date
    Nov 2011
    Location
    Cambridge
    Posts
    24

    help with passing variables to different functions

    I just want to send an integer to the printout() function to print it but it just seems to get the variable and then exit. What is it I'm doing wrong? Thanks

    Code:
    #include <iostream>
    using namespace std;
    
    
    //prototypes
    void printout(int );
    
    
    //main
    int main()
    {
    
    
        int x;
        cout << "Please enter an interger to print: ";
        cin >> x;
            void printout(int x);
    }
    
    
    //definitions
    void printout(int x)
    {
    
    
    
    
    
    
        cout << "The value for our output is: " << x << endl;
    
    
    
    
    
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,315
    This is a function declaration:
    Code:
    void printout(int x);
    This is a function call:
    Code:
    printout(x);
    Also, don't leave out parameter names from your function prototypes.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Trainee Geek Draylath's Avatar
    Join Date
    Nov 2011
    Location
    Cambridge
    Posts
    24
    Thanks for the reply

    I get it now. The prototype and definition have to have the return type in the call is just the function name +arguments.

    Cheers

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    Also don't forget laserlight's advice.
    void printout(int );
    should be
    void printout(int x);

    Possibly x should be renamed to something more descriptive.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Trainee Geek Draylath's Avatar
    Join Date
    Nov 2011
    Location
    Cambridge
    Posts
    24
    I hadn't forgotten. All done and done now and thanks for the input I really appreciate it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing variables to functions
    By theitsmith in forum C Programming
    Replies: 4
    Last Post: 02-06-2012, 12:19 PM
  2. passing variables between functions
    By owi_just in forum C Programming
    Replies: 6
    Last Post: 05-08-2005, 10:12 AM
  3. Replies: 6
    Last Post: 05-06-2003, 04:08 PM
  4. Passing variables through functions and structures?
    By dizz in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2002, 10:18 PM
  5. passing variables to functions?
    By aoe in forum C Programming
    Replies: 12
    Last Post: 06-02-2002, 05:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21