Functions of C language

Source: Internet
Author: User
Tags add numbers function definition function prototype

Function
1. Function prototypes and invocations (functions must be defined or declared before use)
#include <stdio.h>
int max (int a,int b); function declaration

int main (void)
{
int a = 3;
int b = 21;
Max_one = Max (A, b);
printf ("%d\n", Max_one);
return 0;
}

int max (int a,int b)
{
int Max;
if (a>b)
{
max = A;
}
else{
max = b;
}
return max;
}

2. The function's form participates in the actual parameter
formal parameters: When defining a function, the parentheses inside the function name are "formal arguments", or simply formal arguments.
arguments: When a function is called, a variable or expression in parentheses after the function name is called an "actual parameter", or an argument, for short.
1, the argument can be a variable, a constant, or it can be an expression.
2, the parameter must specify the data type, the argument and the formal parameter must be one by one corresponding.
3. If a function does not explicitly indicate the return type of the function, then the return type of the function is int.
4, if a function does not return a value, then the return type of the function is void.
5, C language, the data transfer of the argument parameter is ' value passing ', that is, one-way pass, can only be passed to the parameter by the argument,
It cannot be passed to the argument by a formal parameter.
if the function's formal parameter is an array, you can modify the value of the argument by using a formal parameter.
such as: void Str (char s[])
{
s[0] = ' a ';
s[1] = ' B ';
}
int main (void)
{
char str1[100] = "Hello World";
str (STR1);
printf ("%s\n", str1);//output: Abllo World
return 0;
}

3. Return type and return value of function
1. The return value is obtained by return, and the return statement is not required when the returned value is void.
For example: void Max () {
Statement
}
2. The return-value data type in a return statement should be the same as the function definition.
3. If the function does not have a return statement, the function returns an indeterminate value.

4. Compilation of multiple source code file programs
1, the use of the head file
If you put the main function in the first file, and you put the custom function in the second file,
then you need to declare the function prototype in the first file.
If a function prototype is included in a header file, it is not necessary to declare its prototype every time the function is used, and it is good practice to put the function declaration into the header file.
2, #include与 the meaning of # define
#include is simple file content replacement
#denfine就是简单的文本替换
3, #ifndef and endif
#ifndef __ah__//If there is no __AH__ this macro, then compile the code between #endif, if any, no longer precompiled
#define __A__H__//The name of the specific macro is custom
in #ifndef macros, be sure to capitalize and underline, add numbers if necessary, and avoid conflicts with names in other header files.
repeat the precompiled header file content when #endif//prevent the same file for multiple include.

5. Function recursion
Recursion provides the simplest method for some programming problems.
Cons: Defective recursion can deplete computer resources, and recursive programs are difficult to understand and maintain.

#include <stdio.h>voidTestintj) { while(j>0) {J--; printf ("First Order recursion:%d\n", j);//First order recursion, code order executionTest (j);//itself calls itself printf ("Post- delivery recursion:%d\n", j);//Post -order recursion, reverse code execution        }    }

Example 1: A queue, the previous person is always 2 years old than the latter, seeking the age of the nth individual

Example 1: Seeking the age of the nth person, the previous person is always 2 years old than the later one #include<stdio.h>intAgeintn);//function Declaration    intMainvoid)    {        intn=5; printf ("age=%d\n", age (n)); return 0; }    intAgeintN) {if(n==1)        {            return Ten; }        Else{            returnAge (n1)+2;//The last one is always 2 years older than the next.        }            }
View Code

Instance 2:10 binary Turn 2 binary

#include <stdio.h>voidBinintJ//10 binary to 2, in addition to 2 for redundancy, in reverse order    {        intn = j%2; if(j>0)        {            //printf ("%d", n);//First order recursion, sequential outputBin (j/2); printf ("2 binary:%d", n);//Post -order recursion, reverse output 1101        }    }    intMainvoid)    {        inti = -;        Bin (i); return 0; }
View Code

Example 3: The summation of the fib sequence and the natural number

#include <stdio.h>intFibintN//fib sequence, 1,1,2,3,5,8 ...    {        if(n==1|| n==2){            return 1; }        if(n>2)        {            returnFIB (n2) +FIB (n1);//starting at the third position, equals the value of the first two positions and        }    }        intSumintM//the and of natural numbers    {        if(m==1)        {            return 1; }        Else{            returnSUM (M-1)+m; }    }    intMainvoid)    {        intI,res1,res2;//Enter the number of I positions required, res1 and Res2 are the return values of the FIB function and the SUM function, respectivelyscanf"%d",&i); Res1=fib (i); Res2=sum (i); printf ("The number of positions you requested is:%d\t%d", Res1,res2); return 0; }
View Code

Example 4: Recursive string length

#include <stdio.h>intSUM_STR (CharS[],inti) {if(S[i]) {returnSum_str (s,i+1); }        Else{            returni; }    }    intMainvoid)    {        Chara[ -]; scanf ("%s", a); printf ("%d\n", Sum_str (A,0)); return 0; }
View Code

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.