C Basic knowledge "function"

Source: Internet
Author: User
Tags function definition


C function
1. A function is a set of statements that perform a task together. Every C program has at least one function, the main function main (), and all simple programs can define additional functions.
You can divide the code into different functions. It is up to you to decide how to divide the code into different functions, but logically, the partitioning is usually performed on a specific task based on each function.
A function declaration tells the compiler the name, return type, and parameters of the function. The function definition provides the actual body of the function.
The C standard library provides a number of built-in functions that a program can invoke. For example, the function strcat () is used to concatenate two strings, and the function memcpy () is used to copy memory to another location.
Functions also have many names, such as methods, subroutines, or programs, and so on.
2. Defining functions
The general form of a function definition in C is as follows:
return_type function_name (parameter list)
{
Body of the function
}
In the C language, a function consists of a function header and a function body. Here is a list of all the components of a function:
return type: A function can return a value. Return_type is the data type of the value returned by the function. Some functions perform the required operation without returning a value, in which case the return_type is the keyword void.
Function Name: This is the actual name of the function. The function name and the parameter list together form the functional signature.
Parameter: The parameter is like a placeholder. When a function is called, you pass a value to the parameter, which is called the actual parameter. The parameter list includes the type, order, and number of function parameters. Parameters are optional, that is, the function may not contain parameters.
function Body: A function Body contains a set of statements that define functions to perform tasks.
3. Function declaration
The function declaration tells the compiler the function name and how to call the function. The actual body of the function can be defined separately.
A function declaration consists of the following parts:
return_type function_name (parameter list);
For the function defined above, max (), the following is the function declaration:
int max (int num1, int num2);
In a function declaration, the name of the parameter is not important, only the type of the parameter is required, so the following is a valid declaration:
int max (int, int);
4: Function parameters
If the function is to use a parameter, you must declare the variable that accepts the parameter value. These variables are called formal parameters of the function.
Formal parameters are like other local variables within a function that are created when they enter a function and are destroyed when they exit the function.
When a function is called, there are two ways to pass arguments to the function:
Invocation type description
Call this method to copy the actual value of the parameter to the form parameter of the function. In this case, modifying the formal parameters within the function does not affect the actual parameters.
A reference call is passed through a pointer, and the parameter is a pointer to the address of the actual parameter, which is equivalent to the operation of the argument itself when it points to the parameter.
By default, C uses a value call to pass parameters. In general, this means that the code inside the function cannot change the actual arguments used to invoke the function.

C Basic knowledge "function"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.