Thread: explanation please

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    101

    explanation please

    My understanding appears to be wrong; in the following snippet I define a Print as a routine but when I call it from within Main nothing happens; am I completely on the wrong track here about sub routines? I appreciate it is a silly bit of code but I am trying to understand calling a defined routine from another.

    Code:
    #include <windows.h>
    #include <iostream>
    #include "ShortColours.h"
    
    using namespace std;
    
    int Print()
    {cout<<"dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"<<'\n';
        for(int x(1);x<20;x++)
        cout<<"John"<<'\n';
        return 0;
     }
    
    int main ()
    {
       Print;
       return 0;
    }

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    90
    By doing this:
    Code:
    Print;
    You're not actually calling the function. You're just referencing it, and since you're not using the reference in an expression, nothing happens.

    You want to do this:
    Code:
    Print();

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    you have to call your functions with () after them
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    101
    Quote Originally Posted by rogster001 View Post
    you have to call your functions with () after them
    Thanks all, I knew it would be something simple (like me!).

    Can I do a supplementary; in one book it tells me to put Main as the last function so all the others are known about before you get there and in another it says just declare your functions first and have Main as your first 'real' function what would you guys recommend?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by JayCee++
    Can I do a supplementary; in one book it tells me to put Main as the last function so all the others are known about before you get there and in another it says just declare your functions first and have Main as your first 'real' function what would you guys recommend?
    I recommend that you do as you please. When your programs become bigger, you will be making use of function declarations in header files, but within a source file you could well organise your functions such that the ones that are called by others in the same source file are defined first.

    Oh, and note that the name of the global main function is main, not Main.
    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

  6. #6
    Registered User
    Join Date
    Oct 2011
    Location
    india
    Posts
    18
    make the function call as print()

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This has already been mentioned:
    Quote Originally Posted by rogster001 View Post
    you have to call your functions with () after them
    And acknowledged:
    Quote Originally Posted by JayCee++ View Post
    Thanks all, I knew it would be something simple (like me!).
    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.

  8. #8
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    Quote Originally Posted by vikasvijayan View Post
    make the function call as print()
    Gotta love folks that don't read anything but the first post in a thread
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Explanation pls
    By dpp in forum C++ Programming
    Replies: 6
    Last Post: 09-05-2009, 02:08 PM
  2. Need Explanation
    By Bargi in forum Linux Programming
    Replies: 2
    Last Post: 01-31-2008, 01:25 AM
  3. Explanation Please
    By atari400 in forum C++ Programming
    Replies: 2
    Last Post: 04-25-2003, 08:39 AM
  4. Explanation
    By fallonides in forum C Programming
    Replies: 3
    Last Post: 03-19-2003, 01:41 PM
  5. Further Explanation
    By sketchit in forum C Programming
    Replies: 1
    Last Post: 09-24-2001, 12:23 PM