1. ## Functions

Could someone please try and explain funtions clearly to me. I understand the syntax of them but I get confused on returning values and why the prototype statement has different parameter names than the funtion itself. thanks for your help!

2. When you define a function, you give the names of its arguments something that makes sence to the function:
Code:
```void drawSquare( int upperLeftX, int upperLeftY, int lowerRightX, int lowerRightY )
{
do some stuff here
}```
Now when we call this function, we pass it variables that we've already been working with, or have defined someplace else, that already have names:

coordinate1;
coordinate2;

drawSquare( coordinate1.x, coordinate1.y, coordinate2.x, coordinate2.y );

The above would call the function using two "coordinate" structure's variables. We could have just as easily used any 4 integers.

drawSquare( 0, 0, 10, 10 );

But when we're inside the function, we want our own names for them so we know how they relate to the function.

You 'return' a value to wherever you call it, if you need to make use of something that the function was using, or what not.

int myAdd( int n, int o ) { return n+o; }

Then we could do:

int y;

y = myAdd( 3, 4 );

y will now equal 7.

Quzah.

3. ## ok

first, the function prototype does not have different param names than the function. ex.:

int func1(int x, int y); //prototype

blah blah

int func1(int x, int y) //definition, or "function itself"
{
return x+y;
//Notice that x and y in the main program are not changed, they retain the same value. Scroll down to see how to change the values.
}

This will add the values of x any y, and return them. First I want to say that you could also write the prototype like int func1(int, int); You only have to put the corresponding variable types, and leave out the name. Ok, that function adds the value of x and y and returns it to whatever you're doing. Ex.:

cout<<func1(a,b);

This will print out the sum of a and b. You could also do:

int sum=func1(a,b);

This will assign the sum of a and b to sum. Now, if you want to just want change some variables values, and return nothing, then you must use pointers, and replace int with void. Ex.:

#include "iostream.h"

void func1(int* num);

int main()
{
int num=1;
cout<<"num="<<num<<" before function is called."<<endl;
int* pnum=&num;

func1(pnum);

cout<<"num="<<num<<" after function is called."<<endl<<endl;

return 0;
}

void func1(int* num)
{
*num=999;
}

This will change num permanentlly because pointers hold the address of a variable and when passed to a function, it still has the address. Notice when you use void, you do not return a value.
You can do the same with references. Need any more help just ask. You can also return a pointer, not just the value inside of it. Ask if you want to know how.

4. mike I am a bit of a newbie myself but feel it is time to try and be of some assistance to others .

Functions as you probably know are designed to split your code up into smaller manageable pieces . In a program you may want a certain block of code to be repeated numerous times during its execution. This is the obvious advantage of functions so that you dont have to type out the same section of code more than once in a program.

In a prototype for a function here are certain options

this means no arguements are sent or received from this function .

This kind of function would probably be useful if you know the exact format of your program but you want to have less code in your

main()

function.

if you had this code
Code:
```int addnum (num1 , num2);

int main(void)

{
int a=3;
int b=4;

return 0;

}

int addnum (num1, num2)

{
return num1+num2
}```
would take the arguments a and b and use them as if they were num1 and num2 in the function then the int would be returned to the main function.

Hope this was of some help . I havent checked for errors in my code but i am sure that someone will point em out .

Hope i was of some help to you .

5. Are you Mike Herin? It's a long explanation why i think you are, but are you? And The Gweech - once you start helping other's, you'll be amazed how much you learn yourself!

6. ## Functions

Thank You all for your help

Popular pages Recent additions