?
Solved

Confused - Functions

Posted on 2005-03-12
15
Medium Priority
?
198 Views
Last Modified: 2010-04-15


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
Comment
Question by:andyw27
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +3
15 Comments
 
LVL 12

Accepted Solution

by:
stefan73 earned 504 total points
ID: 13525237
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
 
LVL 12

Expert Comment

by:stefan73
ID: 13525244
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
 
LVL 37

Assisted Solution

by:Harisha M G
Harisha M G earned 498 total points
ID: 13525526
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
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.

 
LVL 1

Expert Comment

by:njava
ID: 13527105
andyw27:

Think of it that one function returns a value and another function takes this value, uses it and returns its own value.
0
 
LVL 8

Assisted Solution

by:ssnkumar
ssnkumar earned 498 total points
ID: 13532968
> 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
 

Author Comment

by:andyw27
ID: 13548674

thanks some useful advice, I will now go away and try work through the code given here.
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 13552531
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
 
LVL 3

Expert Comment

by:harsha_dp
ID: 13580783
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
 
LVL 37

Expert Comment

by:Harisha M G
ID: 13755149
A split ??
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 13771320
I think all the answers deserve points :)

stefan73,
me,
ssnkumar,
harshadp

Is that ok?
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 13779963
Thanks for counting me in Harish:-))
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question