Dark Horse Programmer-learning of C language function

Source: Internet
Author: User

------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------

First, the parameter problem of the function and the return value

Note the following points when defining the parameters of a function:

1. Formal parameters: A parameter that is followed by a function name when defining a function, for short, a formal parameter

2. Actual parameters: The specific data passed in when the function is called, referred to as argument

3. The number of arguments must be equal to the number of formal parameters

4. If the base data type is a formal parameter of a function, it is purely a value pass, modifying the value of a function's internal parameter without affecting the value of the outer argument

5. A function can have no formal parameters, or there can be countless

Here's a concrete example of how the function is used and the relationship between the parameters:

1#include <stdio.h>2 intSumintNUM1,intNUM2)//The two formal parameters are defined in parentheses, and can be undefined when using functions, depending on function and function.3 {4     //int num1; The inside of the function body cannot define the same variables as the parameters. 5num1= -;//Modify the value of an internal parameter6     returnnum1+num2;//Note The return value of the function, the effect of return7 }8 intMain ()9 {Ten     intA= -; One     intb= -;//A and B are actual parameters A      -     intC=sum (A, b); -printf"a=%d,b=%d,c=%d\n", a,b,c);//output a=100,b=27,c=77, when the main function is executed, the SUM function is called, but the value of the argument, A, a, is not changed, only the NUM1 is re-assigned so C output the     return 0; -}

The function of return:

1. Exit function

2. Return a specific value to the function caller

Here are two specially defined functions to take out a separate talk:

1 void Test () 2 {3     return; // void means no return value, no return value in function body, can write return directly or write nothing 4 }56  Test () {7     printf ("aaaaa " ); 8  }

the second Test function, which does not declare a return value type, returns a value note: If the return value type is not explicitly declared, the default is int type, and the return value type function title can return no value, you can use return, the C language is a weakly typed language, by default, the C language does not allow the same two function names

Second, the exercise of function and the point of attention

Steps to define a function:

1. A meaningful name (note the definition of identifiers), based on the function

2. Determine the number of parameters for a function

3. Determining the return value of a function

Function Note points:

1. The name of a function is not allowed

2. Functions cannot be nested defined

3. Functions cannot be defined repeatedly, but can be declared repeatedly

4. If there is a function declaration, there is no definition of the function, compile can pass, because the compiler will only detect the syntax is not reasonable, and does not detect the function has no definition, but the link will be error, because the link will detect whether the function is defined.

function declaration: Used in the compilation, the function is defined in the need to call the function, you can call the function before the function declaration, the Void Function name ();

Here's an example of a specific function to practice the feeling of a function:

1 /*2 1. Find the difference between two numbers3 2. Print a horizontal line4 3. Print N Horizontal lines5 */6#include <stdio.h>7 intMinus (intAintb)8 {9     returnA-b;Ten } One  A voidPrintLine ()//because only one horizontal line is required, no return value is required and no formal parameters need to be defined - { -printf"--------\ n"); the } -  - voidPrintlines (intN//n horizontal lines are undefined so define loops - { +      for(intI=0; i<n; i++) { -printf"--------\ n"); +     } A } at intMain () - { - PrintLine (); -     intC= Minus ( -, in); -printf"%d\n", c); -Printlines (3); in     return 0; -}

Include role: Copy all the contents of the right file to the location of # include, the custom file with "", the system comes with the file with <>.

Purpose of #include<stdio.h>: Copy the function declaration of the system's own function (e.g. printf function)

Link: Merge all the associated. O target files, C-language libraries into the project, and build the executable file

When developing a multi-file team:

1. The definition of the function is placed in the. c file, and the declaration of the function puts the. h file

2. If you want to use a function defined in a. c file, you only need to include the. h file # include "" in the. c file.

the role of 3..h files: be copied by others, compile the link when you do not need to tube. h file

Return 0 means that the program exits gracefully, returning other values that have no effect on the program, except that the program exits unexpectedly

Dark Horse Programmer-learning of C language 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.