Home  >  Article  >  Backend Development  >  How to declare a function in c language

How to declare a function in c language

尚
Original
2020-04-25 14:34:127340browse

How to declare a function in c language

The so-called declaration (Declaration) is to tell the compiler that I want to use this function. It doesn’t matter if you don’t find its definition now. Please don’t report an error. I will fill in the definition later.

The format of function declaration is very simple, which is equivalent to removing the function body in the function definition and adding a semicolon; at the end, as shown below:

dataType  functionName( dataType1 param1, dataType2 param2 ... );

You can also write no formal parameters, Write-only data type:

dataType  functionName( dataType1, dataType2 ... );

The function declaration gives the function name, return value type, parameter list (emphasis on parameter type) and other information related to the function, which is called the function prototype (Function Prototype).

The function prototype is to tell the compiler information related to the function, so that the compiler knows the existence of the function and its existing form. Even if the function is not defined temporarily, the compiler knows how to use it.

Example:

#include <stdio.h>

//函数声明
int sum(int m, int n);  //也可以写作int sum(int, int);

int main(){
    int begin = 5, end = 86;
    int result = sum(begin, end);
    printf("The sum from %d to %d is %d\n", begin, end, result);
    return 0;
}

//函数定义
int sum(int m, int n){
    int i, sum=0;
    for(i=m; i<=n; i++){
        sum+=i;
    }
    return sum;
}

Recommended: "c Language Tutorial"

The above is the detailed content of How to declare a function in c language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn