functions

This is a discussion on functions within the C Programming forums, part of the General Programming Boards category; I'm having trouble understanding the concept of functions. How exactly are they used, and where do they go? Things like ...

  1. #1
    Registered User distanceisdeath's Avatar
    Join Date
    Feb 2006
    Posts
    10

    functions

    I'm having trouble understanding the concept of functions. How exactly are they used, and where do they go? Things like that. Thanks for the help.

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    here's a usage example
    Code:
    #include <stdio.h>
    
    int generic(); // The decleration
    
    int main()
    {
    	generic();
    }
    
    int generic() //The implementation
    {
    	int example = 15234+21534;
    	return example;
    }
    Making functions usually has two parts:
    The decleration (these go in .h files or at the top of source files)
    and the implementation (these go in .c files)

    The basic two purposes of a function are
    -Saving space when a single chunk of code is going to be used multiple times
    -Helping readibility

  3. #3
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Quote Originally Posted by MadCow257
    here's a usage example
    Code:
    #include <stdio.h>
    
    int generic(); // The decleration
    
    int main()
    {
    	generic();
    }
    
    int generic() //The implementation
    {
    	int example = 15234+21534;
    	return example;
    }
    Careful there. Your prototype of:
    Code:
    int generic();
    Is saying "generic is a function that returns int and takes any number of arguments".

    If you called that function as generic("potato", 3, 8.5); the compiler wouldn't complain. You should instead use:
    Code:
    int generic(void);
    The compiler then knows generic() takes no arguments.

  4. #4
    Registered User distanceisdeath's Avatar
    Join Date
    Feb 2006
    Posts
    10
    thanks for the help

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, 02: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21