Thread: Functions

  1. #1
    Registered User CAP's Avatar
    Join Date
    May 2002
    Posts
    179

    Functions

    Ok I am reading that Sams' Teach Yourself C in 21 Days to try and get started right...well it talks briefly about a function that is in the code(which takes 2 variables, multiplies them and gives results)and I really confused, I thought it would be simple but I tried Javascript for a bit before like:
    function whatever()
    {
    code here
    }

    and now C is using

    int product a,b,c

    and that is what a function looks like??
    can someone explain to me how that is a function cause all I see it doing is delcaring 3 variables so....
    -Microsofts Visual C++ Introductory Kit-
    Current Projects: Learning Everything C.

    Everyone has a photographic memory, some people just don't have any film.
    ______________________________

    When was the last time you went for a colon cleansing? Because quite frankly, you're so backed up with crap that it's spilling out your mouth

  2. #2
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    A function looks like your first example:
    Code:
    function whatever() 
    { 
    code here 
    }
    > int product a,b,c
    This just declares 4 integer variables.

    To make a program you were talking about you could do something like this:
    Code:
    //Prototype
    int Product(int,int);
    
    //main function
    int main(void){
        int Total = Product(15,3);
        printf("%i\n", Total);
        return 0;
    }
    
    //Product function
    int Product(int Num1, int Num2){
        return (Num1*Num2);
    }
    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  3. #3
    Unregistered
    Guest
    CAP, int product a, b, c is the function call. When the flow through main reaches int product a, b, c, it sends 2 numbers to the function. The function then multiplies the numbers together and returns an int. If this isn't clear, ask more questions.

  4. #4
    Unregistered
    Guest
    CAP, I think I've given you some bad info, however int product a,b,c doesn't make any sense to me. Could you check it please.

  5. #5
    Registered User CAP's Avatar
    Join Date
    May 2002
    Posts
    179

    Sorry all

    I am sorry about my first post.
    I made a mistake when reading through it all, what I did was (somewhere around 1 or 2 am ) I merged(although I don't know what the hell I was thinking) both
    int a,b,c
    and
    int product(int x, int y)

    I am sorry to all of you who posted to try and answer my stupid question.
    I still don't get it but it makes more sense to me now so once again I am sorry.
    -Microsofts Visual C++ Introductory Kit-
    Current Projects: Learning Everything C.

    Everyone has a photographic memory, some people just don't have any film.
    ______________________________

    When was the last time you went for a colon cleansing? Because quite frankly, you're so backed up with crap that it's spilling out your mouth

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >int a,b,c

    These are three variables.

    >int product(int x, int y)

    This is a function. The variables x and y are the input parameters, the name product is the name of the function. It has int in front, which tells that the function is of type int. This means that the function returns a value which can be assigned to a variable of type int.

    So you can do something like this:

    a = product (b, c);

    Now the variables b and c are used as input and the value returned by the function product is stored in variable a.

  7. #7
    Registered User CAP's Avatar
    Join Date
    May 2002
    Posts
    179
    Ok that makes a whole lot more sense thanks Shiro.
    I played around with some javascript before so I can see what you are saying.

    The first one with int product(int x, int y) is a bit confusing but the second one with a = product(int x, int y) is super clear, now I will go and read somemore on my book, thanks a lot.
    -Microsofts Visual C++ Introductory Kit-
    Current Projects: Learning Everything C.

    Everyone has a photographic memory, some people just don't have any film.
    ______________________________

    When was the last time you went for a colon cleansing? Because quite frankly, you're so backed up with crap that it's spilling out your mouth

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM