Hi! I have two questions about functions. First how can I use a main() local variable in a function outside of main? And how can I use a variable in the function paramaters which already has stored information (stored in main() ), and not have to give it new information when I initiliaze the function [ in main ].

Sorry If the questions are dumb or unlogical, Its kind of hard to explain them.

Thank you,

AssaultM16

Dani AI

Generated

The compiler error you saw ("too few arguments") comes from a mismatch between the function declaration and the call. The parameter names in a prototype are just placeholders — they do not refer to the local variables in main. Either pass the needed value(s) when you call the function, or change the function so it doesn't require those extra arguments.

Two better options (beyond making the call match the prototype):

  • Make the second parameter optional with a default value (declare the default in the prototype/declaration):
#include <string>
#include <iostream>

void greet(const std::string& a, const std::string& b = "")
{
    if (b.empty()) std::cout << "Hello " << a << '\n';
    else std::cout << "Hello " << a << " and " << b << '\n';
}
  • Pass by reference when the function should modify the caller's variable (or use const& to avoid copying large objects):
#include <string>

void set_default(std::string& s)
{
    if (s.empty()) s = "Anonymous";
}

Quick practical notes:

  • You used std::string but included <cstring>; include <string> ( <cstring> is for C-style string functions).
  • If a function doesn't return a meaningful integer, use void as the return type.
  • Default arguments belong in the declaration (or prototype) so callers can see them.
  • Avoid globals unless necessary; prefer explicit parameters (clearer and easier to test).
  • Enable compiler warnings (for example, g++ -Wall -Wextra) — they point right to signature mismatches and typos.

As suggested, parameters are the normal way to move data from main into other functions; described pass-by-value correctly; and caught the argument-count/typo issues. Use default parameters or references when appropriate, correct your includes, and the error will go away.

Recommended Answers

All 7 Replies

1. pass the variable as a parameter to the function.

2. you mean something like this?

void foo(int x)
{
    cout << "x = " << x << "\n";
}

int main()
{
    int n = 123;
    foo(n);
}

>First how can I use a main...?
Using function parameters.
>and not have to give it new information.
Value parameter (Called By Value). - A new storage location is created when the value of an actual argument is passed to a formal argument of function, and modification of the formal parameter does not impact the actual parameter.

void Test(int n) {
       n=10;
   }
  int main(){
      int a=1,n=2;
      Test(a); 
      Test(n);
      std::cout << a << "  " << b;
  return 0;
  }

A correction on Adatapost's post:
This line: std::cout << a << " " << b; should actually be : std::cout << a << " " << n; A small typo :)

commented: Thanks buddy. +11

Thanks for the replies but I believe I wasn't clear enough. Let me show you this prototype example:

#include <iostream>
#include <cstring>

using namespace std;

int name(string a, string name1);

int main() {

    string name1;
    string name2;

    cout<<"What is your name";
    getline(cin,name1);

    cout<<"And what is yours?";
    getline(cin,name2);

    name(name2);

    return 0;

}

int name(string a, string name1) {

    cout<<"Hello" << a << "and hello" << name1;

    cin.get();

    return 0;

}

It gives me these errors:

-too few arguments in the name function;

How would I be able to use name1 in the function ?

Thank You,

AssaultM16

Call it with 2 variables as you've declared it, so change: name(name2); to name(name1, name2);

Thanks niek, I can't believe it was that easy!

your function prototype is declared like so :

//accepts 2 string not 1 or 3.
int name(string a, string name1);
cout<<"And what is yours?";
    getline(cin,name2);

    name(name2); //you call it with 1 paramter, or 1 string

Look below :

string name1 = "jack";
string name2 = "jill";
name(name1,name2); //valid -- Good
name(name2); //invalid -- bad
name(name1); //invalid -- bad
name(name2, name 1) ; // valid -- Good
name(name1,name2, name1); // invalid -- bad
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.