"C-language learning", "C Primer Plus", 9th Chapter function

Source: Internet
Author: User
Tags pow

Learning Summary

1, the function is helpful for us to omit the duplicated code, the function can make the program more modular, thus facilitates the program's reading, the modification and the consummation. When designing or architecting a system, we tend to pursue modularity, assembly, and loose coupling, and the function is the expression of its code. Many programmers prefer to think of functions as "black boxes", where certain inputs produce specific results or return a value, while the internal behavior of a black box does not need to be considered, but it helps to devote energy to the overall design of the program rather than its implementation details. In accordance with the C design principle, we should not write a separate function for each task, but should try to abstract the functions of the function to achieve the general purpose.

2. The components of a function are function types, function names, function parameters (formal parameters), function body implementations, function returns (depending on the type of function). As shown in the following:

3. The traditional function declaration form before the ANSI C specification is not accurate because it declares only the return value type of the function and does not declare its arguments. such as int min (); This is the declaration of the preceding form of ANSI C to inform the compiler min () to return a numeric value of type int. However, the statement does not describe the number and type of parameters for min (). Therefore, if the wrong argument type and number of arguments are used in the function min (), the compiler cannot find the error.

4. The process of a function call itself is called recursion. Recursion can replace loops and vice versa. The advantage of recursion is that it provides the simplest solution for some programming problems, and the disadvantage is that some recursive algorithms quickly deplete the computer's memory resources. At the same time, the use of recursive programs is difficult to read and maintain.

5, pointer-related operators:&, *, both are unary operators. The operator follows a variable, such as the & variable is given the address of the variable, and the pointer address is inside most systems, it is represented by an unsigned integer, but it is not possible to treat the pointer as an integer type, and some methods that handle integers cannot be used to manipulate pointers. The * operator is used to get the numeric value stored in the address being pointed to. The pointer variable is the value that is stored in the pointer variable.

6, the pointer is also a type, what kind of data type will need what kind of pointer, such as integer type pointer definition:

int *pi;

The asterisk (*) here indicates that the variable is a pointer. Pi is a pointer, and *PI is a pointer of type int.

7. Functions can often change external variables through pointer parameters, such as:

1#include <stdio.h>2 voidChangeValue (int*a,int*b);3 intMain () {4         intA=1, b=2;5ChangeValue (&a,&b);6printf"a=%d,b=%d\n", A, b);7         return 0;8 }9 voidChangeValue (int*a,int*b) {Ten         inttemp; Onetemp = *A; A*a=*b; -*b=temp; -}

Printing results:

A=2,b=1

8. Programming questions (question 6)

1#include <stdio.h>2 3 DoublePowerDoubleNintp);4 5 intMain () {6         DoubleX,xpow;7         intexp;8 9printf"Enter A number and the positive integer power to which\n");Tenprintf"The number would be reduced. Enter Q to quit.\n"); One  A          while(SCANF ("%lf%d", &x,&exp) = =2){ -xpow=Power (X,EXP); -printf"%.3g to the power%d is%.5g\n", X,exp,xpow); theprintf"Enter Next pair of numbers or Q to quit.\n"); -         } -  -printf"Hope enjoyed this power trip--byte!\n"); +         return 0; - } +  A DoublePowerDoubleNintp) { at         Doublepow=1; -         inti; -         if(n==0) -                 return 0; -         if(n==1) -                 return 1; in          for(i=1; i<=p;i++){ -pow*=N; to         } +         return 1/POW; -}

Operation Result:

Enter a number and the positive integer power to which

The number would be reduced. Enter Q to quit.

2

3

2 to the Power 3 is 0.125

Enter next pair of numbers or Q to quit.

4

5

4 to the Power 5 is 0.00097656

Enter next pair of numbers or Q to quit.

1

3

1 to the Power 3 is 1

Enter next pair of numbers or Q to quit.

1

5

1 to the Power 5 is 1

Enter next pair of numbers or Q to quit.

0

3

0 to the Power 3 is 0

Enter next pair of numbers or Q to quit.

0

81

0 to the power Bayi is 0

Enter next pair of numbers or Q to quit.

Q

Hope enjoyed this power trip--byte!

9. Programming questions (question 7)

1#include <stdio.h>2 3 DoublePowerDoubleNintPDoublePOW);4 5 intMain () {6         DoubleX,xpow;7         intexp;8 9printf"Enter A number and the positive integer power to which\n");Tenprintf"The number would be reduced. Enter Q to quit.\n"); One  A          while(SCANF ("%lf%d", &x,&exp) = =2){ -Xpow=power (X,exp,1); -printf"%.3g to the power%d is%.5g\n", X,exp,xpow); theprintf"Enter Next pair of numbers or Q to quit.\n"); -         } -  -printf"Hope enjoyed this power trip--byte!\n"); +         return 0; - } +  A  at DoublePowerDoubleNintPDoublePOW) { -         if(n==0) -                 return 0; -         if(n==1) -                 return 1; -         if(p==0) in                 returnN; -         if(p==1) to                 return 1/(nPOW); +         returnPower (n,--p,pow*n); -}

"C-language learning", "C Primer Plus", 9th Chapter 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.