# Thread: Explanation of arguments/parameters in Functions

1. ## Explanation of arguments/parameters in Functions

I am not 100% sure about functions at the moment.

[1] Declare function above main.

Code:
`int qcheck(int holdans, int *p);`

[2] Call function:

Code:
`qcheck (holdans, &score);`
[3] Function definition that is called:

Code:
```int qcheck(int holdans, int *p)
{
code
}```

Question.
Does 1 and 2 always have to match exactly?

p.s I love pointers (when I understand them), they can be so simple and short.

2. Yes, they should be.
You are asking if 1 and 3 have to match exactly, right?

3. Originally Posted by hellogamesmaste
Does 1 and 2 always have to match exactly?
That depends on what you mean by "match exactly". For example, a parameter could be declared as a const int*, but you can pass a pointer to a non-const int as a corresponding argument.

4. 2 is coded wrong. It returns an int, therefore assign the return value to an int.

5. Originally Posted by Dino
2 is coded wrong. It returns an int, therefore assign the return value to an int.
It is not a requirement to read the return value of a function. Do you always assign the return value of printf() to a variable when you use it?

6. The return value is optional... it's correct code. Whether it's semantically correct or not, we can only guess.

7. I ask because my program didn't work and I didn't know why.

In [1] I had:

Code:
`int qcheck(int , int *p);`
in [3] I had:

Code:
`int qcheck(holdans, int *p)`

So I kept getting error messages because the int was missing in 3.

Another question, regarding pointers in functions.

Usually you do the following:

int a, *p
p = &a
*p = 10

However, with functions, you can leave out the p = &a by:

Code:
`int qcheck(int , int *p);`
Code:
`qcheck (holdans, &score);`
Passing the &score into a calling function to a called function that has int *p as a parameter, passing p to &score is therefore not necessary. Does the function do this automatically?

I got really confused and kept doing in main:

Code:
`p=&score`
and then tried to
Code:
`qcheck(holdans, p)`
because I thought it would pass the address.

I got it working in the end, but I kept thinking of pointing p to &score. Obviously this must not be necessary when passing pointers to functions.

8. So I kept getting error messages because the int was missing in 3.
yeah - you need to put the type in both places

However, with functions, you can leave out the p = &a by:
Whenever you pass a value to a function, the values you pass in are assigned to the local variables specified in the parameters. In other words, if I have a function

Code:
`add(int a, int b)`
that I call from main, main shouldn't be manually assigning anything to a and b, because they're not in scope for main. All you do is call the function as
Code:
`add(1, 3)`
and the values 1 and 3 are assigned to a and b as local variables inside the function. The same goes for pointer. If you accept a pointer as a parameter, whatever address is passed in is assigned to that pointer.

9. Ahh thank you. That has made it clearer. I just hope it sticks

Popular pages Recent additions