Call the function by value in C ++

The method calls the function by the value of the parameters passed to a function that copies the actual value of a parameter into the official argument of the function. In this case, changes made to the parameter inside the function have no effect on the parameter.

By default, C ++ Language uses the call function by value to pass parameters. Under the general sense, the code inside a function cannot change the parameter used to call the function. Suppose the definition of the function traodoi () is as follows:

 // phan dinh nghia ham de trao doi cac gia tri. void traodoi ( int x , int y ) { int temp ; temp = x ; /* luu giu gia tri cua x */ x = y ; /* dat y vao trong x */ y = temp ; /* dat x vao trong y */ return ; } 

Now we call the function traodoi () by passing true values ​​like the following example:

 #include using namespace std ; // Phan khai bao ham void traodoi ( int x , int y ); int main () { // Khai bao bien cuc bo: int a = 100 ; int b = 200 ; cout << "Truoc khi trao doi, gia tri cua a la: " << a << endl ; cout << "Truoc khi trao doi, gia tri cua b la: " << b << endl ; // goi ham de trao doi cac gia tri. traodoi ( a , b ); cout << "Sau khi trao doi, gia tri cua a la: " << a << endl ; cout << "Sau khi trao doi, gia tri cua b la: " << b << endl ; return 0 ; } 

You put the above function definition at the end of this code, then compile and run the above C ++ program will produce the following result:

Call the function by value in C ++ Picture 1

It indicates that there is no change in value even though they have been changed inside the function.

According to Tutorialspoint

Previous article: Function in C / C ++

Next lesson: Call the function by pointer in C ++

4 ★ | 1 Vote

May be interested

  • Shell functionsShell functions
    functions allow you to streamline an overall feature of a script into a smaller and more logical part that can perform the same function whenever it is needed through the function call.
  • Anti-spam call with 'cricket': Why not?Anti-spam call with 'cricket': Why not?
    in addition to the services provided by carriers, phone companies have designed anti-spam features for their users when designing.
  • JavaScript functionsJavaScript functions
    a function is a group of reusable code that can be called anywhere in your program. this makes it unnecessary to write the same code over and over again. it helps programmers write modular code. functions allow a programmer to divide a large program into small and manageable functions.
  • Call recording Android with Call Recorder - ACRCall recording Android with Call Recorder - ACR
    call recorder - acr is a free call recorder application on android, many options: record incoming calls with any phone number, arrange call recording by date, ... this is the way to record the call. call with call recorder - acr for you.
  • How to call video call on Zalo PCHow to call video call on Zalo PC
    calling video calls is a new feature in the zalo pc version, allowing users to call video directly from zalo on the computer.
  • How to call, video Facebook Messenger on mobile, tabletHow to call, video Facebook Messenger on mobile, tablet
    the call feature, facebook video call on the computer has been around for a long time, but recently, facebook has supported this feature on phones and tablets. users can now comfortably chat, text, and video calls from their facebook accounts.
  • Zalo allows direct calling from contactsZalo allows direct calling from contacts
    on the new version zalo has allowed to integrate the call feature in zalo's contacts, no need to open the application to save time.
  • Save time with these text formatting functions in Microsoft ExcelSave time with these text formatting functions in Microsoft Excel
    microsoft excel is a main application for anyone who has to work with numbers, from students to accountants. but its usefulness extends beyond a large database, it can do a lot of great things with text. the functions listed below will help you analyze, edit, convert, change text and save many hours of boring and repetitive tasks.
  • Call command in WindowsCall command in Windows
    the call command calls a batch program from another program without stopping the original batch program. the call command accepts the label as its target.
  • Free Conference Call - Download Free Conference Call here.Free Conference Call - Download Free Conference Call here.
    free conference call is a free online meeting tool that allows screen sharing. this award-winning tool allows you to host an unlimited number of meetings with up to 1,000 participants.