Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to declare a function in C language

Shulou Source: shulou.com Published: 2022-05-31 14:29:50 11月02日 Update

This article mainly introduces the relevant knowledge of "how to declare functions in c language". The editor shows you the operation process through actual cases, and the operation method is simple, fast and practical. I hope this article "how to declare functions in c language" can help you solve the problem.

Declaration of function

The function call in C language is defined first and then used, that is, the called function is defined before the main tone function.

If you need to define the tuned function after the tone function when you customize the function in the program, you need to add the function prototype declaration before the function call. If there is neither a function definition nor a function declaration before the function call, there will be an error at compile time.

The general form of a function prototype declaration:

Type descriptor function name (list of formal parameter declarations)

Example: define a function to calculate the area of a circle, enter the radius in the main function and calculate the area.

Reference Code:

1 # include

2 # define PI 3.14159

3 int main ()

4 {

5 / / function prototype declaration

6 double circle (double radius)

7 double r, area

8 scanf (&% lf)

9 area = circle (r)

10printf ("area=%.2f\ n", area)

11 return 0

12}

13 double circle (double radius)

14 {

fifteen

16 return PI * radius * radius

17}

Only the type of formal parameter can be given in parentheses after the function name in the function declaration, and the name of the formal parameter can be omitted. For example, the function declaration on line 5 in this example can be written as follows:

Double circle (double); if a function declaration is made at the beginning of the source program file before all function definitions, then all functions in the source program file can call the declared function.

This is the end of the introduction to "how to declare functions in c language". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Function language prototype form knowledge tone file source program type industry area parameter difference utility code content radius parameter name parenthesis Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology vpn Huawei Shulou Information Shulou Tech Info