Thread: Functions Explained

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

    Functions Explained

    I dunno if someone wants to take the time but I would be glad, I got this code from Sams Teach Yourself C Programming in 21 Days:

    Code:
    #include <stdio.h>
    
    long cube(long x);
    
    long input,answer;
    
    int main()
    {
    	printf("Enter an interger value: ");
    	scanf("%ld", &input);
    
    	answer=cube(input);
    
    	printf("\nThe cube of %ld is %ld.\n\n", input,answer);
    
    	return 0;
    }
    
    long cube(long x)
    {
    	long x_cubed;
    
    	x_cubed=x*x*x;
    	
    	return x_cubed;
    }
    Now I get most of this but what I don't understand is when the program sends whatever the user entered to the function how does all of that work. Because it says 'long cube(long x)' and in the main part it has 'cube(input)'. I get confused when I look at the fuction because it is returning x_cubed, it uses only the variable x, so I don't see how or where input comes into play here or what they point of x is...I know this is a beginner question but so am I, thanks.
    -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 moi's Avatar
    Join Date
    Jul 2002
    Posts
    946

    Re: Functions Explained

    Originally posted by CAP
    I dunno if someone wants to take the time but I would be glad, I got this code from Sams Teach Yourself C Programming in 21 Days:

    Code:
    #include <stdio.h>
    
    long cube(long x);
    
    long input,answer;
    
    int main()
    {
    	printf("Enter an interger value: ");
    	scanf("%ld", &input);
    
    	answer=cube(input);
    
    	printf("\nThe cube of %ld is %ld.\n\n", input,answer);
    
    	return 0;
    }
    
    long cube(long x)
    {
    	long x_cubed;
    
    	x_cubed=x*x*x;
    	
    	return x_cubed;
    }
    Now I get most of this but what I don't understand is when the program sends whatever the user entered to the function how does all of that work. Because it says 'long cube(long x)' and in the main part it has 'cube(input)'. I get confused when I look at the fuction because it is returning x_cubed, it uses only the variable x, so I don't see how or where input comes into play here or what they point of x is...I know this is a beginner question but so am I, thanks.
    "long x" is only in the scope of the cube() function. the variable x doesn't exist outside of it. in order to call cube() you just have to pass a long to it, it doesnt matter what the variable name is.

    it is the same thing with x_cubed; it is only in the the scope of cube(). cube() returns a long, you assign that long to whatever variable you want.
    hello, internet!

  3. #3
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    hmm

    to keep it short and simple :

    long cube(blah blah blah)

    "long" is the type of the return value of that function

    long cube(long x)
    this "long" is the type of the argument passed to the function.

    long x_cubed;
    you declared a variable of type long and did something to it
    later on (i.e. x_cubed = x * x * x)

    return x_cubed

    you're returning the value of x_cubed from the function

    and finally...

    answer=cube(input);

    that value is being taken over by the variable "answer"
    which is also of type "long"

    it's as simple as that.


    >I get confused when I look at the fuction because it is returning x_cubed, it uses only the variable x, so I don't see how or where input comes into play here or what they point of x is

    input came into play when you were using it to assign a
    value to your x_cubed variable.... you said x_cubed = x * x * x
    ... the value of "x" is the value of the variable sent to the
    function (i.e. input)
    just think of a function argument as a box that can accomodate
    stuff given to it by the function call
    so, by saying
    cube(input) ... you're sending "input" variable into that "box - x"
    of that function ... which will later process it
    (multiply/divide/whatever) and then return a value ...
    now, this is a very raw way of explaining it, but i hope you
    got the gist of what i said.

    GOOD LUCK
    Last edited by moonwalker; 08-20-2002 at 12:29 AM.

  4. #4
    Registered User CAP's Avatar
    Join Date
    May 2002
    Posts
    179
    Ok then, so the whole long x, x_cubed and so on and so on is pretty much like a frame that defines how the function works, but when you put input it, input takes over and just runs it course...ok seems simple enough, this is chapter 5 and it said that scopes and local variable are on chapter 12 or something so thanks for the heads up
    -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. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  2. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  3. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  4. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM
  5. functions - please help!!!!
    By linkies in forum C Programming
    Replies: 1
    Last Post: 08-21-2002, 07:53 AM