[Study Notes] [C Language] function, Study Notes C Language

Source: Internet
Author: User

[Study Notes] [C Language] function, Study Notes C Language
1. What is a function?

Any C language program is composed of one or more program segments (applets), each of which has its own functions. We generally call these segments "functions ". Therefore, you can say that the C language program is composed of functions.

For example, if you write an MP3 player program in C language, the program structure is shown in:

1 # include <stdio. h> 2 3/* 4 1. when to define a function: Add a common new function 5 6 2. function Definition Format 7 Return Value Type Function Name (form parameter list) 8 {9 function body 10} 11 12 3. to define a function, you need to clarify things 13 1> Start a meaningful function name 14 2> 15 */16 17 int printLine () 18 {19 printf ("------------- \ n "); 20 return 0; 21} 22 23 int average (int num1, int num2) 24 {25 return (num1 + num2)/2; 26} 27 28 int main () 29 {30/* 31 printLine (); 32 33 printLine (); 34 35 printLine (); 36 */37 38 int a = 10; 39 int B = 9; 40 41 int c = average (a, B); 42 43 printf ("c is % d \ n", c); 44 45 int a1 = 11; 46 int b1 = 20; 47 48 int d = average (a1, b1); 49 printf ("d is % d \ n", d); 50 51 return 0; 52}

2.

1 # include <stdio. h> 2/* 3 parameter Note: 4 1. format parameters: define the parameters following the function name. actual parameter: the specific data passed in by calling the function, referred to as the real parameter 6 3. the number of real parameters must be equal to the number of parameters 7 4. the function body cannot define variables 8 5 that are the same as the parameters. if the basic data type is used as the function parameter, it is purely a value transfer. Modifying the value of the internal function parameter does not affect the value of the External Parameter 9 6. A function can have no form parameter or an infinite number of form parameters 10 */11 12 //. for short, the form parameter 13 int sum (int num1, int num2) 14 {15 // The variable 16 // int num1; 17 18 num1 = 50; 19 20 return num1 + num2 cannot be defined inside the function body; 21} 22 23/* 24 return: 25 1> exit function 26 2> return a specific value to function caller 27 28 return value note 29 1> void indicates no return value 30 2> if not explicitly stated return value type, the default value is int type 31. 3> even if the return value type is explicitly declared, no value 32 is returned. By default, C language does not allow two functions with the same name 34 */35 36 char test () 37 {38 return 'a'; 39} 40 41/* 42 void test (int, int B) 43 {44 45} */46 47 void test5 () 48 {49 50} 51 52/* pseudocode 53 void login (QQ, password) 54 {55 // 1. verify if QQ has a value of 56 if (QQ has no value) return; 57 58 // 2. verify that the password has a value 59 if (no value for the password) return; 60 61 // 3. send QQ and password to server 62 63} */64 65 int test3 () 66 {67 printf ("999999999 \ n "); 68} 69 70 // if the return value type is not explicitly declared, the default value is int type 71 test2 () 72 {73 printf ("888888888 \ n"); 74 return 10; 75} 76 77 int main () 78 {79 int c = test2 (); 80 printf ("c = % d \ n", c); 81 82 test3 (); 83 // test (); 84 85/* 86 int a = 100; 87 int B = 27; 88 89 // a and B are called the actual parameters of the function, 90 int c = sum (a, B); 91 92 93 printf ("a = % d, B = % d, c = % d \ n",, b, c); */94 95 return 0; 96}

3. Function exercises

1/* 2 calculate the difference between two integers 3 print a horizontal line 4 print N horizontal lines 5 6 define the function Step 7 1> according to the function, start a meaningful name. 8 2. determine the number of parameters of the function. 9. 3. Determine the return value of the function. 10 */11 # include <stdio. h> 12 13 void printLines (int n) 14 {15 for (int I = 0; I <n; I ++) 16 {17 printf ("----------------- \ n "); 18} 19} 20 21 void printLine () 22 {23 printf ("------------------- \ n"); 24} 25 26 int minus (int a, int B) 27 {28 return a-B; 29} 30 31 int main () 32 {33 printLines (10); 34 // printLine (); 35 // printf ("% d \ n", minus (100, 29); 36 37 return 0; 38}

4. Notes for Functions

1 # include <stdio. h> 2/* 3 1. by default, functions with the same name are not allowed. functions cannot be nested. the function cannot be defined repeatedly, but it can be declared repeatedly. if there is a function declaration, there is no function definition. 7. 1. The compilation is successful. Because the compiler only detects unreasonable syntax combinations and does not check whether the function is defined. 8. 2. The link reports an error, because the link checks whether the function defines 9 10 */11 12 // function declaration 13 // void printLine (); 14 // void printLine (); 15 // void printLine (); 16 // void printLine (); 17 // void printLine (); 18 // void printLine (); 19 20 int main () 21 {22 void printLine (); 23 24 printLine (); 25 return 0; 26} 27 28 // Function Definition 29 30 void printLine () 31 {32 printf ("-------- \ n"); 33}

 

Related Article

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.