The issue has been solved thanks to Eugene, however, I am still curious about what I did wrong with the user-defined functionI am a beginner and I do not see my mistake in this code, I am trying to print the array elements then swap the first and last element then print the values again, is it possible to swap without a user-defined function? if so how? thank you in advance. this is my code.
#include<stdio.h>
int swap(int);
int main()
{
int size;
int a[5]={10,20,30,40,50};
for(size=0;size<5;size++)
{
printf("%d",a[size]);
}
swap(a);
size=0;
for(size=0;size<5;size++)
{
printf("%d",a[size]);
}
}
int swap(int)
{
int temp=a[0];
a[0]=a[4];
a[4]=temp;
}
The error message actually exactly tells you what is wrong:
Parameter name missing.
a undefined...
(Please do not post text error message as pictures. We cannot copy&paste it here form your picture.)
In function definition you must provide a name for each parameter. Otherwise you cannot use it inside that function:
int swap(int)
{
int temp=a[0];
a[0]=a[4];
a[4]=temp;
}
Here don't tell your compiler how that int parameter is called.
And on the other side you don't tell the compiler what you think, a
should be. It is not visible in scope.
To fix that, change like this:
int swap(int a[])
{
int temp=a[0];
a[0]=a[4];
a[4]=temp;
}
That will solve the error messages you showed us.
But you should also get at least a warning about not retuning anything from a non-void function.
You defined return type as int
but don't return any int
value.
For a swap function there is normally no need to return anything.
Therefore use this:
void swap(int a[])
{
int temp=a[0];
a[0]=a[4];
a[4]=temp;
}
Any decent beginners text book should clearly cover how function definitions have to look like. You should not skip the first chapters when learning a new language.