• C

Confused - Functions



I'm trying to get my head round using functions in C.

I understand that I have to declare a prototype but the thing which is confusing me the most is passing the values back and fourth from several functions ?

Can anybody explain the basic principles of this please (in laymans terms) ?

Thanks.
andyw27Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

stefan73Commented:
Hi andyw27,
Functions can return values and have parameters. Example:

int add3(int input){
    return input + 3;
}

This you can simply call as
int myvar = 5;
myvar = add3(myvar);

...and myvar will be 8.

You can also do by reference passing, which can modify the value in place:

void add3(int* input){
    *input += 3;
}
(note the void return type)

...and this will be called as:

int myvar = 5;
add3(&myvar);

...and myvar will be 8 again.


Cheers!

Stefan
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
stefan73Commented:
BTW: You can also nest function calls, such as:

int add3(int input){
    return input + 3;
}

int myvar = 5;
myvar = add3(myvar+add3(myvar));

...and myvar will be ((5+ ((5)+3))+3) = 16.
0
Harisha M GEngineerCommented:
A functin will have generally this form....

<ReturnDataType> function_name(<DataType 1> <Var1>,<DataType2> <Var2>, ...)
{
     /* Your code in the function */
     return <value or expression or variable.>
}

Now, let us consider a simple example...

void myprint(int a)
{
     printf("%d",a);
}

main()
{
     myprint(10);
}

When you run this program, the control is initially at the first statement of the main() function. So myprint() function is called with argument 10.
This 10 will get "stored" in a in the called function. When you print a, you are in turn printing the value of a which is currently present. And if you call the same function with myprint(15), 15 will get printed. Here 'a' is only namesake and it can be anything...
a, b, or, andyw27


Also consider another example...

int next(int x)
{
      return x+1;
}

main()
{
      printf("%d",next(1));
}

When execution starts, printf() function is called, which in turn calls next() function.
Now, next(1) means, x will get a value of 1 and next will return x + 1 = 2.
So, equivalently, printf("%d",2); which prints 2.

Hope you understand. And feel free to ask if you have any more questions
0
The Firewall Audit Checklist

Preparing for a firewall audit today is almost impossible.
AlgoSec, together with some of the largest global organizations and auditors, has created a checklist to follow when preparing for your firewall audit. Simplify risk mitigation while staying compliant all of the time!

njavaCommented:
andyw27:

Think of it that one function returns a value and another function takes this value, uses it and returns its own value.
0
ssnkumarCommented:
> I understand that I have to declare a prototype but the thing which is confusing me the most is passing the values back and fourth from several functions ?
To functions you can send values. And functions can return single value.

It works like this. Say you have four operations to do: add, subtract, multiply and divide.
So, to all of them you send two values. All of them return one value (that is the result).
So, your functions become:
int add(int a, int b)
{
   int c;
   c = a + b;

   return c;
}

int subtract(int a, int b)
{
   int c;
   c = a - b;

   return c;
}

int multiply(int a, int b)
{
   int c;
   c = a * b;

   return c;
}

int divide(int a, int b)
{
   int c;
   c = a / b;

   return c;
}

So, you can call these function from main like this:
main()
{
   int i = 20, j = 10, result;

   result = add(i, j);
   result = subtract(i, j);
   result = multiply(i, j);
   result = divide(i, j);
}

In the main() function, you are sending two values (20 and 10 stored in i and j respectively) to each of those functions.
In these functions these values gets copied to variables a and b.
And c is a temporary variable to which you will put the result.
Then the function returns the value stored in c.
In the main() function, you will receive this returned value in a variable called result (which you can use to display or use in another operation).

Hope this clears the doubt.
If not, your feed back is needed to understand what is not understood.

-ssnkumar
0
andyw27Author Commented:

thanks some useful advice, I will now go away and try work through the code given here.
0
ssnkumarCommented:
Give your feedback on this.
If you have still some confusions/questions left, we can try to help you based on your feedback.

If every thing is really answered, then close the question.

-ssnkumar
0
harsha_dpCommented:
Functions are the modules which may or may not return a value, if returns it is unique value.
When we invoke the function compiler searches for viable functions, then it decides which function to invoke amongst the vaiable functions. If there is no such functions generates an error. the arguments or parameters are communicated between function call statement and function called thru stack. If the parameter itself is a function call, compiler has to check return type of the function whether it will match the required parameter at that position.  
void fcn(int,int,double)
  can be inoked like fcn([integer constant or function that returns integer compatible data],[integer constant or function that returns integer compatible data],[double constant / function returning double data] )
During the call implicit conversion will be done if needed.
0
Harisha M GEngineerCommented:
A split ??
0
Harisha M GEngineerCommented:
I think all the answers deserve points :)

stefan73,
me,
ssnkumar,
harshadp

Is that ok?
0
ssnkumarCommented:
Thanks for counting me in Harish:-))
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.