Dark Horse programmer--c language-function

Source: Internet
Author: User

This paper mainly describes the definition of functions, usage, parameters, arguments, and return values.

First, function.

A program consists of one or more program segments (small programs), each of which has its own function, which we generally call "functions"

Second, the definition of function

1. Defining functions

① defines function names; ② defines functional body;

2. Define the format of the function

return value type function name (formal parameter list )

{

function Body

}

Code examples

1 // defines a function that calculates the number of two integers and 2 int sum (intint3{4     int c = A +  b; 5 }

Iii. parameters of the function

1. formal parameter: The parameter after the function name that defines the function , referred to as the formal parameter

2, actual parameters: Call the function of the specific incoming data, referred to as an actual argument

3, the number of arguments must be equal to the number of formal parameters

4. The function body cannot define the same variables as the parameters.

5, if the basic data type as a function parameter, is purely a value pass, modifies the value of the function's internal parameter, and does not affect the value of the outer argument

6, a function can have no formal parameters, can also have an infinite number of parameters

1 //Code Examples2 //formal parameters, referred to as formal parameter3 intSumintNUM1,intnum2)4 {5     //the function body cannot define the same variables as the parameters6     //int num1;7 8NUM1 = -;9 Ten     returnNUM1 +num2; One}

Iv. return value of a function

1, return of the role:

① Exit Function

② returns a specific value to the function caller

2. Note of return value

①void represents no return value

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

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

1 //Code Examples2 //returns the character type3 CharTest ()4 {5     return 'A';6 }7 //void has no return value8 9 voidtest5 ()Ten { One      A } - //returns int type data - inttest3 () the { -printf"return int type \ n"); - } -  + //If the return value type is not explicitly declared, the default is int type - test2 () + { Aprintf"default int type \ n"); at     return Ten; -}

Five, the error point of the function

1, by default, does not allow the same name as a function

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

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

② link error, because the link will detect whether the function is defined

 1  //  2  //  declaration of the function  3   PrintLine ();  4   The definition of the function  5  6  void   PrintLine ()  7  8  printf ( --------\ n   "  9 } 

Vi. simple usage of include

1. Function: Copy all contents of the right file to #include所在的位置

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

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

1 //Code Examples2#include <stdio.h>3 4 intMain ()5 {6printf"How to use the Include \ n");7     8 //#include "Gaizuojia/first.txt"9#include"/users/gaizuojia/desktop/Code/include/gaizuojia/first.txt"Ten     return 0; One}

VII. Development of multiple documents

(a) Benefits:

1, facilitate team cooperation

2. Easy to read

3, more people modify, more efficient

(ii) Usage:

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

The role of 3.h files: copied by others. You do not need to pipe . h files when compiling links.

//Code Examples//the writing of the main function#include <stdio.h>#include"divided.h"intMain () {intScore1 = -; intScore2 = -; intc =average (score1, score2); intD =sum (score1, score2); printf ("average score is%d\n", c); printf ("Total score is%d\n", D); printf ("e=%d\n", E); return 0;}
1 //Code Examples2 //divided. c Files (basic operations)3 intAverageintAintb)4 {5     return(A + B)/2;6 }7 8 intSumintAintb)9 {Ten     returnA +b; One } A  - intMinus (intAintb) - { the     returnAb; - } -  - intMultiplyintAintb) + { -     returnAb; +}
1 //Code Examples2 //divided. h header file (declaration of the base operation function)3 intAverageintAintb);4 intSumintAintb);5 intMinus (intAintb);6 intMultiplyintAintb);

Dark Horse programmer--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.