Need Help With Functions

This is a discussion on Need Help With Functions within the C Programming forums, part of the General Programming Boards category; I am a college student studying c programming and i need help with using functions because when i create a ...

  1. #1
    Registered User Hackstorix's Avatar
    Join Date
    Feb 2013
    Posts
    9

    Post Need Help With Functions

    I am a college student studying c programming and i need help with using functions because when i create a program i insert all the code into the main function which is very difficult to work with...

    I wondering if there was anyone who could help me understand the basics of using functions...

    Thank You.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,655
    One of the first functions we used is here.

    Take a look how we take the code from main and put it in the function.

    If main is a bit difficult to understand, replace main with this
    Code:
    int main(void)
    {
            int i = 5;
            int result;
            result = isPrime(i);
            if(result == 1)
            {
                  printf("It is prime\n");
             }
             else
             {
                  printf("It is not\n");
              }
    
            return 0;
    }
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    331
    Quote Originally Posted by Hackstorix View Post
    I wondering if there was anyone who could help me understand the basics of using functions...
    A function should be anythign that represents a logical unit.

    E.g. the area of a circle, or other geometrical shape. The maximum, minimum, or average in a list. Loading a data base of employees from disk. Paying the employees. Saving the updated data with the new pay information back to disk.

    There are some tricks. Don't be afraid to softcode a fucntion parameter, then hardcode the argument. Say you input a line consisting of a list of numbers, separated by commas. You can count the numbers by counting the commas.
    The function

    Code:
    int strcount(char *str, char ch)
    {
       int answer = 0;
       while(*str)
       {
         if(*str =ch)
           answer++;
         str++;
       }
       return answer;
    }
    will do this for you. However the comma will be hardcoded, higher up.

    Code:
    Nnumbers = strcount(line, ',') + 1;
    The second trick is to use pointers a lot. Virtually all of you C functions will pass information to and from caller, using pointers,usually pointers to structures or arrays of structures.

    Also use malloc() a lot. Mostly there won't be a natural limit to the
    amount of data you are operating on. By using malloc(), you allow the program to grow, until the machine runs out of memory.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    http://www.malcolmmclean.site11.com/www

  4. #4
    Registered User Hackstorix's Avatar
    Join Date
    Feb 2013
    Posts
    9
    thanks for your help
    std10093

    Malcolm McLean

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WinAPI functions - similar functions as part of VS C++
    By jlewand in forum Windows Programming
    Replies: 2
    Last Post: 02-02-2012, 07:54 AM
  2. Creating Functions & passing information to other functions
    By RyanLeonard in forum C Programming
    Replies: 4
    Last Post: 10-28-2010, 12:17 PM
  3. Replies: 7
    Last Post: 04-19-2006, 11:17 AM
  4. Replies: 6
    Last Post: 05-06-2003, 03:08 PM
  5. Replies: 1
    Last Post: 01-20-2002, 10:50 AM

Tags for this Thread


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