Thread: function question

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    7

    function question

    hello, can someone please tell my why the following method of calling a function does not work??

    Code:
    #include <iostream>
    
    int test1(int x){
    
    return x* 12;
    
    }
    
    int test2(int x){
    
    return x*12;
    
    }
    
    int main(){
    
    int test1, test2; /* if i rename all occurences of these to something that is different that function names then it works*/
    
    std::cin >> test1 >> test2;
    
    std::cout << test1(test1) << std::endl << test2(test2) << std::endl;
    }
    i know what the problem is , in fact i have fixed it already but i cannot understand what is wrong.

    if it illegal in c++ to use the name of a function as the parameter being passed to the said function?

    please see my comment on where i fixed it to work

    thank you

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Well, it's very bad to have multiple things with the same name -- the compiler sees test1 and test2 as ints at that point in the code.

    You could prefix the function calls with ::, but it would be far better to change the names.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    Yeah, it's just *way* better to give them a different name... And a meaningful name if possible.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Functions and variables share a namespace. By declaring two local variables called test1 and test2 in main(), you're hiding the functions of these names. The compiler can't find them.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Use the scope resolution operator
    Code:
    std::cout << ::test1(test1) << std::endl << ::test2(test2) << std::endl;
    But it's far better to have different names for things.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CreateThread() function question
    By chiefmonkey in forum C++ Programming
    Replies: 5
    Last Post: 05-15-2009, 07:52 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  4. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM