Calling functions to main

I'm trying to understand how calling functions to the main works. For example

int fun1(int num1);
int fun2(int *p);
int fun3(int &num2);

I know how to call them in the main in
int x1=5 or it was just int *p1.

I do not know how to do it if it was

int *p2
p2 = new int[10]

or

int x2[10]

so basically I'm having trouble with arrays and functions. If you could help me by explaining why say for example

(the one i actually know) ~ fun2(x2) would be x2.

Thanks!!
When we call a function, that block of code executes (get's placed on the stack).
There's a number of key identifiers you need to be away of:

1
2
3
4
returnType name(parameters)
{
    return type value;
}


So in the case of a simple function that prints a message:

1
2
3
4
void printMessage(void)
{
    cout << "Hello World!";
}


Here the above code follows the same format void is the return type, so we return nothing. the name is "printMessage" and the parameters are void, another way I could have written this would be void printMessage()

1
2
3
4
5
int product(int a, int b)
{
    int prod;
    return (prod = a * b);
}


here we specify the return type of type "int", the name as "product" the parameters int a, & b. we then calculate and return the answer. which is an int.

now calling these functions from main you simply:

1
2
3
4
5
printMessage();

int mynum=52, anotherNum=89283, answer;

answer = product(mynum, anotherNum);


http://www.cplusplus.com/doc/tutorial/functions/
Last edited on
Thanks, I understand how to do that much, but I'm not sure how to do the arrays.

int doit(int *p){

...
 return 0;
}


int main(){

int *p2;
p2 = new int[10];

doit(??);


the same goes for

int fun3(int &num2);
int fun1(int num1);

i'm not sure what to put in the parentheses
With that function, just do doit(p2);

The other two functions can't be used with the array, as they each only accept a single integer.
Topic archived. No new replies allowed.