08-c language Functions

Source: Internet
Author: User
Tags file copy

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 make it easy to call 2 later. Steps to define a function

L Function Name: What is the function name

L function Body: What does the function do, and what code 3 is included in it. 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 functions

1. Basic concepts of formal parameters and arguments

2. The number of formal parameters and arguments are the same: sum (10, 11, 12)

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 return pi five, function return value

1. The basic concept of the return value, the role of return

2. void

3. Return

l void can omit return

L can use return multiple times

L cannot have other statements after return

4. Weak syntax for functions

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 six, the 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:

L to ask for two integers and

L Print a horizontal line

L Print N horizontal line seven, function note

L cannot nest defined functions

L Call myself in a dead loop.

L cannot repeat the definition, can repeat the declaration eight, the function of the Supplement 1. Main function

L return Value: 0, normal exit, 1, abnormal exit 2. printf function

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 10, the declaration of the function

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, compilation warning, link Error xi.,. h file and. C File Division

1. The disadvantages of a 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.

2. 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.C is used in avr.c, compile the link main.c, run the program (the reason for drawing analysis error)

3. Declaring the SUM function in other files

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

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

5. 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, #include 1. Introduction to preprocessing Directives 2. #include的作用

is purely a file copy of 3. #include <stdio.h>

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

L <> and "" The Difference 4. #include的路径问题

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

08-c language Functions

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.