This is the 29th day of my participation in the August Wenwen Challenge.More challenges in August

directory

  • A brief introduction.
  • The return value of the function
    • 1. The function has no return value
    • 2. The function has a return value
  • Function parameters
    • 1. The function has no arguments
    • 2. The function has fixed parameters
    • 3. The function has an indefinite length parameter
  • Function declaration and definition
    • 1. Function declaration: There is no need to implement the functionality of this function
    • 2. Function definition: The function must be implemented
  • Five. Guess you like it

C/C++ learning Directory >> C language basics

A brief introduction.

In C language, a function is a piece of code that can be used repeatedly to perform a function independently. It can accept the parameters passed by the user or not. The process of encapsulating code blocks into functions is called function definition.

/* dataType is the return value type, which can be any dataType in C, such as int, float, char, etc. FunctionName is a functionName, which is a type of identifier and is named in the same way as the identifier. Function names must be followed by parentheses (). The body is the body of the function, which is the code that the function needs to execute and is the body part of the function. Even if there is only one statement, the function body is surrounded by {}. */ dataType functionName() { //body }Copy the code

If there is a return value, use it in the function bodyreturnStatement return.returnAnd the type of data that comes outdataTypeThe same.

The return value of the function

1. The function has no return value

If the function does not return a value, the function name is preceded by void, for example:

void func(); // Declare a function that returns no valueCopy the code

2. The function has a return value

If the function has a return value, the function name is preceded by the return value type, which can be specified by int/float/double/char, etc., for example:

int func1(); // Declare a function that returns an integer of type float func2(); // Declare a function that returns a float double func3(); // Declare a function that returns a floating-point number of type double char func4(); // Declare a function that returns a charCopy the code

Function parameters

1. The function has no arguments

A function with no arguments has no argument list, for example:

int func1(); // Declare a function that returns an integer of type float func2(); // Declare a function that returns a floatCopy the code

2. The function has fixed parameters

dataType  functionName( dataType1 param1, dataType2 param2 ...)
{
    //body
}
Copy the code

dataType1 param1, dataType2 param2 … Is a parameter list. Functions can have only one argument or multiple arguments separated by,. Parameters are essentially variables that are defined with a type and a name. Compared to the definition of a function with no parameters, the definition of a function with parameters is just one more argument list, for example:

int func1(int x); // Declare a function that returns an integer float func2(int x,int y); // Declare a function that returns a floatCopy the code

3. The function has an indefinite length parameter

An indeterminate number of arguments to a function, which can be two, three or more, such as the most common printf function:

printf("%s","hello world"); Printf (" % d % d - % d ", 1, 2, 3);Copy the code

The variable length parameter of the function is not explained here for the time being, and will be introduced in detail in the following article.

Function declaration and definition

Function declaration is just an empty shell, there will be no concrete function implementation, and define the implementation of the function to implement;

1. Function declaration: There is no need to implement the functionality of this function

int add(int x,int y); // Just declare it. You don't need to implement the functionCopy the code

2. Function definition: The function must be implemented

Return (x+y); return (x+y); }Copy the code

Five. Guess you like it

  1. C language logic operator
  2. C language ternary operator
  3. C language comma expression
  4. C sizeof and strlen are different
  5. C language strcpy and strcpy_s function difference
  6. C memcpy is different from memcpy_S
  7. C language array definition and use
  8. C array traversal
  9. C array subscript is out of bounds
  10. C array memory overflow
  11. C array subscript out of bounds and memory overflow difference
  12. C language two-dimensional array definition and use
  13. C language two-dimensional array row number and column number calculation
  14. C language pointer declaration and definition
  15. P ++ / p –
  16. The C languagep++/§ + + / _ (p++) / _p + +
  17. C language uses Pointers to iterate over groups of numbers
  18. C language pointer and array difference
  19. C array pointer
  20. C language pointer array
  21. C language pointer array and array pointer difference
  22. C NULL pointer
  23. C void Pointers
  24. C language field pointer
  25. C language function declaration and definition

C language function declaration and definition

This article is published by the blog – Ape Say Programming Ape Say programming!