Solved

# Confused - Functions

Posted on 2005-03-12
Medium Priority
198 Views

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
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
• 3
• 3
• 2
• +3

LVL 12

Accepted Solution

stefan73 earned 504 total points
ID: 13525237
Hi andyw27,
Functions can return values and have parameters. Example:

return input + 3;
}

This you can simply call as
int myvar = 5;

...and myvar will be 8.

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

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

...and this will be called as:

int myvar = 5;

...and myvar will be 8 again.

Cheers!

Stefan
0

LVL 12

Expert Comment

ID: 13525244
BTW: You can also nest function calls, such as:

return input + 3;
}

int myvar = 5;

...and myvar will be ((5+ ((5)+3))+3) = 16.
0

LVL 37

Assisted Solution

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

LVL 1

Expert Comment

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

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).
{
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 = 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

ID: 13548674

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

LVL 8

Expert Comment

ID: 13552531
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

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

ID: 13755149
A split ??
0

LVL 37

Expert Comment

ID: 13771320
I think all the answers deserve points :)

stefan73,
me,
ssnkumar,

Is that ok?
0

LVL 8

Expert Comment

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

## Featured Post

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
Course of the Month12 days, 20 hours left to enroll