C Tenth round: function disclosure

Source: Internet
Author: User
Tags function prototype

C Tenth round of language: The function of the big secret

"Learning goals"

1. Definition of functions

2. Use of functions

3. Declaration of functions

4. Formal parameters and arguments

5. Return statement

6. Invocation of functions

Function: A function is a subroutine that contains one or more C-language statements to complete a subset of the functions in a program. C source programs are made up of functions.

A: Classification of functions

(a) from the point of view of the definition of function:

(a) Library functions: Provided by the C system, without definition, such as: Printf,scanf,putchar ...

(b) User-defined functions: User-defined functions

(b) The functions of the C language are both functions and procedures in other languages:

(a) A function with a return value: Returns the function value to the caller after the function has finished executing the task

(b) function without return value: When this function completes a task, it does not return the function value to the caller

(iii) Transfer of data between the keynote function and the modulated function:

(a) An argument function: (that is, the parameter function) has parameters when the function is defined and declared

(b) Parameterless function: function definition, function description and function call without parameters

B: definition of a function

type function name (parameter class table) {declaration statement}

(i) Definition of the function without parameters

Type identifier function name ()//no parameters within parentheses

{

Function statement Body;

return (expression); = = return expression; When no value is returned, return to return; (that is, empty).

}

[1] No parameter no return worthwhile function definition

Definition of no parameter no return value function # include <stdio.h>//no parameter no return function definition//Void means no argument, can not write, but write comparison specification, enhanced readability void print (void) {         int number=;                 printf ("%d\n", number);            return; Do not return anything. can be enhanced readability} The//main () function is also defined as the parameterless function int main (void) {                 ///print function number print         ();  Call of function            return 0;}


[2] Definition of a function with no parameter and return value

Definition of no parameter return value function # include <stdio.h> int Add (void) {         int a=;         int b=;                 return (A + b);//Returns the value of the expression A + B}int main (void) {         int sum;                 Sum= Add ();                 printf ("sum=%d\n", sum);            return 0; }

(ii) The definition of an argument function:

Type identifier function name (parameter list)

{

function body Statement;

return (expression);//The sentence exists when there is a return value. Return if no value is returned; (that is, return null).

}

[1] No return value function with parameter

  Definition of a parameter without return value function # include <stdio.h>//parameter no return value definition void Add (int a, int b) {     int sum= 0;         Sum= A + b;         printf ("%d\n", sum);         return;}  int main (void) {         int n=;         int m=;                 printf ("n+ m=");         ADD (n, m);              return 0; }


[2] There is a return value function with a parameter

The definition of the function with the return value of the parameter # include <stdio.h>//The definition of the parameter return value int Add (int a, int b)  //return type is int{return     (A + b);}  int main (void) {         int sum= 0;         int n=;         int m=;                 Sum= Add (n, m);                 printf ("n+ m=%d\n", sum);              return 0; }

C: Declaration of functions

The declaration of a function prototype is to tell the compiler to reserve the function name for later use. (Very similar to the hotel's reservation, people do not have to pre-order location)

type function name (parameter class table); This is the declaration, there is no function to write functions.

function declaration # include <stdio.h>//The declaration of a parameter with a return value//Only the prototype of the function is declared, and no function int Add (int a, int b) is defined; int main (void) {         intsum= 0;         intn=;         intm=;                 Sum=add (n, m);                 printf ("n+ m=%d\n", sum);             return 0; }//parameter with the return value defined int Add (int a, int b)  //return type int{return     (A + b);}

D : Formal parameters and actual parameters

(a) Formal parameters: The formal parameter appears in the definition of the function, the entire function can be used.

(b) Actual parameter: The argument appears in the main function (main), and after entering the function, the argument cannot be used.

PS: When a function call occurs, the main function transmits the value of the argument to the parameter of the called function so that the key function transmits the data to the transferred function.

Features:

(a) Parametric the memory unit is allocated only when it is called, and is released when it is finished. So the scope of the formal parameter is only valid within the function.

(b) Arguments can be constants, variables, expressions, functions, and so on, regardless of the type of argument, they must have a specific value when making a function call.

(c) Arguments and formal parameters should be strictly consistent in number, type, and order, or "type not worthy" errors will occur

(d) A one-way transfer of data in a function call. That is, the value of the argument can only be passed to the parameter, rather than the value of the parameter being directed to the argument. Therefore, during a function call, the value of the formal parameter changes, and the value in the argument does not change .

Explanation of formal parameters and arguments #include<stdio.h>//functions A and B are formal parameter void Sub (INTA, int b)  //subtraction {            int Sub;                       Sub= A-B;                       printf ("a-b=%d\n", A-B);                                    return; The end flag of the sub function}  int main (void) {            int n, m;                       n=;            m=;                       The sub function is called a            sub (n, m);//n and M are both argument               return 0;      }


E:return Statement

The return statement, also encountered many, also explained. Here again to explain.

Format: returnexpression;

Write the return statement, regardless of the return value.

Returns expression when there is a return value;

return when no returns; Although nothing is returned, it can be used as the end flag for a function. Improve readability

Return statement Instance

[1] return null//print () function definition void print (intnum)//function: Prints the value of num (         "%d\n", num);                 return; Returns NULL. function: Enhanced Readability}

[2] The return value of the//add () function defines the int Add (int a,int b) {    int sum= 0;                 Sum= A + b;                 return sum;        or use return (A + B); Return the value of an expression directly}

F: function Call

The program is always executed from the main () function, which is called when the program encounters a function name.

During the invocation of a function, the actual participation of the parameter takes place in the value pass. The value of an argument passed by value is not changed in the calling function.

#include <stdio.h>//define the SUM function to find the 0-n between and int sum (int n) {  intsum= 0;  Local variables, the function sun after the call sum will be released   for (; n> 0; n--)//for statement, in which an expression is omitted  {      sum+=n;  sum= sum+ n;  }   Returnsum;} int main (void) {  intnum=;   printf ("Call function sum before num=%d\n", num);  printf ("Sum of the number of 0-%d: sum=%d\n", num, sum (num));  printf ("Call function sum after num=%d\n", num);    return 0;}


"Smile at the Fingertips" error is unavoidable, hope to get everyone's correction ^ ^

When reproduced, keep the original link http://codingit.howbbs.com and http://blog.csdn.net/mirrorsbeyourself

C Tenth round: function disclosure

Related 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.