C Language Fundamentals: Declaration and Definition of Functions

        In this chapter we will learn about the definition and use of functions. We can understand the concept of function in this way, a function is a collection of functions, it can complete the function of close-up according to the input, and output the result. Of course there are times when a function just does some close-up functionality without necessarily having an input or input. If we want to write a function ourselves, we need to write two parts: declaration and definition. The declaration of a function is to tell the compiler that we want to define a function, and specify its return value (output), function name, and parameter list (input). The syntax for declaring a function is as follows:

                type function_name(type var);

        Let's look at a few examples of declaring functions:

int max(int ​​a, int b); //Return the larger of the two variables.
float sum(float a, float b, float c); //Returns the sum of three variables a, b, and c.
printf(char *fmt, ...); //display in format

        Note that the ... after the fmt parameter in the printf function indicates that this function can receive an indefinite number of parameters. We will learn about functions of this indefinite parameter type in subsequent chapters.

        After the function is declared, you can define the function's implementation. When implementing the function, the function's name, return value, and parameter list must be consistent with the function's declaration. Let's implement the first two functions above:

int max(int a, int b)
{
	if (a < b)
	{
		return b;
	}
	return a;
}

float sum(float a, float b, float c)
{
	return a + b + c;
}

 

        After defining the function content, we can use these two functions. Let's write a program to call these two functions:

#include <stdio.h>

int max(int a, int b);
float sum(float a, float b, float c);

int main(int argc, char *argv[])
{
	int a = 1, b = 2;
	float c = 2.3, d = 3.4, e = 4.5;
	int f = max(a, b);
	float g = sum (c, d, e);
	printf("%d %f\n", f, g);

	return 0;
}

int max(int a, int b)
{
	if (a < b)
	{
		return b;
	}
	return a;
}

float sum(float a, float b, float c)
{
	return a + b + c;
}

    

        When a function is defined before calling the function, it is not necessary to declare the function. E.g:   

#include <stdio.h>

int max(int a, int b)
{
	if (a < b)
	{
		return b;
	}
	return a;
}

float sum(float a, float b, float c)
{
	return a + b + c;
}

int main(int argc, char *argv[])
{
	int a = 1, b = 2;
	float c = 2.3, d = 3.4, e = 4.5;
	int f = max(a, b);
	float g = sum (c, d, e);
	printf("%d %f\n", f, g);

	return 0;
}

    

        But the functions we usually write are often provided to many programs to call, and other functions are often called inside the function. If the function is not declared, multiple functions that call each other will not be able to be compiled by the compiler and cannot be used, so it is recommended that the reader declare the function before writing the function.


Welcome to the public account: programming aliens

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325691254&siteId=291194637