Functions of C language

Source: Internet
Author: User
Tags square root variable scope

# include<stdio.h>//Max is the name of the function, I and J are formal parameters (formal arguments), void indicates that the function has no return valuevoidMaxintIintj) {    if(I >j) printf ("%d\n", i); Elseprintf ("%d\n", j);}//int indicates that an integer value is returnedintMainvoid){    inta,b,c,d,e,f; A=1, B =2, C =3, d =9, E =-5, F = -;    Max (A, b);    Max (C,D);    Max (E,F); return 0;}
Why the need for a function to avoid repetitive operations is advantageous to the modularity of the program what is called function logic: a separate unit of code capable of accomplishing a particular function physically: the ability to receive data "Of course not receiving data", the ability to process the received data, to return the results of data processing, or to return any Value "Summary: function is a tool that is designed to solve a large number of similar problems
# include<stdio.h>intFvoid)//void in parentheses indicates that the function cannot accept data int to indicate that the function return value is int type{    return Ten;//returns to the main function, and the F-tuned function}voidGvoid)//A function name preceded by void indicates that the function has no return value{        //return 10; //contradicts the void above}intMainvoid){    intj = the; J=f (); printf ("%d\n", J); return 0;}
How to define the function format:The name of the function's return value function (the formal parameter list of the function)
        {the execution body of the function
        }1. The essence of function definition is to describe in detail how a function implements a particular function Meaning of the 2.return expression:
1. Terminate the tuned function and return the value of the expression to the keynote function
2. If an expression is empty, only the function is terminated and no value is returned to the keynote function
3.break is used to terminate the loop and switch, and return is used to terminate the function
Example:void f (){return; Return is used only to terminate the function, without returning any values to the keynote function}int f (){return 10; First: Terminate function second: Return to the keynote function}
3. The type of the function return value is also referred to as the type of the function, because if the return value type in front of the function name differs from the type of the expression in the return expression in the function executor, the type of the final function return value is the type of the return value before the letter name Classification of functionsThere are parameter functions and parameterless functions with return value and no return value library function and user-defined function value transfer function and address transfer function common function and main function (main function)a program must have and only have one main function
main function can call normal function normal function cannot call main function
normal functions can call each other
The main function is the entry of the program and the exit of the program.
# include<stdio.h>/*Seeking primes*/BOOLIsPrime (intval) {    inti;  for(i =2; I < val;++i) {if(val% i = =0)             Break; }    if(i = =val)return true; Else        return false;}intMainvoid){    intm; scanf ("%d",&m); if(IsPrime (m)) printf ("yes!\n"); Elseprintf ("no!\n"); return 0;}

The problem of attention

# include<stdio.h>void f (void);  // function declaration, semicolon cannot throw away int Main (void) {    f ();     return 0 ;} /* void f (int);  function declaration, semicolon cannot discard int main (void) {    f ();  Error, declaration is of type int, and there is no    return 0;} */
# include <stdio.h>voidFvoid);//unless it's stated here .voidGvoid) {f (); //because the definition of function f is placed after the call F statement, all syntax errors}voidFvoid) {printf ("haha!\n");}intMainvoid){    return 0;}/*void g (void) {f (); Because the definition of function f is placed after the call F statement, all syntax errors}void f (void) {printf ("haha!\n");} int main (void) {return 0;}*/
sequence of function calls and function definitions
           If a function call is written in front of the function definition, it must be preceded by a function declarationfunction Pre-declaration:
1. Tell the compiler that some of the letters that are about to appear may represent a function
2. Tell the compiler about a number of possible letters that represent the parameters and return values of the function
3. Function declaration is a statement, the end must be appended with a semicolon
4. The Declaration of the library function is implemented by the file name .h> where the #include< library function resides .Formal parameters and arguments
Number same location one by one corresponding data types must be compatible with each other How to solve practical problems with reasonable design function in software development
a function is as independent as possible, a single Learn more, imitate the code of the Ox man &NBSP; The         function is the basic unit of C, and the class is the basic unit of Java,c#,c++   common system functions     double sqrt (double x);            x square root     INT abs (int x)             Find x Absolute &nbsp ;   Double fabs (double x)             x absolute value   special:    recursive             Data structure video   variable scope and storage:    by scope:        global variable:      &NB Sp     variables defined outside the function, called global variables             Global variables use range: Start from the defined position to the end of the program       &NBS P Local variables:            The parameters of variables or functions defined inside a function are collectively referred to as local variables             &NBSP ;   void  f (int i)                  {        &NBSP ;           int  j = 20;                 }               I and J are local variables             Local variables use scope: can only be used inside the function Note the issue: problems with naming conflicts between global variables and local variables
inside a function if the name of the local variable is defined as the name of the global variable, the local variable masks the global variableStatic variable automatic variable register variable by the way the variable is stored

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.