Thread: Variables do not equal functions!!!

  1. #1
    me@burk.
    Guest

    Wink Variables do not equal functions!!!

    The data-type which preceeds the name of a function is simple the value which the function can return. The object of a function is to split a program into smaller more manageable parts. By only declaring variables within functions they cannot be used anywhere else in the program and this also reduces the space for error.

    If you are just starting out you should consider using global variables. These are variables which are declared outside all functions including main(). (these are only really suitable for smaller programs eg. under 150 lines of code).

    eg.

    /* a simple function */
    #include <stdio.h>

    int a, b, c; /* declaring a, b, c as integer globals */

    void simple_function ()
    {

    a = b + c;

    }


    main ()
    {

    b = 5;
    c = 6;

    simple_function (); /* executes the function simple_function */

    printf ("the value of a is %d\n", a);

    }


    The other way to use functions is to pass data by value to the functions. This can be done also easily but maybe is a difficult concept for beginners to understand.

    eg.

    /* a simple program passing data to functions */
    #include <stdio.h>

    int simple_function (int a, int b)
    {

    int c;

    c = a + b;

    return (c); /* returns the value of c to main*/

    }


    main ()
    {

    int calculation;

    /* perform calculation 5 + 6 */
    calculation = simple_function (5, 6);

    printf("the calculated value is %d\n", calculation);

    }


    I hope this helps in someway. C really is a great language though, stick at it!

    B. Kernighan

  2. #2
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    Don't you have to put 'void' as the function parameter if there are none?

  3. #3
    Unregistered
    Guest

    Thumbs up

    Yes void is also a data type in C. It is used to show that a variable (or in this case return data type) stores no data. In functions that do not return a value you should use void.

    eg. void function_that_does_not_return_a_value ();

    :-)

    However this cannot be used for the main function. Main always returns int (whether you declare it or not). So you can declare the main as:

    int main ()
    {
    return (0);
    }


    "or"

    main ()
    {
    return (0);
    }

    "or, more simply"

    main ()
    {
    }

    Its important to realise that even if a function does not need to return a value, an integer value can be return to determine successful execution of that part of the program.

    B. Kernighan

  4. #4
    Unregistered
    Guest
    Sorry I think I misunderstood your message. You mean the function arguments eg.

    int my_function (void);

    The answer is no. This is not compulsary, certainly not int the ANSI C standard anyway. The void is merely an indicator to the program the no arguments are needed to run that function. Leaving it blank means exactly the same thing. eg.

    int my_function (void)
    {

    return (0);

    }

    or just as good ->

    int my_function ()
    {

    return (0);

    }


    Enjoy! :-)

    B. Kernighan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. write functions with pointers variables
    By noob programmer in forum C Programming
    Replies: 5
    Last Post: 10-25-2008, 05:48 AM
  2. Replies: 13
    Last Post: 12-14-2007, 03:34 PM
  3. Replies: 3
    Last Post: 10-23-2006, 06:37 PM
  4. Replies: 6
    Last Post: 05-06-2003, 03:08 PM
  5. passing variables to functions?
    By aoe in forum C Programming
    Replies: 12
    Last Post: 06-02-2002, 04:19 PM