The functions in C

Source: Internet
Author: User

First, what is a function

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

L For example, if you write a MP3 player program in C, the program structure is as follows:

Second, the definition of function 1. Defining the purpose of a function

L encapsulate a common function to facilitate later invocation.

2. Steps to define a function

L Function Name: What is the function name

L function Body: What does the function do and what code it contains

3. Format

L fixed format (all functions in many languages are written in this way)

return value type function name ( formal parameter list )

{

function body

}

l Example

Defines a function that calculates the number of two integers and

sum (int a, int b)

{

int C = a + B;

}

Third, function call

L SUM (10, 11); Elicit return value

L Description of Function call procedure

L briefly describe the function of return

Iv. parameters of the function
    1. Basic concepts of formal parameters and arguments
    2. The number of parameters is the same as the argument:sum (ten, one, one)
    3. The pass of a parameter is a value pass
    4. Parameter name cannot have the same name as a local variable within a function
    5. Functions can have no parameters: Design a function to return PI

V. Return value of a function
    1. The basic concept of return values , the function of return
    2. void
    3. Return

l void can omit return

L can use return multiple times

L cannot have other statements after return

    1. Weak syntax for a function

L If there is no write return value type, the default is int

L If you write a return value, you can not return

L Call a function that has not been defined

Vi. steps to define a function
    1. Clear function, a meaningful function name
    2. Define the parameters and return values of the function
    3. Example:

L to ask for two integers and

L Print a horizontal line

L Print N Horizontal line

Seven, function notice

L cannot nest defined functions

L Call myself in a dead loop.

L cannot be defined repeatedly, can be declared repeatedly

Eight, the function of the Supplement 1. Mainfunction

L return Value:0, normal exit;1, abnormal exit

2. printffunction

L #include

L return Value: Length of string

Nine, exercise
    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) to return 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

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

Xi.. hfiles and. Cthe division of Documents
    1. The disadvantage of single file

L The contents of a file are too many, not conducive to reading, debugging

L Multiple people modify the same file problem

L The company is a team player.

    1. Extract the sum function into another . C file

L CALL the sum function directly first, compile warning, link main.c error

L #include "sum.c", compile link main.c, run successfully (Paint Analysis . o file)

L If sum.cis used in avr.c , compile the link main.c, run the program (the reason for drawing analysis error)

    1. Declaring the sum function in another file

l int sum (int,int);

L Compile link main.c

L Compile link sum.c

L Compile link main.c sum.c, run successfully

L AVR.C Use the same method

    1. extract int sum (int,int) to another file

L Do not extract the disadvantages of the declaration: Add a new function

L extract to . C file? The development tool will compile all the . C links

L extract to . h file

    1. Summary of extraction Steps

L. C file definition of write function

L. h File write function declaration

If you want to use my function, please include my . h file

L Benefit Analysis

12, #include1. Introduction to preprocessing Directives 2. #includethe role

It's purely a file copy.

3. #include <stdio.h>

L What's in stdio.h? What to do when it comes to linking

L <> and "" The Difference

4. #includeThe path problem

L The default is the same path, other issues are discussed later

The functions in C

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.