Function declaration in C.
Hello coders! Welcome back!
Today will look into the function declaration rules in C language.
Let's look into another example of declaring a function-
Here, I not mentioned any parameters to the function. Remember even in function declaration I have not mention any parameter. Function definition and function declaration both are same.
Today will look into the function declaration rules in C language.
What is a function declaration?
We have seen variable declaration in previous blogs ( click here to view variable declaration).
In the variable declaration, we are telling the properties of the variable to the compiler.
For example-
- int var;
Type of variable → int
Name of variable → var
Similarly, we declare functions too.
A function declaration is also known as Function prototype. Means we are declaring the properties of the function to the compiler.
For example-
- int test (char, int);
The return type of the function → int
Name of the function → test
Number of parameters → 2
The data type of 1st parameter → character
The data type of 2nd parameter → integer
NOTE- In a function declaration it is necessary to put a semi-colon at the end.
⋆ It is not necessary to put the name of the parameter in the function prototype.
Example-
int test (char var1, int var2);
Here var1 and var2 variable names are optional in function declaration but mandatory in the function definition. Let's look with a code example to understand this.
Here you can see that I have not mentioned the variable name in the function declaration, whereas mentioning data type is mandatory. Whereas I have mentioned variable name in the function definition.
Example-
int test (char var1, int var2);
Here var1 and var2 variable names are optional in function declaration but mandatory in the function definition. Let's look with a code example to understand this.
Here you can see that I have not mentioned the variable name in the function declaration, whereas mentioning data type is mandatory. Whereas I have mentioned variable name in the function definition.
Here, I not mentioned any parameters to the function. Remember even in function declaration I have not mention any parameter. Function definition and function declaration both are same.
Is it always necessary to declare the function before using it?
→ Not necessary, but it is preferred to declare it before using it.
Let's look at why it is not necessary!
There is another way of writing the function. Let's look into the code for a detailed explanation.
Here, you can see that the function definition is written first.
The compiler will get to know the return data type of the function and number of parameters in that function. Then use it in the main function.
In this case, we can avoid writing function declaration.
What happens if we define the function after writing the main function?
To answer this let's look at the code for better understanding-
It pops a warning saying the implicit declaration of function 'test' which means we have not declared the test function, the compiler assumes something about this that its data type might be an integer. We have declared it as a character. There arrives the conflict, hence the second error pops up saying error: conflicting types for 'test'
Remember it always assumes about the data type nothing more than that!!
This is it for this blog! will look into more details about functions in further blogs. If any doubts drop a comment always ready to clear them.
Happy learning!
Comments
Post a Comment