Parameters

Some important parameters for functions are poin
void somethingHappens (int *a); //pointers

void somethingHappens (int& a); //address

void somethingHappens (int arg[]); //array
If I was to use the functions above, how would it look like? Give me examples.

Am I missing any other important ones?
Last edited on
Anyone else able to help me with this?
Anyone able to figure this out or know this?
The first and third function is the same, and takes a pointer as argument. You can pass an array to the function because arrays can be implicitly converted to a pointer to the first element.

If you have a pointer to an int that you want to pass to the function you just pass it as it is.
1
2
3
int* p;
// Don't forget to initialize the pointer before passing it to the function.
somethingHappens(p)


If you have an int variable, and you want to pass a pointer to that variable to the function you can use operator&.
1
2
int i;
somethingHappens(&i);


Passing an array will look like this.
1
2
int array[3] = {1, 2, 3};
somethingHappens(array);
What is actually passed to the function is a pointer to the first element in the array, which is 1 in the example above.


The second function takes a reference to an int as argument. If you have an int variable, and you want to pass a reference to that variable to the function you just pass it as it is.
1
2
int i;
somethingHappens(i);
Last edited on
1
2
int a;
somethingHappens(&a);  // pass pointer to a 


1
2
int a;
somethingHappens(a);  // pass a by reference 


1
2
int a[5];
somethingHappens(a);  // pass a as an array 


Missing:
1
2
3
4
void somethingHappens(int a);  // by value

int a;
somethingHappens(a);  //  pass a by value 


Clear?
@Peter87
@AbstractionAnon
Thanks for explaining everything to me. This cleared up a lot of my confusion.
i have same problem... but after visited this page.... problem almost solve..
closed account (zb0S216C)
I'd just like to point something out: if you need to change the value of a built-in type through a function, don't pass it by reference; instead, prefer to return the new value:

1
2
3
4
5
6
7
8
9
10
11
12
int CreateNew_Value(int CurrentValue)
{
    return(CurrentValue + ...);
}

int main( )
{
    int Value(0);
    
    // Do something...
    Value = CreateNew_Value(Value);
}

Wazzak
@Royeiln
What other problems do you still have?
Topic archived. No new replies allowed.