Thread: array function arguments & parameters

  1. #1
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    112

    array function arguments & parameters

    Hi all..it's me again...

    Code:
    // Write a function named sumarrays() that accepts two arrays as arguments, totals all values in both arrays, and returns the total to the calling program. Use the function created in exercise 7 in a simple program.
    
    #include <stdio.h>
    
    #define MAX 5
    
    int array_1[MAX], array_2[MAX], count;
    
    int sumarrays(int array_1[], int array_2[], int length);
    
    int main()
    {
        int a;
    
        for(count = 0; count < MAX; count++)
        {
            printf("Enter an integer value (array_1): ");
            scanf("%d", &array_1[count]);
        }
        
        for(count = 0; count < MAX; count++)
        {
            printf("Enter an integer value (array_2): ");
            scanf("%d", &array_2[count]);
        }
    
        int answer;
    
        answer = sumarrays(array_1, array_2, MAX);
    
        printf("Total of both arrays = %d\n", answer);
    
        return 0;
    }
    
    int sumarrays(int num_array_1[], int num_array_2[], int length)
    {
        int a, total;
    
        total = 0;
        count = 0;
        
        for(count = 0; count < length; count++)
        {
            total += num_array_1[count];
        }
    
        for(count = 0; count < length; count++)
        {
            total += num_array_2[count];
        }
    
        return total;
    }
    please explain, how sum is being added with this line:
    Code:
    answer = sumarrays(array_1, array_2, MAX);

    Thank you!!!

    I wrote this code, it was a Q&A out of a Sam's Teach yourself C programming in 24 hours...

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,558
    That's just a function call that passes the two arrays as arguments, along with MAX. Each array is converted to a pointer to its first element. What exactly do you have trouble understanding about that?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arguments and parameters help.
    By AFCKING in forum C Programming
    Replies: 5
    Last Post: 02-02-2013, 07:36 PM
  2. command-line arguments or parameters to a program
    By c_lady in forum C Programming
    Replies: 2
    Last Post: 06-21-2010, 10:28 AM
  3. Sending fewer parameters then arguments
    By gar35 in forum C++ Programming
    Replies: 1
    Last Post: 05-01-2010, 09:20 PM
  4. Explanation of arguments/parameters in Functions
    By hellogamesmaste in forum C Programming
    Replies: 8
    Last Post: 08-17-2009, 02:30 PM
  5. Parameters vs. Arguments
    By Sereby in forum C Programming
    Replies: 2
    Last Post: 07-27-2004, 10:00 AM

Tags for this Thread