C language function Grammar detailed _c language

Source: Internet
Author: User
Tags function prototype

1. Overview

In C, a function is a function to complete a subroutine or a module. There are main programs or other function calls, and other functions can be invoked between each other. The same function can be called any time by one or more functions.
Attention:
A, a C program has one or more program modules, each program module as a source program file. A source file can be shared by multiple C programs.
b, the program is compiled in the source file as a unit, rather than in a function as a unit of compilation. A source file is a compilation unit
c, c program execution is starting from the main function, but also in the main function to end the entire program
D, all functions are parallel, that is, when the function is defined separately, is independent of each other. A function does not belong to another function, that is, the function cannot nest the definition. Functions can be invoked with each other, but the main function cannot be called. The main function is called by the system.
E, from the perspective of user use, functions are divided into two types:
(1) Standard function. The standard function is the function library, which is provided by the system. The number and function of standard functions provided by different C-language compilation systems are different, but the basic functions are the same.
(2) User-defined functions.
F, from the function form, the function is divided into two kinds:
(1) a parameter function.
(2) no parameter function. When the function is invoked, the calling function passes the data to the called function by parameter

2, the definition of the function

Defines the general form of a parameterless function:
Type descriptor function name ()
{
Declarations section
Statement section
}
"Real" defines a parameterless function, outputting the "Hello World" string

Copy Code code as follows:

void Show ()
{
printf ("Hello World");
}

Defines the general form of a parameter function:
Type identifier function name (form parameter list)
{
Declarations section
Statement section
}
The "example" passes parameters A and B of two int, calculates the and of a plus B, and then returns the and of a plus B.

Copy Code code as follows:

int add (int a, int b)
{
int t = 0; Declarations section
T = a + B;
return t;
}

An empty function is a function that has no function body, such as:

void Test () {...}

When an empty function is invoked, nothing is done and does not have any practical effect. In the program design, usually only the basic function in the initial stage, for advanced functions we can provide an empty function, the future implementation of the empty function.

The parameters that are specified when the function is defined are called "formal parameters," and the arguments passed when the function is invoked are called "arguments."
A, the parameters specified in the definition function, do not occupy the memory cell when the function call is not present. The formal parameters in a function are assigned to the internal deposit element only when a function call occurs. After the call is finished, the memory units that the formal parameters occupy are also freed.
b, arguments can be constants, variables, or expressions, such as: Max (3, a+b);
c, in a defined function, you must specify the type of the formal parameter
D, arguments, and formal parameters should be of the same or assignment-compatible type.
E, in C, the parameters of the argument are passed as "value pass", one way pass, only the argument is passed to the formal parameter, and the formal parameter cannot be passed to the argument. In-memory formal parameters and arguments are different memory cells. Therefore, changing the value of the parameter does not affect the value of the argument.

3, the return value of the function

In general, it is expected that the function call is the calling function to get a definite value, which is the return value of the function.
A, the return value of the function is obtained through the returns statement in the function. The return statement is taken back to the calling function with a certain value in the called function. If you need to bring back a function value from the called function, the called function must contain a return statement. You do not need a return statement if you do not need to bring back the function value from the called function. A function can have more than one return statement, and which statement will work on which return statement is executed.
b, the type of the function value. Since the function has a return value, this value should of course belong to a certain type, and the type of function value should be specified when the function is defined. C language, usually do not add type description of the function, automatically according to the whole type processing. The return type is not written in Turbo C 2.0, and Turbo C + + 3.0 cannot be passed at compile time. Therefore, it is recommended that the reader specify the function type for all functions when defined.
C, the type specified in the definition function should be the same as return type. If the type of the function value is inconsistent with the expression value type in the return statement, the function type will prevail.
D, for functions that do not have a return value, you should use "void" to define the function as "no type" or "empty type." The return statement must not appear in the function body at this time.

4. Function call
The general form of a function call is a function name (a list of arguments), such as:

Copy Code code as follows:

int a = MAX (2, 4);

By the position in the program where the function appears, there are 3 ways in which the function can be called:
A, function statement. Call the function as a statement. The function return value is not allowed at this time. such as: Show ();
b, the expression of the function. The function appears in an expression called a function expression and requires a function to return a certain value. such as: a = 2*max (a, b);
c, function parameters. Function call as an argument to a function. such as: M = max (A, max (b, c));

5. Statement and function prototype of the modulated function

Calling another function in one function requires the following conditions:
A, the modulated function must be a function that already exists
b, if you use a function library, you should also use the #include command at the beginning of the text file to "include" the information required to call the library function in this file. such as: #include <stdio.h>
where "stdio.h" is a "header file". The Stdio.h file contains some macro-definition information that is used by the input and output library functions. If you do not include information for the stdio.h file, you cannot use the functions in the input Output library.
C, if you use a function defined by the user, and the position of the function is behind the function that called it (in the same file), you should declare the called function in the keynote function. Such as:

Copy Code code as follows:

#include <stdio.h>
void Main ()
{
float Add (float x, float y); Or: Float Add (float, float);
Float A, B, C;
scanf ("%f%f", &a, &b);
c = Add (A, b);
printf ("Sum is%f\n", c);
}
float Add (float x, float y)
{
return x + y;
}

6, local variables and global variables

A, local variables
Variables defined within a function are internal variables that are valid only within the scope of a function, that is, they can be used only within this function and cannot be used outside of this function. Such as:

Copy Code code as follows:

FLOAT F1 (int a)
{
int B, C; b, c variables are only valid within the F1 function
}
char f2 (int x, int y)
{
int I, J; I and J variables are effective inside the F2 function
}
void Main ()
{
int m, n; The M and n variables are valid in the main function
}

Attention:
1 The variables defined in the main function (M, n) are also valid only in the main function, not because they are defined in the main function and are valid throughout the file or program. The main function also cannot use variables defined in other functions.
2 variables of the same name can be used in different functions, they represent different objects and do not interfere with each other.
3 The formal parameter is also a local variable.
4 within a function, you can define variables in a conforming statement, which are valid in this conformance statement called "sub-Program" or "program block." Such as:

Copy Code code as follows:

void Main ()
{
int A, B; A and B are valid inside the main function
......
{
int C;
c = a + B; C is valid in the conformance statement
}
}

b, Global variables
A variable defined outside a function is a global variable. Global variables can be shared by other functions in this file. Its valid range starts from the location where the variable is defined to the end of the source file. Such as:

Copy Code code as follows:

int A, B; A, B range from here to the end of the source file
Float F1 ()
{
int B, C;
}
Char C1, C2; The range of C1 and C2 is from here to the end of the text.
Char F2 ()
{
int I, J;
}
void Main ()
{
int m, n;
}

Attention:
(1) Global variables are a channel for exchanging data between functions. General First letter Uppercase
(2) Do not use global variables when it is no longer necessary. The reasons are as follows: Global variables occupy storage units throughout the program, reducing the versatility of the function, because relying on external variables and using too many global variables can reduce the clarity of the program
(3) If the global variable and the local variable have the same name, the local variable takes precedence

7, the storage category of variables

From the life cycle of variable existence, variable can be divided into: static storage mode and dynamic storage mode. Static storage means the way in which the system allocates fixed storage space during the operation of the program. Dynamic storage is the way to allocate storage space dynamically as needed during program operation.

In-memory storage space for the user to use. Storage space is divided into three parts:
(1) Program area
(2) static storage area. (Storage: global variable) The memory space is assigned to the global variable at the beginning of the program, and is saved until the end of the program is released.
(3) dynamic storage area. (Store: function parameters, automatic variables, field protection and return address when the function is called) allocate space only when the call ends, freeing space.
In the C language, each variable and function has two properties: the data type and the storage category of the data.

Storage categories are grouped into large amounts:
1), Static storage category
2), dynamic storage category

Includes 4 types: Automatic (Auto), static (static), register (register), external (extern)
A, auto
B, static
C, register
D, extern

8, internal functions and external functions
A, internal functions
If a function can only be called by other functions in this file, it is called an "intrinsic function". When you define an intrinsic function, precede the function name and function type with a static. The syntax is as follows:

Static type identifier function name (parameter list);

For example:

Copy Code code as follows:

static int Add (int a, int b);

An intrinsic function is also called a static function, because it is declared with static. Using intrinsic functions, you can make the scope of a function limited to the file in which it is located, with internal functions of the same name in different files, and do not interfere with each other.

For example: The test.c file invokes the Void Show () method in the test2.c file, outputting "Hello World".
Source file: test.c

Copy Code code as follows:

#include <stdio.h>
void Main ()
{
extern void Show ();
Show ();
}

Source file: test2.c

Copy Code code as follows:

#include <stdio.h>
void Show ()
{
printf ("Hello world\n");
}

b, external functions.
When you define a function, if you add the keyword extern at the leftmost end of the first number of functions, this function is an external function that can be invoked by other files. Such as:

Copy Code code as follows:

extern int Max (int a, int b); Can be called for other files

The C language stipulates that if extern is omitted when defining a function, it is implied as an external function.

In the file where you want to call this function, declare the function with extern to indicate that the function is an external function defined in another file.

For example, the test.c file invokes the int add (int a, int b) method in the Test3.c file, returns a value, and then passes the return value to the show (int result) method in the Test2.c file.
Source file: test.c

Copy Code code as follows:

#include <stdio.h>
void Main ()
{
extern void Show (int result);
extern int Add (int a, int b);
int result = 0;
result = Add (2, 3);
Show (result);
}

Source file: test2.c

Copy Code code as follows:

#include <stdio.h>
void Show (Result)
{
printf ("result=%d\n", result);
}

Source file: test3.c

Copy Code code as follows:

#include <stdio.h>
int add (int a, int b)
{
return a + B;
}

The small partner whether to the C language function grammar understanding is more profound, hoped everybody can like this article.

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.