C51 learning experience, function call by reference and function call by value

Publisher:cheng1984Latest update time:2018-01-03 Source: eefocusKeywords:C51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Call by value creates a copy of the parameter and passes it to the called function. Modifying the copy of the parameter value in the calling function does not affect the original variable value; call by reference allows the calling function to modify the value of the original variable.

C language uses pointer * and indirect reference operator & to simulate call by reference. Arrays will automatically simulate call by reference. Call by reference can modify the parameter variables in the calling function environment in the called function, and call by value protects data.

eg

(1) Call by value

int cubeByValue(int);

main()

{ int num=5,result;

result=cubeByValue(num);

}

int cubeByValue(int n)

{

return n*n*n;

}

(2) Call by reference


int cubeByValue(int *);

main()

{ int num=5,result;

cubeByValue(&num); // pass the address of num to nPtr

printf("%d",num);

}

int cubeByValue(int *nPtr)

{

*nPtr=*nPtr * *nPtr * *nPtr;

}



int array[size];

void modifyarray(int [],int );

void  MIDI fyarray(array,size)

{

void swap( int *,int *);

......

}

Note: The function prototype swap can be defined in the calling function midifyarray, as shown above, to ensure that swap can only be called correctly in the calling function m


Keywords:C51 Reference address:C51 learning experience, function call by reference and function call by value

Previous article:C51 Programming Nested Design
Next article:Buffered Input Design in C Program

Recommended ReadingLatest update time:2024-11-16 13:01

C51 Compiler - Advanced Programming Skills (2) - Segment Naming Convention
Segment Naming Conventions The objects (program code, program data and constant data) generated by the Cx51 compiler are stored in the form of program segments. A segment is a unit of code and data. A segment may be relocatable or its address may be absolute. Each relocatable segment has a type and a name. The segme
[Microcontroller]
Solution to directly use binary numbers in Keil C51
    In Keil C51, numbers cannot be assigned directly in binary form, although it is possible in 8051 assembly. Although binary numbers are long to write and prone to errors, because they are written one bit at a time, programmers can clearly see the status of each bit, which is more intuitive. Therefore, many people m
[Microcontroller]
[C51 code] Menu function [for LCD1602]
#ifndef __menu_h__ #define __menu_h__ #include "head.h" #include "LCD1602_2.h" #include "DS18B20.h" #define Null 0 /*********************** * Function declaration * ***********************/ void ShowMenu(void); void Menu_Change(volatile KeyNum); /*********************** * Key function key macro defi
[Microcontroller]
The volatile keyword in C51
I wrote a "weird" program these days. I say it's "weird" because when I debug it on a single chip, the value of a variable always remains unchanged. The most typical "weird" statement is: _uKey = P2;. As a result, the value of _uKey is still the value when it was initially defined (0x00). I was so angry about it. I ask
[Microcontroller]
C51 single chip counter experiment
Experimental requirements Implement a stopwatch display. Specifically, draw a MSC51 microcontroller and two eight-segment digital tubes. After running, the digital tubes will display 00-59 in sequence, display in a cycle, and jump back after 59. Interrupts must be used, T0 or T1 is not limited, mode 0, 1, 2 is not l
[Microcontroller]
C51 single chip counter experiment
The microcontroller part of the program for serial communication between C51 microcontroller and PC
Note that the baud rate is 9600 Function: The computer sends a byte to the microcontroller through the serial port, and the microcontroller sends it back to the computer after receiving it. 、#include #define uchar unsigned char #define uint unsigned int uint temp; bit flag; void init_ser1(); main() { init_ser1();
[Microcontroller]
C51 MCU Study Notes (IV) - MCU Interrupt System and Application
1. MCU interrupt system The concept of interruption: When the CPU is processing an event A, another event B occurs, requiring the CPU to process it quickly (an interrupt is generated); The CPU temporarily interrupts the current work and switches to process event B (interrupt response and interrupt service); After the
[Microcontroller]
C51 MCU Study Notes (IV) - MCU Interrupt System and Application
C51 program-controlled laser advertising system
introduction At present, laser technology is developing day by day and is widely used in basic research, medical treatment, machinery, communication and other fields. Computers are often used for visual laser control because they have high speed and large storage space and can control large systems. When controlling
[Microcontroller]
C51 program-controlled laser advertising system
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号