Why Does a Function with two differing variable types work?

Hi all, why does this work? the function type i wrote is int, but i have a string in the argument area? Sorry for the Noob question.. Can you help me understand a bit better?


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
  // EduFunction_Class.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int display_int(int numbr, string chr) // You can transfer both int and Char in one Function and more. 
									   //Despite the fact that string is in the int display_int function
{
									//Multiple arguments can be created here......
	cout << endl << numbr; // only use what is in the funtion unless you need to calculate here.
	cout << endl << chr; // only add to the string using another string variable if you wish to extend.
	cout << endl;
	return 0; // can i return this to main?? "Use another funtion to explore...?"
}


int main()
{
	int number = 15;
	string cr = "99 Clearly numbers can be strings too!";

	display_int(number,cr); // see funtion for output display.

	cin.ignore(); // doesn't allow any key to end an app though...

	//system("pause"); // cheat for pause.
    return 0;
}
main() is a function and can have any number of variables
Hi all, why does this work? the function type i wrote is int, but i have a string in the argument area? Sorry for the Noob question.. Can you help me understand a bit better?


The function type tells you what kind of value the function returns.

That is completely unrelated to the types of the arguments the function takes.
Last edited on
Here's an example to help you visualize a common pattern of returning a status: 'true' if a function call succeeds or 'false' if it fails.

Since functions return one thing, if you'd like both a status and function-related output, use outgoing parameters. In C++ these are typically references (but could also be pointers). [ Related but not shown here: to denote that a reference will be read-only and not modified, add the keyword const ]

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
#include <iostream>

using namespace std;

// Performs a/b division, stored in result
// Returns true if successful; false if b is 0
bool Divide(double a, double b, double& result)
{
    if (b==0)
        return false;
    result = a/b;
    return true;
}
    
int main() 
{
    double result;
    double a = 5;
    cout << boolalpha;
    bool success;
    for (int b=0; b<=5; ++b)
    {
        success = Divide(a, b, result);
        cout << a << '/' << b << ".  Success? " << success;
        if (success)
            cout << "  Result: " << result;
        cout << endl;
    }

    return 0;
}

5/0.  Success? false
5/1.  Success? true  Result: 5
5/2.  Success? true  Result: 2.5
5/3.  Success? true  Result: 1.66667
5/4.  Success? true  Result: 1.25
5/5.  Success? true  Result: 1


Another common status pattern is to return an integer: often it's 0 on success or non-zero on fail (just like int main());
Last edited on
Topic archived. No new replies allowed.