C language study notes 6

Own functions:

note:

1, [] contains the content can be omitted, the data type description is omitted, the default is a function of the type of int; indicates that the function parameter is omitted is no function parameter, the parameter indicates that the function is not omitted are function parameters;

2, the function names follow naming identifier ;

3, from the previously defined function much as possible on the main function, if placed behind, then the main function, the main function needs to be declared before a custom function declaration format is: [Type Description Data] function name ([parameters]);

Function call:

We need to use a custom function when you have to call it, then call when it is called function calls .

In the C language, the general form of the function call is:

Function name ([parameters]); 

note:

1, when no parameters for the function call may be [] comprises omitted.

2, [] may be a constant, variable, or other configuration data types and expressions, the parameters are separated by commas.

There are no parameters involved:

No function call in the function parameters without reference function , the function call in the function parameters required have function parameters .

Ginseng and has no function parameter general form:

The only difference with a reference function and a function of no arguments that: more than one function parameter list () in the.

Types of actual parameters:

Function parameters are divided into parameter and arguments in two ways:

Parameter is a parameter used in the definition of the function names and function of the body when the object is used to receive incoming calls when the function parameters.

Argument is a parameter of the transfer function at the time of call .

Parameter argument function and has the following characteristics:

1, the only parameter to allocate memory unit is called only when, at the end of the call, immediately release the memory unit allocated. Thus, the parameter is valid only within the function . After the end of the function call returns no longer use the calling function formal parameters.

2、实参可以是常量、变量、表达式、函数等,无论实参是何种类型的量,在进行函数调用时,它们都必须具有确定的值,以便把这些值传送给形参。因此应预先用赋值等办法使实参获得确定值。

3、在参数传递时,实参和形参在数量上,类型上,顺序上应严格一致,否则会发生类型不匹配”的错误。

函数的返回值:

函数的返回值是指函数被调用之后,执行函数体中的程序段所取得的并返回给主调函数的值。

函数的返回值要注意以下几点:

1. 函数的值只能通过return语句返回主调函数。return语句的一般形式为:

return 表达式   或者为:  return (表达式);

2. 函数值的类型和函数定义中函数的类型应保持一致。如果两者不一致,则以函数返回类型为准,自动进行类型转换。

就如某一题选A,那么就返回一个字符型数据,用代码表示就是:

就如某一提答案为100,那么就返回一个整型数据,用代码表示就是:

3. 没有返回值的函数,返回类型为void。如果小刚算了一会没有返回结果的话,那么用代码表示就是:

注意:void函数中可以有执行代码块,但是不能有返回值,另void函数中如果有return语句,该语句只能起到结束函数运行的功能。其格式为:return。

递归函数:

递归就是一个函数在它的函数体内调用它自身。执行递归函数将反复调用其自身,每调用一次就进入新的一层。

注意:递归函数必须有结束条件

递归函数特点:

1、每一级函数调用时都有自己的变量,但是函数代码并不会得到复制,如计算5的阶乘时每递推一次变量都不同;

2、每次调用都会有一次返回,如计算5的阶乘时每递推一次都返回进行下一次;

3、递归函数中,位于递归调用前的语句和各级被调用函数具有相同的执行顺序;

4、递归函数中,位于递归调用后的语句的执行顺序和各个被调用函数的顺序相反;

5、递归函数中必须有终止语句。

 

Guess you like

Origin www.cnblogs.com/www-bokeyuan-com/p/11161690.html