Hey, Yet again i was just mucking around on DEV-C++ and made this code..

#include <conio.h>
#include <iostream>

using namespace std;

unsigned long Double(unsigned long doubleMe)
{
         return (2 * doubleMe);
}

int main()
{
    unsigned long numberToDouble;
    unsigned long doubledNumber;
       
    cout << "Enter a number you would like to double : ";
    cin >> numberToDouble;
    
    doubledNumber = numberToDouble;
    Double(doubledNumber);
    
    system("cls");
    
    cout << numberToDouble << " Doubled = " << doubledNumber;
    
    getch();
}

It is meant to cout the original number and then the doubled number, but if i enter 7, it cout's 7 doubled = 7

Help please?

I tried declaring it before using it but it said something like too many arguments,do i need to declarelike unsigned long Double(..); or not?

Dani AI

Generated

A few quick points that directly follow from ’s post and ’s hint.

The function call Double(...) returns a value but does not change the variable you pass in, because parameters are passed by value. If you want the doubled value to be stored, assign the return to a variable. A compact, modern example with different names:

#include <iostream>

unsigned long timesTwo(unsigned long x) {
    return x * 2;
}

int main() {
    unsigned long n;
    std::cout << "Enter a number: ";
    if (!(std::cin >> n)) return 1;
    unsigned long doubled = timesTwo(n);
    std::cout << n << " doubled = " << doubled << '\n';
}

If you intended the function to modify the argument in place, use a reference parameter instead of returning a value:

void timesTwoInPlace(unsigned long &x) {
    x *= 2;
}

// usage:
// timesTwoInPlace(n);
// then n holds the doubled value

If you add a prototype before main, make sure it matches the signature exactly (for example unsigned long timesTwo(unsigned long);). A mismatched prototype like unsigned long Double(); will cause a “too many arguments” error when you call it with one parameter.

Other small tips: avoid nonstandard headers like <conio.h> and getch() for portable code (use std::cin.get() or let the console close naturally), system("cls") is Windows-only, enable compiler warnings (e.g. -Wall -Wextra) to catch mistakes early, and be mindful of overflow when doubling large unsigned values.

Recommended Answers

All 2 Replies

You need to assign the result on line 20.

Thank You!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.