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,


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;

>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) {
  int main(){
      int a=1,n=2;
      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 :)

Thanks buddy.

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";

    cout<<"And what is yours?";


    return 0;


int name(string a, string name1) {

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


    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,


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

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?";

    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
This question has already been answered. Start a new discussion instead.