Thread: help with passing variables to different functions

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    Location
    Inside my computer
    Posts
    24,654
    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, 09:12 AM
  3. Replies: 6
    Last Post: 05-06-2003, 03: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, 04:19 PM