zoukankan      html  css  js  c++  java
  • <C Primer Plus >2 Altering Variables in the Calling Function

     1 #include <stdio.h>
     2 int diff(int x, int y);//the first form
     3 void interchange(int *u, int *v);//the second form
     4 int main(void){
     5     int x = 9;
     6     int y = 5;
     7     int z;
     8     printf("The Originally x = %d and y = %d
    ", x, y);//alter variables in the calling function
     9     interchange(&x, &y);
    10     z = diff(x, y);//need a value for some calculation or action
    11     printf("Now  x = %d and y = %d
    ", x, y);
    12     printf("z = %d
    ", z);
    13     return 0;
    14 }
    15 
    16 int diff(int x, int y){
    17     return(x - y);
    18 }
    19 
    20 void interchange(int *u,int *v){
    21     int temp;
    22 
    23     temp = *u;
    24     *u = *v;
    25     *v = temp;
    26 }

    int function1(int x);

    int function2(int *ptr);

    Remeber:

    1 Use the first form if the function needs a value for some calculation or action;

    2 Use the second form if the function needs to alter variables in the calling function.

      One function does not have direct access to variables declared in another function. If you do need one function to acess another function's data,you can use pointer function arguments.

  • 相关阅读:
    JS知识点简单总结
    Js答辩总结
    JS答辩习题
    轮播
    jQuery选择器总结
    JS的魅力
    JS与JAVA数据类型的区别
    单表查询、多表查询、虚拟表连接查询
    Mysql基本语句
    Mysql数据库
  • 原文地址:https://www.cnblogs.com/michael2016/p/6580684.html
Copyright © 2011-2022 走看看