d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Handling Functions > C
Add Reply New Topic New Poll
Member
Posts: 3,706
Joined: Mar 8 2009
Gold: 17,175.10
Oct 15 2012 09:11am
I'm given code to trace and refine my skills dealing with functions and manipulating them, however I cannot find an example solution in the notes, only how to write and call functions. I will post the code and hte questions I have regarding it. Maybe someone could point me to material deep within the webz that covers this topic. I'm open to any and all materials.

Code
#include <stdio.h>
int f(int a, int b, int c);

int main() {

 int a = 2, b = 3, c = 1;

 c = f(a+b, a+c, b+c);
 printf("a=%d b=%d c=%d\n", a, b, c);

 b = f(a, b, c);
 printf("a=%d b=%d c=%d\n", a, b, c);

 a = f( a, b, f(c, b, a) );
 printf("a=%d b=%d c=%d\n", a, b, c);
 
 system("PAUSE");
 return 0;
}

int f(int a, int b, int c) {

 int sum;
 sum = a + b + c;
 if (sum < a*B)
   return a + b;
 if (sum <= 2*a*B)
   return b + c;

 return a + c;
}


Specifically I do not know where to start. Obviously variables are declared here:
Code
int f(int a, int b, int c)

and here:
Code
int a = 2, b = 3, c = 1;


but I do not know how to handle
Code
c = f(a+b, a+c, b+c);

Code
b = f(a, b, c);

Code
a = f( a, b, f(c, b, a) );


Also I am at a complete loss when it comes to
Code
int f(int a, int b, int c)

and
Code
return a + c;


I know I am missing the background for functions but I just cannot find suitable material in his notes.
I will even provide the link to all of his lecture notes, maybe it is just buried somewhere in there. http://www.cs.ucf.edu/~dmarino/ucf/cop3223/lectures/indexF08.html
I apologize for all the questions, but I have hit a road block, and I need someone to point me to or provide some sort of study material, or examples.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Oct 15 2012 09:50am
Int f(int a, int b, intc) this not only declares functions, but also says you are passing it arguments.

So c=f(a+b, a+c, b+c) will pass the function f three values.

In the function f, a will now be a+b and so on.

Functions can return values as well. You can tell what they return by how they were defined.

Ie, int f or string s etc. yours is int, so it should return an int.

Also, you have some constants in your code that were never defined (B)

This post was edited by Eep on Oct 15 2012 09:51am
Member
Posts: 3,706
Joined: Mar 8 2009
Gold: 17,175.10
Oct 15 2012 10:20am
Quote (Eep @ Oct 15 2012 07:50am)
Int f(int a, int b, intc) this not only declares functions, but also says you are passing it arguments.

So c=f(a+b, a+c, b+c) will pass the function f three values.

In the function f, a will now be a+b and so on.

Functions can return values as well. You can tell what they return by how they were defined.

Ie, int f or string s etc. yours is int, so it should return an int.

Also, you have some constants in your code that were never defined (B)



Not sure how they went from lower to upper but that happened while copying.
So for example I want to start by doing
Code

c = f(a+b, a+c, b+c);
c = f(5,3,4);
int f(int a, int b, int c) (with a=5,b=3,c=4)
sum = 12;
sum < a*b;
return a+b(8) to c value
a=2, b=3, c=8.

I got this part to print correctly

Now I do

Code
b= f(a,b,c);
b= f(2,3,8);
int f(int a, int b, int c) (with a=2,b=3,c=8)
sum 13
sum !< a*b
sum !< 2*a*b
return a+c(10) to b value
a=2,b=10,c=8


Finally

Code
a = f( a, b, f(c, b, a) );
a = f( 2, 10, f(8, 10, 2) );
   f(8,10,2)
   sum= 20
   20 < 80
   return a+b(18) into f(c)
a = f(2,10,18)
sum = 30
sum !< 20
sum < 2*20
return b+c(28) into a value
a=28,b=10,c=18


Now that I know the functions retain their values, I got the correct output, it seems. What makes it so that the values are retained? Previously I thought that after every evaluation of these functions:
Code
b = f(a, b, c);
c = f(a+b, a+c, b+c);
a = f( a, b, f(c, b, a) );


the variables in the next f() -a,b,c would get their values from:
int a = 2, b = 3, c = 1;

your explanation was very helpful though. I'm missing some background information on functions clearly- but that was a big step forward..

This post was edited by TheDiscoveryChannel on Oct 15 2012 10:22am
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Oct 15 2012 11:27am
remember functions in math class? it works exactly the same way here.

for example, if I said:
f(x) = 2x

then i said
c = 2
c = f(c)

followed by
c = f(c+3)

that means
c = f(c) --> c = f(2) --> c = 4
c = f(c+3) --> c = f(4+3) --> c = 14
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Oct 15 2012 01:37pm
After calling a function, the function retains those values but as soon as you pass it new args (AKA call it again) then the values it had before are obviously replaced. So you don't have to worry about that.

Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Oct 15 2012 07:08pm
easy answer: (read this)

functions do not retain their values after you call them and return from them. the reason being is the way assembly and the cpu works in general. basically when you pass variables to a function they are only defined to those integers until the function is returned and then they are removed. what you are doing is simply storing the functions returning integer into a local variable in the scope of main(). by doing this you are retaining those numbers returned by the function.


technical stuff: (dont read unless you want a headache)

in low level asm this is known as the stack frame.

each time you call a function you push your variables in reverse order onto the stack and then you call your function (the call function itself does two things. push the current memory address of the next line of code to run and initiate a JMP to the address if the function). when this is done the function will have its own stack frame consisting of the three variables that were pushed to the stack before the call. once the function is returned via the ret command (which pops the return address off the stack and initiates a JMP back to the original code flow) all registers are cleared besides %eax which holds the return value and normally the stack frame for the current function being called is also cleaned my popping off the values you passed to the function.
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Oct 15 2012 11:30pm
which doesn't really matter. all that is just how the language/machine goes about achieving the desired behavior of functions.

all you need to really know is that functions work just like they do in mathematics --- because that's precisely what they are.

when you calculate f(x) = some equation for some value of x, the function doesn't behave differently just because you used it to calculate some other values of x.

(besides, the above may only be true for certain architectures, i.e. x86. it could be done completely differently depending on the cpu architecture)

This post was edited by irimi on Oct 15 2012 11:31pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Oct 15 2012 11:58pm
Quote (irimi @ Oct 16 2012 01:30am)
which doesn't really matter.  all that is just how the language/machine goes about achieving the desired behavior of functions.

all you need to really know is that functions work just like they do in mathematics --- because that's precisely what they are.

when you calculate  f(x) = some equation for some value of x, the function doesn't behave differently just because you used it to calculate some other values of x.

(besides, the above may only be true for certain architectures, i.e. x86.  it could be done completely differently depending on the cpu architecture)


this
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll