C-Language section eighth function

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.
    • For example, if you write a MP3 player program in C, the program structure is as follows:

    1. Definition of a function
    1. Defining the purpose of a function
    • Encapsulates a common feature to facilitate later invocation

    1. Steps to define a function
    • Function name: What are the names of functions?
    • Function Body: What does the function do and what code it contains

    1. Format
    • Fixed format (This is the case for many language functions)

return value type Name of function ( 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;

}

    1. Function call
    • SUM (10, 11); Elicit return value
    • Describe the calling procedure for a function
    • Briefly describe the function of return

    1. Parameters of the function
    1. Basic concepts of formal parameters and arguments
    2. The number of formal parameters and the arguments are the same: sum (10, 11, 12)
    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

    1. return value of the function
    1. The basic concept of return values, the function of return
    2. void
    3. Return
    • void can omit return
    • You can use return multiple times
    • Cannot have other statements after return
    1. Weak syntax for a function
    • 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

    1. steps to define a function
    1. Clear function, a meaningful function name
    2. Define the parameters and return values of the function
    3. Example:
    • For two integers and
    • Print a horizontal line
    • Print N Horizontal lines

    1. function Note
    • Cannot nest defined functions
    • The dead loop calls themselves to call themselves
    • cannot be defined repeatedly, can be declared repeatedly

    1. Complement of functions
    1. Main function
    • Return value: 0, normal exit; 1, abnormal exit
    1. printf function
    • #include
    • Return value: The length of the string

    1. 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) 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

    1. Declaration of a 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, compile warning, link error

    1. Division of labour between. h files and. c Files
    1. The disadvantage of 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

    1. 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)

    1. Declaring the SUM function in another file
    • 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

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

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

    1. #include
    1. Introduction to preprocessing Directives
    2. #include的作用

It's purely a file copy.

    1. #include <stdio.h>
    • What's in stdio.h? What to do when it comes to linking
    • The difference between <> and ""
    1. #include的路径问题
    • The default is the same path, and other issues are discussed later

C-Language section eighth 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.