A simple question

closed account (1v5E3TCk)
Helloeveryone,
I don't understand an exercise in my book. What he want me to do?


4. Write a function that takes two input arguments and provides two separate results to the caller, one
that is the result of multiplying the two arguments, the other the result of adding them. Since you can
directly return only one value from a function, you'll need the second value to be returned through a
pointer or reference parameter.


I code like that but I dont know if it is right answer:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>

using namespace std;


void islem( int *a , int *b )
{
	int temp1 = *a;
	int temp2 = *b;
	
	*a = temp1*temp2;
	*b = temp1+temp2;
}



int main()
{
	int a;
	int b;
	
	cout<< "Birinci sayiyi giriniz:";
	cin>> a;
	
	cout<< "İkinci sayiyi giriniz:";
	cin>> b;
	
	islem( &a, &b );
	
	cout<< "Carpim:" << a << '\n' ;
	cout<< "Toplam:" << b ;
}
From the sound of the exercise (surely named "Poorly designed function") it would look more like the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

int addAndMultiply( int a, int b, int& prod )
{
    prod = a*b ;
    return a+b ;
}

int main()
{
    int a, b ;

    std::cout << "Enter a number: " ;
    std::cin >> a ;
    
    std::cout << "Enter another number: " ;
    std::cin >> b ;

    int product ;
    int sum = addAndMultiply(a, b, product) ;

    std::cout << "Sum: " << sum ;
    std::cout << "\nProduct: " << product << '\n' ;
}
closed account (EwCjE3v7)
@cire

Why do you use std:: why not just put
using namespace std;
its less code
closed account (1v5E3TCk)
@Captain
For using less memory I think.

@cire
Thank you I understand it.
senhor wrote:
@Captain
For using less memory I think.

No, using namespace std; does not affect the memory usage of the program.
closed account (1v5E3TCk)
It was only a guess.
If so why so many people usee std:: ?
So it doesn't pollute the global namespace & cause naming conflicts for variables & functions.

Ideally, one should put their own code into it's own namespace.

HTH
closed account (EwCjE3v7)
@senhor
I knew its not about the memory but you should not give out false information. Be 100% sure because a small problem can cause other problems...
People just have their own ways
CaptainBlastXD wrote:
@senhor
I knew its not about the memory but you should not give out false information. Be 100% sure because a small problem can cause other problems...

senhor didn't give false information. He said he thought that was the reason and we have no reason do doubt he didn't think so.
Why not put this in the Beginners section?
closed account (EwCjE3v7)
Oh sorry senhor u wrote I think..I never read that...sry.....next time I should be 100% sure of what I read :) ...sry
closed account (1v5E3TCk)
@Captain
It is not a problem Captain.

@Peter87
Thanks.
Topic archived. No new replies allowed.