C language basics-Lesson 5-functions, C language functions

Source: Internet
Author: User
Tags exit in

C language basics-Lesson 5-functions, C language functions
1. prototype and call of function 1.1

You must define or declare a function before using a function.

Double circle (double r );

Int main ()

{

Double length = circle (10 );

Printf ("length = % f \ n", length );

Return 0;

}

 

Double circle (double r)

{

Return 2*3.14 * r;

}

 

1.2 The form of the function participates in the real Parameter

When calling a function, most functions have parameters. data must be transferred between the main function and the called function.

When defining a function, the variable name in the ARC behind the function name is "form parameter", or "form parameter. When a function is called, the variable or expression in the brackets following the function name is called "actual parameter.

1. When no function call occurs, they do not occupy memory units. Only when a function call occurs is allocated memory. After a function call is completed, the memory occupied by the parameter is released.

2. Real parameters can be variables, constants, or expressions.

3. When defining a function, you must specify the Data Type of the parameter.

The four data types involved in the real parameter must be compatible.

5 In the C language, the data transmitted by the real-participation parameters is "value transfer", that is, one-way transmission. Only the real parameters are passed to the form parameters, but not the real parameters.

If the function parameter is an array, you can use the form parameter to modify the value of the real parameter.

1.3 return types and return values of functions

1. the return value of the function is obtained through return in the function. If the return value of the function is void, you do not need a return statement.

2. The returned data type in the return Statement of the function should be the same as that in the function definition.

3. If there is no return statement in the function, the function returns an uncertain value.

1.4 return statements of the main and exit functions and functions

Int test1 ()

{

Printf ("111111 \ n ");

 

// Return 0;

Exit (0); // calling exit in the subfunction also means that the program is terminated, but calling return in the subfunction means that the subfunction is terminated and the program is executed normally.

 

Printf ("222222 \ n ");

}

 

Int main ()

{

Test1 ();

Printf ("AAAAAA \ n ");

Exit (100); // exit is a library function in C language. The result of calling exit is that the program is terminated.

Return 100; // in the main function, calling exit is the same as calling return.

Printf ("CCCCCCC \ n ");

 

Return 0; // The return of the main function indicates that the program is terminated.

Printf ("BBBBBB \ n ");

}

 

Compilation of header files for over 1.5 source code file programs

If you put the main function in the first file and the custom function in the second file, you need to declare the function prototype in the first file.

If you include a function prototype in a header file, you do not have to declare its prototype every time you use the function. It is a good habit to put the function declaration into the header file.

1.5.2 # include and # define

# Include is a simple replacement of File Content

# Define is a simple text replacement.

1.5.3 # ifndef and # endif

# Ifndef indicates conditional pre-compilation. If the condition following # ifndef is true, the pre-compilation starts from # ifndef to # endif. Otherwise, the code will not be precompiled.

1.6 recursion of functions

A function can call itself. This is called recursion of a function.

Void recurse (int I)

{

If (I> 0)

{

Recurse (I-1 );

}

Printf ("I = % d \ n", I );

}

 

Int main ()

{

Recurse (10 );

Return 0;

}

 

1.6.1 recursive process analysis

Void up_down (int n)

{

Printf ("in % d, location % p \ n", n, & n );

If (n <4)

Up_down (n + 1 ));

Printf ("out % d, location % p \ n", n, & n );

}

 

Int main ()

{

Up_down (1 );

Return 0;

}

N people lined up and asked the n-person how old he was. He replied that he was two years older than the previous one and asked him how old he was. He replied that he was two years older than the previous one, he always asked the first person to answer his question. He was 10 years old.

Int age (int n)

{

Int I;

If (n = 1)

I = 10;

Else

I = age (n-1) + 2;

Return I;

}

 

Set10Example of converting a hexadecimal number to a binary number

234 is in decimal Format 2*10 to the power of 2 + 3*10 to the power of 1 + 4*10 to the power of 0.

The last digit of an odd binary must be 1, and the last digit of an even binary must be 0.

You can use number % 2 to obtain the last digit in binary format. If you want to convert a complete integer to binary, you need to use a recursive function.

Before recursive call, calculate the value of number % 2 and output the result after the recursive call statement. In this way, the first value is output at the end.

To get the next number, we need to divide the original number by 2. This calculation is equivalent to moving the decimal point to the left in decimal notation. If the obtained number is an even number, the value of the next binary is 0, if the result is an odd number, the next binary number is 1.

The recursion is stopped until the result of Division 2 is less than 2.

Void to_binary (unsigned int n)

{

Unsigned int I = n % 2;

If (n> = 2)

To_binary (n/2 );

Printf ("% c", I + 0x30 );

}

 

An example of the Fibonacci series

The Fibonacci series refers to a series of 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,144 ,...

0th items are 0, and 1st items are the first 1.

This series starts from 2nd items, and each item is equal to the sum of the first two items.

Int fib (int n)

{

If (n = 0)

Return 0;

If (n = 1)

Return 1;

If (n> 1)

Return fib (n-1) + fib (n-2 );

}

 

1.6.2 advantages of Recursion

Recursion provides the simplest method for some programming problems.

1.6.3 disadvantages of Recursion

A flawed recursion quickly consumes computer resources, making recursive Programs hard to understand and maintain.

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.