Table of contents
1.
Introduction
2.
What happens when a function is called before its declaration in C?
2.1.
Example
3.
What happens to parameters?
3.1.
Example
4.
FAQs
4.1.
Can you call a function before it has been declared?
4.2.
Why do we need to declare a function before the main program?
4.3.
What happens when the function is called?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

What happens when a function is called before its declaration in C?

Author Sanjana Yadav
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A function is a collection of statements that work together to complete a task. Every C program includes at least one function, main(), and even the most simple programs can declare additional functions.

You may split your code into functions. It is up to you to split your code into various functions, but logically the separation should be such that each function performs a distinct task.

A function declaration informs the compiler about the function's name, return type, and parameters. A function definition provides the actual body of the function.

One question that might come to your mind regarding function is what happens if we call a function before its declaration. Well, don't worry today in this article we will be answering this question with detailed codes and explanations.

Also see: C Static Function, Tribonacci Series

What happens when a function is called before its declaration in C?

If we don't use any function prototypes, the function body is declared in a section that comes after the function's calling statement. The compiler believes the default return type is an integer in this situation. However, if the function returns a different type of value, it throws an error. If the return type is likewise an integer, it will operate correctly; however, certain warnings may be generated.

Example

The following program, for example, fails to compile.

#include <stdio.h>
int main(void)
{
    // sampleFunc() is not declared
    printf("%c\n", sampleFunc());
    return 0;
}
char sampleFunc()
{
return 'C';
}
You can also try this code with Online C Compiler
Run Code

Output

 error: 'sampleFunc' was not declared in this scope
   40 |  printf("%c\n", sampleFunc());
      |                 ^~~~~~~~~~
You can also try this code with Online C Compiler
Run Code

If the function char sampleFunc() in the above code is declared after main() and the calling statement, it will fail to compile. Because the compiler, by default, believes the return type to be "int." And if the return type does not match int at the time of declaration, the compiler will throw an error.

Because the function is declared before the main, the following program compiles and runs correctly ().

#include <stdio.h>
int sampleFunc()
{
return 20;
}
int main(void)
{
    //sampleFunc() is declared
    printf("%d\n", sampleFunc());
    return 0;
}
You can also try this code with Online C Compiler
Run Code

Output

20
You can also try this code with Online C Compiler
Run Code

 

You can also read about the jump statement and  Short int in C Programming

What happens to parameters?

The compiler makes no assumptions about parameters. As a result, when the function is applied to some arguments, the compiler will be unable to conduct compile-time validation of argument types and arity. This can lead to issues.

For example, the following program compiled successfully in GCC but returned a trash value as output.

Example

#include <stdio.h>
int main (void)
{
printf("%d", sumFunc(20, 15));
return 0;
}
int sumFunc (int x, int y, int z)
{
return (x+y+z);
}
You can also try this code with Online C Compiler
Run Code

Output

 error: 'sumFunc' was not declared in this scope
   39 |     printf("%d", sumFunc(20, 15));
      |                  ^~~
You can also try this code with Online C Compiler
Run Code

There is a common misperception that the compiler expects input parameters as int. If the compiler had assumed that the input arguments were int, the above program would have failed to compile.

It is usually suggested to define a function before using it so that there are no surprises when the program is executed.

FAQs

Can you call a function before it has been declared?

This function works if you call it BEFORE or AFTER where it has been declared. When you call a declaration function before it has been declared, Hoisting works appropriately.

Why do we need to declare a function before the main program?

It is usually a good practice to define the functions either before the main or in a separate header file that will be included in other c files where that function has been used. This allows us to quickly identify all of the functions declared/defined in that.

What happens when the function is called?

A function is a collection of code that performs a certain purpose and may be called anytime needed. To better comprehend the code while using multiple function calls or recursion, the idea of a function call must be known.

Conclusion

In this article, we have extensively discussed the scenario when a function is called before its declaration. We discussed the cases when the function is declared before and after the main with examples.

We hope that this blog has helped you enhance your knowledge of the functions in C and if you would like to learn more about C, check out our articles C Archives.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass