Thread: Questions about Function calls..

  1. #1
    1479
    Join Date
    Aug 2003
    Posts
    253

    Questions about Function calls..

    I am continuing my reading in deitle/deitle. In chapter 3 it is explaining functions to me. My question is why are the parameters named different? This is how it calls the function:
    (All the numbers are of type double)
    Code:
    cout << maximum ( number1, number2, number3 ) <<endl;
    In the function definition, they have it set up like this:
    Code:
    double maximum (double x, double y, double z)
    In the function they are not returning x,y or z. They are returning the largest number of number1, number2, and number 3. To me it looks like the function call passes number1, number2, number3 and recieves x,y and z?!?!?

    My question is, why are the variables in the parameters different? When I read about it, I think they are saying that they are different because of something programmers use called signatures? I am really confused on this one...can anyone help me out?
    Last edited by RealityFusion; 07-27-2004 at 08:45 PM.
    Knowledge is power and I want it all

    -0RealityFusion0-

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Its all about scoping. number1, number2, number3 only exist in their function. x, y, z exist only in their function. When you call a function you make a copy of the number1, number2, and number3 which are then given to maximum() which calls them x, y, z. They are different because they can be and different names might make more sense.

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    In addition to what Thantos said, the function maximum doesn't know anything about the code that calls it. It doesn't care. It just says, "Give me three doubles, I'll name them whatever I want - maybe I'll name them x, y and z." The code that calls the maximum function doesn't care what that function will call the numbers. It says, "I have three doubles that I am calling number1, number2 and number3. I want to find the maximum, so I'll send these doubles to the maximum function."

    That is what Thantos is referring to by scope. Each part of the program doesn't care what the variables in the other part of the program are called. To see why this is important, add a couple more variables - number4 and number5 - and then try to find the maximum of number1, number2 and number3, then number 2, number 3, and number4, and so on. If the variable names were the same in the function, how would that work?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Function prototype questions
    By Kayoss in forum C++ Programming
    Replies: 6
    Last Post: 11-30-2005, 05:27 PM
  5. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM