The function of Dark Horse programmer--c Language

Source: Internet
Author: User
Tags file copy

1. What is a function any C language Program is composed of one or more program segments (small programs), each of which has its own function, which we generally call " functions ". So, you can say that the C language program is composed of functions. 2 . Definition of function 1. purpose of defining functions encapsulates a common feature to facilitate later invocation

2. steps to define a function function Name: What is the function name function Body: What does the function do and what code it contains

3. format Fixed Format (this is the case for many language functions)

return value type function name ( formal parameter list )

{

? function Body

}

Example

Defines a function that calculates the number of two integers and

Sum(int a, int b)

{

? int c = a + B;

}

3. function Adjustment sum (10, 11); elicit return value describe the calling procedure for a function briefly describe the function of return

4 . Parameters of the function 1. Basic concepts of formal parameters and arguments2. The number of formal parameters and the same argument:sum (ten, one, one) 3. the pass of a parameter is a value pass 4. parameter names cannot have the same name as local variables within a function 5. functions can have no parameters: Design a function to return PI

Parameter Note points

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

2. Actual parameters: Call the function of the specific data passed in, referred to as an argument

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

4. Variables within the function body that cannot be defined as parameters

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

6. A function can have no formal parameters, or can have an infinite number of parameters

5 . Return value of function 1. The basic concept of the return value , the role of Return

The function of return:

1> Exit Function

2> returns a specific value to the function caller

Return value Note point

1> void represents no return value

2> If the return value type is not explicitly stated, the default is to return the int type

3> even if the return value type is explicitly declared, no value can be returned

By default, the C language does not allow the same names as two functions

void can omit return You can use return multiple times cannot have other statements after return 4. weak syntax for functions If there is no write return value type, the default is int If you write a return value, you can not return Call a function that has not been defined

6 . Steps to define a function 1. clear function, a meaningful function name 2. define the parameters and return values of the function 3. For example: for two integers and
1#include <stdio.h>2 intSumintNUM1,intnum2)3 {4     returnNUM1 +num2;5 }6 7 intMain ()8 {9     intA =2;Ten     intb =5; One     intc =sum (A, b); Aprintf" %d%d%d", a,b,c); -  -     return 0; the}

2 Print N Horizontal lines

1#include <stdio.h>2 3 voidPrintlines (intN)4 {5      for(inti =0; i<n; i++)6     {7printf"-------------------\ n");8     }9 }Ten  One intMain () A { -Printlines (Ten); -     //PrintLine (); the     //printf ("%d\n", minus (+)); -      -     return 0; -}

7. function Note cannot nest defined functions The Dead loop calls themselves to call themselves cannot be defined repeatedly , can be declared repeatedly

1. By default, 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

1> compilation can be passed, because the compiler will only detect the syntax is not reasonable, and will not detect the function has no definition

2> link error, because the link will detect whether the function is defined

8, the function of the supplement

1. main function return value :0, normal exit;1, abnormal exit 2. printf functions #include

1> function: Copy all contents of the right file to the location of # include

2> custom files with "", the system comes with the file <>

3> #include <stdio.h> Purpose: Copy the declaration of the printf function

return Value: The length of the string man is a 3-character example: printf ("abc"); The return value is 3 if printf ("ABC man \ n"); The return value is 7 because one Chinese is 3 characters.

9. Exercises 1. Write a function double AVR (int a, int b)to calculate The average of A and b 2. write a function int pows(int base, int n) returns the n- th square of the base 3. Write a function int pieadd (int n), calculate The value of the 1+2+3+......+n and return. For example , the return value of Pieadd (3) is 1+2+3=6

10 . Declaration of functions 1. the order in which functions are defined 2. function declaration function : Declaration and definition, similar to the identity card and human Relations , the compilation of buy tickets, link boarding 3. only function declaration, no definition , compile warning, link error

Link: merges all associated. o Target files and C-language libraries in the project to generate executables

1. function definition put. c file, function declaration put. h file

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

3. The role of the. h file: copied by someone else. You do not need to pipe. h files when compiling links.

4.cc XX.O XXX.O to link multiple target files together

CC XX.C XXX.C compiling and linking multiple source files

11 . Division of Labour between. h files and. c Files 1. The disadvantages of a single file The content of a file is too much, not conducive to reading, debugging multiple people modifying the same file problem The company is team work

2. Extract the sum function into another . C file call the SUM function directly first , compile warning, link main.c error #include "sum.c" , compile link main.c, run successfully (Paint analysis . o file) If you use SUM.C in avr.c , compile the link main.c, run the program (the reason why the paint analysis is wrong)

3. declaring the sum function in other files int sum (int,int); Compile link main.c Compile link sum.c Compile link main.c sum.c, run successfully avr.c Use the same method

4. extract int sum (int,int) to another file The downside of not extracting claims: adding new functions extracted into a . C file? The development tool will compile all the . C Links extracted to the . h file

5. Summary of extraction steps definitions for. c File Write functions Declaration of the. h file Write function To use my function, please include my . h file Benefit Analysis

12. #include 1. Introduction to pre-processing directives 2. The role of #include

It's purely a file copy.

3. #include <stdio.h> What's in stdio.h? What to do when it comes to linking The difference between <> and "" 4. #include path issues The default is the same path, and other issues are discussed later

The function of Dark Horse programmer--c Language

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.