C++ Basics (3) Functions

  • function declaration
int sum(int, int);
  • In VC++, actual parameters take values ​​from right to left
int i=3;
test(i, ++i, i); // 3 parameter values ​​are 4, 4, 3 
i= 3 ;
test(i, i ++, i); // 3 parameter values ​​are 3, 3, 3 respectively, i==4 after execution
  • Tower of Hanoi problem
#include "iostream.h"
#include "stdio.h"
#include "stdlib.h"
int hanoi(int n,char a,char b,char c){
    if(n==1)printf("%c -> %c\n", a, c); //final plot move form a to c
    else{
        hanoi(n-1, a, c, b); //n-1 plots move from a to b by c
        printf("%c -> %c\n", a, c); //final plot move form a to c
        hanoi(n-1, b, a, c); //n-1 plots move from b to c by a
    }
    return 0;
}

int main(int argc, char* argv[])
{
    int n;
    cin>>n;
    hanoi(n, 'a', 'b', 'c');
    return 0;
}
  • Variable storage type and lifetime

Dynamic storage area: function parameters, local variables within the function. Allocates memory when the function is called, and releases it when it finishes

Static storage area: global variables, static modified local variables. The memory is allocated at the beginning of the program running and released after execution. The default initial value is 0.

auto: dynamic storage;

static: static storage;

register: Only local variables or formal parameters can be modified, stored in registers, and a variable cannot be modified together with static;

external: declarations are variables defined in other source files

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324867328&siteId=291194637