Functions of C language

Source: Internet
Author: User
Tags pow rand random seed square root uppercase letter

function : A collection of lines of code that is written to perform certain functions. Can be called by the same program or other program.

function is the basic constituent unit of C language.

The advantages of the function : The code is more concise, easy to maintain, can improve the reusability of code.

How to customize a function :

return value type Function Name ( parameter )

{

function Body ;

return value ;

}

Life case : make Ham

Ham type Make Ham (Live pigs )

{

Kill a pig;

hair removal ;

cleavers ;

added starch ;

added Melamine ;

.....

packaging ;

out Ham ;

}

Attention:

1. Return value type: Can have a return value, or it can have no return value (void)

2. function Name: a name for the functions , similar to the name of a person ( simply say that the function names, convenient later call )

3. Parameters : raw Materials

functions in C are divided into built-in functions and custom functions

common built-in functions :

Built-in functions

Header file

Use

Double sqrt (double x)

Math.h

Calculation x the square root  

Double pow (double x, double y)

Calculation x of the y Second Power  

Double ceil (Double x)

not less than x the smallest integer, and to Double form Display  

Double floor (Double x)

no more than x the maximum integer, and to Double form Display  

int toupper (int x)

Ctype.h

if x is a lowercase letter, the corresponding uppercase letter is returned  

int tolower (int x)

if x is uppercase, the corresponding lowercase letter is returned  

int rand (void)

Stdlib.h

generate a random number  

void exit (int retval)

Terminating Programs  

Example :

//calculate 1~10 square root and cubic        inti; DoubleSquare,power;  for(i=1; i<=Ten; i++) {Square=SQRT (i);//using the sqrt (x) function to calculate the x square rootPower=pow (I,3);//Use the POW (x, y) function to calculate the y-square of xprintf ("the square root of%d is%.2lf, the cubic is%.2lf\n", I,square,power);//. 2lf:double type is accurate to two digits after the decimal point}

Floor () and ceil () functions:

//Floor (x);---> The largest integer not greater than X, not roundedprintf"Floor (12.1) =%f\n", Floor (12.1));// Aprintf"Floor ( -12.1) =%f\n", Floor (-12.1));//-13//ceil (x);---> Smallest integer not less than x, not roundedprintf"ceil (12.1) =%f\n", Ceil (12.1));// -printf"ceil ( -12.1) =%f\n", Ceil (-12.1));//-12

Get random Number:

// set Random seed, Time (NULL): Gets the number of seconds of the current 00:00:00 1970-01-01  for (i=0;i<; i++) {//  printf ("%d\t", Rand ()); // rand (): pseudo-random number, generated random number 0~32767 // generate a random number between 0~99 printf ("%d\t", rand ()%);}

Functions of C language

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.