Another C programming problem

Thread Starter

oussama123443

Joined Nov 2, 2014
27
hi all i'm asked to make a function in c that returns the repeated digits when 5 values are entered by the user, i did the program but the compiler(Code Blocks in Mac) is giving me a warning saying that "Control end at non void function" , the program is assuming that i'm not returning a value to the function call . any ideas how to avoid that please and fix it ??? here is my program below:

Code:
#include <stdio.h>

int repeat(int fA, int fB, int fC, int fD, int fE);

int main (){

    int a, b, c, d, e;

    printf("Enter numbers: ");

    scanf("%d%d%d%d%d", &a, &b, &c, &d, &e);

    printf("\n%d\n", repeat(a, b ,c, d, e));

    return 0;

}

int repeat(fA, fB, fC, fD, fE) {

    int k,j, array[] = {fA, fB, fC, fD, fE};

    for(k = 0; k <= 3; ++k) {

        for( j = k + 1; j <= 4; ++j) {

            if(array[k] == array[j]) {

                return array[k];

            }

        }

    }

}
Edited by mod: added code tags, changed variable i to variable k
 
Last edited by a moderator:

MrChips

Joined Oct 2, 2009
30,795
A thread belongs to the Thread Starter. Please do not hijack someone else's thread.
Now you have your own.

Also please use code tags to enclose program code.
 

NorthGuy

Joined Jun 28, 2014
611
To avoid this error you need a "return" at the very end of the "repeat" function. The compiler must know what to return if all the comaprisons fails and you get out of the for loops.
 

WBahn

Joined Mar 31, 2012
30,045
The compiler is probably choking on the fact that your function declaration and your function definition do not match.

Your function declaration (prototype) says:

int repeat(int fA, int fB, int fC, int fD, int fE);

which indicates that the function takes five arguments all of which are of type int.

But your function definition fails to declare the type of the variables. Most compilers will throw an error on this.

Also, note that using scanf() to get input is a REALLY bad thing to do. You should use fgets(). Some compilers also have safe versions of the many unsafe functions in the standard libraries.

Your function needs to be written so that it ALWAYS returns a value of the appropriate type. What if your if() statement that has your return statement never executes? What does the function return then?
 

WBahn

Joined Mar 31, 2012
30,045
You cannot return an array like that. Why not make the array global instead?
But he's not returning an array, he is either returning one element of the array or he is returning nothing at all.

However, I'm not aware of any C compilers that can handle something like:

int k,j, array[] = {fA, fB, fC, fD, fE};

I believe this kind of array initialization must resolve to literal values at compile time. Otherwise, how can the compiler force the array pointer to point to a block of memory that has those variables in that order? In this particular case, that might be possible since the variables are on the stack and are probably in the same order, but what if the order was different between the two or some other parameter was in the argument list.
 

Eric007

Joined Aug 5, 2011
1,158
The compiler is probably choking on the fact that your function declaration and your function definition do not match.

Your function declaration (prototype) says:

int repeat(int fA, int fB, int fC, int fD, int fE);

which indicates that the function takes five arguments all of which are of type int.
Well I may be wrong but I think there is no problem with the matching of the function declaration and function definition. I think it is *good practice* but not necessary to repeat the type ('int' in this case) in the function definition.
That would be a problem if He/She had put a different 'type' in the function definition.

Again I might be wrong, also I haven't looked in depth in the code for other potential problem.
 

WBahn

Joined Mar 31, 2012
30,045
From my copy of the somewhat ancient C99 draft standard:

The declarator in a function definition specifies the name of the function being defined
and the identifiers of its parameters. If the declarator includes a parameter type list, the
list also specifies the types of all the parameters; such a declarator also serves as a
function prototype for later calls to the same function in the same translation unit.

There is an allowance for this to be split and for there to be a separate declarator giving the types, but this is an obsolescent feature that is deprecated and may go away in a future version of the standard. But even so, this code does not provide that declaratory.
 

JohnInTX

Joined Jun 26, 2012
4,787
There is an allowance for this to be split and for there to be a separate declarator giving the types, but this is an obsolescent feature that is deprecated and may go away in a future version of the standard. But even so, this code does not provide that declaratory.
Some older compilers in the transitional period from original K&R to ANSI supported back-compatibility by enforcing type checking on functions whose parameters were declared in the function's parenthesis and not if declared below the function name.

Code:
myfunc(x,y,z)
{
int x,y,z
blah blah
}// x,y,z not type checked


myfunc(int x, int y, int z)
{
blah blah
} // x,y,z WERE type checked
In some discussions I read on C (Reagan was president..!) there were actually programmers that insisted that type-checking parameters was a bad thing - it inhibited the creative process - particularly the habit of using int as pointer. We don't hear much from them any more - my guess is that they are still debugging...
 
Top