Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

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.
0
andyw27
Asked:
andyw27
  • 3
  • 3
  • 2
  • +3
3 Solutions
 
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
 
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 GCommented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 GCommented:
A split ??
0
 
Harisha M GCommented:
I think all the answers deserve points :)

stefan73,
me,
ssnkumar,
harshadp

Is that ok?
0
 
ssnkumarCommented:
Thanks for counting me in Harish:-))
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now