Depth understanding of the function

Function parses
1. Why use function
wants to know why you want to use the function, then we need to know what is the function?
Function is a piece of code can be reused, a feature to be done independently, may be used to receive user data transmitted. Americans will function called "Function", Function addition to "function" means, and "function" means, in essence, the program is made up of functions, it can be said that the program is a functional component .
After understanding the function, we begin to understand how to use the function?
When a function is very small, we can directly write all the code main (primary function); but when we begin a massive program to develop the program more complex, if all of the code will still put in the main, our development will become increasingly difficult role this time function is very practical.
(1) can be reused multiple times in the function call, to avoid duplication of the code, the function can also be shared.
(2) reduce the complexity of the program will be a complex task into smaller, simpler tasks
...

int sum(int a,int b)
{
    int num;
    num=a+b;
    return num;
}

int main()
{
    int x=10,y=11,z=12;
    int num1,num2;
    num1=sum(x+y);
    num2=sum(x+z);
    return 0;
}

...
I used a simple addition function, to reflect the convenience function for the program.
2. Why use function overloading
Again, let's look at function overloading?
Function overloading refers to the same scope, there may be a group with the same function name, the list of functions of different functions. Overloads a set of functions commonly used to designate similar features.
What good is it overloaded function
(1) reduce the number of function names, to avoid the contamination of the name space
(2) increases the readability of the program
...

int sumOfSquare(int a,int b)
{
    return a*a+b*b;
}

double sumOfSquare(double a,double b)
{
    return a*a+b*b;
}

int main()
{
    int m,n;
    cout<<"Enter two integer:";
    cin>>m>>n;
    cout<,"Their sum of square:"<<sumOfSquare(x,y)<<endl;
    double x,y;
    cout<<"Enter two integer:";
    cin>>x>>y;
    cout<<"Their sum of square:"sumOfSquare(x,y)<<endl;
    return 0;
}

...
3, what is the value of the passed
value in the call transfer means the process is passed to the parameter value of the respective arguments in the calling procedure, each parameter of actual and occupy different bit values of the storage space, the process is performed , changing the value of a variable parameter, but the value of the argument does not call process.
To learn more about the value passed by the experiment
...

#include <stdio.h>
void swap(int a,int b)
{
    int t;
    printf("(2)a=%d  b=%d\n",a,b);  /*调用函数过后形参的值*/
    t=a;a=b;b=t;
    printf("(3)a=%d  b=%d\n",a,b);  /*调用函数内交换后形参的值*/
}
int main()
{   
    int a=10,b=20;
    printf("(1)a=%d  b=%d\n",a,b);  /*实参a,b未调用函数时的值*/
    swap(a,b);
    printf("(4)a=%d  b=%d\n",a,b);  /*实参a,b调用后的值*/
    return 0;
}

...
4 What is the address of the transmission
address transmission means is invoked when the process is to pass the memory address of the argument to the called procedure variable parameter, is actually involved in memory cells formed with the same address argument, change the parameter value is changed at the arguments.
By experiment to learn more about address delivered
...

void fun(int *b)
{
    *b = 1; 
}

int main()
{
    int a = 0;
    fun(&a);
    printf("a=%d\n", a);
    return 0;
}

...
5, recursive functions
for recursive functions, my understanding is that when the function is executed, a function called directly or indirectly function itself.
Fibonacci column which is a very common recursive function.
...

int Fibon(int n)
{
    if(n==1||n==2)
    {
        return 1;
    }
    else
    {
        return Fibon(n-1)+Fibon(n-2);
    }
}

...

Guess you like

Origin www.cnblogs.com/xlog/p/11505636.html