0

Is this function have any sense? if I didn't define the value of p_var1[] and p_size?

Just to know if it make sense, I have bigger code, but as a beginner, for me, if there is no values for these variables, that is weird.

First function:

int max1(int p_values[15][15])
{
    int max_value;
    for(int i=0; i < 15; i++)
    {
        int max_v = fnc(p_values[i], 15);
        if( max_value < max_v)
        {
            max_value = max_v;
        }
    }
    return max_value;

}

and second

//p_var1[] - is an array of values
//p_size - is the size of array
int fnc(int p_var1[], int p_size)
{
    int max_value;
    for (int i = 0; i < p_size; i++)
    {
        if (max_value > p_var1[i])
        {
            max_value = p_var[i];
        }
    }
    return max_value;


}
  • You have to define the values when calling the function, or you can set the default ones in the definition. – Bhawan Sep 17 '19 at 02:10
  • so, if there is no values in this one, the function is non sense? –  Sep 17 '19 at 02:10
  • no, it's not. When you call the function from some other part of code, it will be executed. – Bhawan Sep 17 '19 at 02:11
  • In your code, `max_value` can be anything, you should go with `int max_value = p_var1[0]` and start the `for` loop with `int i = 1`. – Daniel Sep 17 '19 at 02:15

2 Answers2

2

This code isn't a full program, it's just a function definition. Here, a function called fnc is declared that can be called with parameters. Here's an example of a full program using it:

//p_var1[] - is an array of values
//p_size - is the size of array
int fnc(int p_var1[], int p_size)
{
    int max_value;
    for (int i = 0; i < p_size; i++)
    {
        if (max_value > p_var1[i])
        {
            max_value = p_var[i];
        }
    }
    return max_value;


}

int main() {
    int lst[5] = {10, 2, 6, 4, 8};
    int max = fnc(lst, 5); // max = 10
    return 0;
}
scatter
  • 610
  • 4
  • 21
0

I guess you wrote fnc(p_values[i], 15) without knowing what it means? Not the best approach, but asking about it shows promise. When this expression is reached, the fnc identifier says that we're going to pause the execution of the current function (max1) and jump to execute the function fnc. Once that function finishes, the returned value will be the value of the expression and execution of max1 can resume. The stuff in the parentheses says that as we jump to fnc, assign p_var1 = p_values[i] and p_size = 15 (the first parameter is assigned the first argument, and the second parameter is assigned the second argument). If you're confused by the terms "parameter" and "argument", see What's the difference between an argument and a parameter?

So when you call fnc you do define the value of p_var1 and p_size (for that function call). (Wikipedia has an article with an example that covers this also, and you might find some useful information in the rest of that article.)

JaMiT
  • 9,693
  • 2
  • 12
  • 26
  • I need to read again the chapter with Functions.. cause I didn't get it :( –  Sep 17 '19 at 04:01