Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How functions return multiple values in C++

Shulou Source: shulou.com Published: 2022-06-02 08:05:53 10月03日 Update

What this article shares with you is about how functions in C++ return multiple values. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article. Let's take a look at it with the editor.

1. There is a thorny problem: (how to return multiple values by the CAccord Cure + function? )

I encapsulate the algorithms into different functions, and it's easy to call this function in matlab, as follows:

% matlab function example function [outputArg1,outputArg2] = test_func (inputArg1,inputArg2)% test_func A summary of this function is shown here. The detailed description outputArg1 = inputArg1;outputArg2 = inputArg2;end is shown here.

In matlab, it's easy to output multiple values.

However, it is not so easy for you to return (output) multiple values in a C function.

two。 What shall I do? (code example)

There are three methods, and I will only say the most commonly used and effective one.

When the function is called, pass the parameter with the address and use the pointer to change its value

In this way, the modified value becomes the original parameter.

Go directly to the code:

1) main function / / main.cpp

/ / main.cpp#include # include "mycal.h" int main () {int a = 2ntint b = 1poliint citint c1ittmyaddyogfunc (a maestro bmemorie cepagenet C 1); printf ("c =% d\ n", c); printf ("C1 =% d\ n", C1); return 0;}

2) Sub-function / / mycal.cpp

/ / mycal.cpp#includeint myadd_func (int a _ line int bline int * c, int * c _ 1) {* c = a + b _ politic _ c _ 1 = a + b _ blank 100 percent return 0;}

3) header file corresponding to subfunction

/ / mycal.hint myadd_func (int a, int b, int * c, int * C1)

Running result:

The above is how the function in C++ returns multiple values. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: Functions multiple clocked codes parameters more knowledge examples articles related output different original practical effective tricky address that is work meeting commonly used Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Docker NVidia OPPO Reno MariaDB