Thread: C Functions how do i put this in a function

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    3

    C Functions how do i put this in a function

    Ive been told to put the following code into a function and yet when i try since it is 3 arrays i cant get it to work at all. Any ideas to get me started on this?
    I tried using a function like this
    Code:
    void funct1(int x[], y[], z[]);
    {
    for (i=0; i<funct1; i++)
    return      x[i] = i;
    return       y[i] = i;
    return       z[i] = i;
    }
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
       int x[10], y[15], z[20];
       int i, sumx, sumy, sumz;
    
       for (i=0; i<10; i++)
          x[i] = i;
    
       for (i=0; i<15; i++)
          y[i] = i;
    
       for (i=0; i<20; i++)
          z[i] = i;
    
       sumx = 0;
       for (i=0; i<10; i++)
          sumx = sumx + x[i];
    
       sumy = 0;
       for (i=0; i<15; i++)
          sumy= sumy + y[i];
    
       sumz = 0;
       for (i=0; i<20; i++)
          sumz = sumz + z[i];
    
       printf ("The sum of the x array = %d\n", sumx);
       printf ("The sum of the y array = %d\n", sumy);
       printf ("The sum of the z array = %d\n", sumz);
    
       system("PAUSE");
       return 0;
    }
    It works fine with no function but im told a function would make it easier to read and faster to process although it seems pretty fast to me.

    I'm new to C and working through a workbook and don't understand functions real well yet.

    Thank you for any assistance.
    Last edited by Fyrewynd; 10-23-2003 at 02:28 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. How properly inherit from template?
    By 6tr6tr in forum C++ Programming
    Replies: 118
    Last Post: 04-25-2008, 04:30 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM