5 Replies - 825 Views - Last Post: 15 October 2012 - 10:41 AM Rate Topic: -----

#1 bradleycmetz   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 17-September 12

Handling Functions

Posted 15 October 2012 - 08:04 AM

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.

#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:
int f(int a, int b, int c)
and here:
int a = 2, b = 3, c = 1;

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

Also I am at a complete loss when it comes to
int f(int a, int b, int c)
and 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.ed...s/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.

This post has been edited by bradleycmetz: 15 October 2012 - 08:05 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Handling Functions

#2 NathanMullenax   User is offline

  • D.I.C Head
  • member icon

Reputation: 103
  • View blog
  • Posts: 218
  • Joined: 23-September 12

Re: Handling Functions

Posted 15 October 2012 - 08:38 AM

The a, b, and c referred to in the function are different than the ones referred to in the scope of main.

When these function calls happen:
c = f(a+b, a+c, b+c);
b = f(a, b, c);
a = f( a, b, f(c, b, a) );


Each parameter is evaluated to an integer, and then the function is called. In the first example, something like this happens.
c = f(a+b,a+c,b+c);
// ->
c = f(2+3,2+1,3+1);
// ->
c = f(5,3,4);


Then when the function is called, the local variables (the formal parameters) in the scope of f are bound. a is 5, b is 3, and c is 4 for the duration of the function call and only in the function.

The notes on function calling from your class seem to cover this material in the discussion of formal parameters versus actual parameters.

http://www.cs.ucf.ed.../Functions1.doc

Another way of looking at it, if f was written this way, the code would still do the same thing.
int f(int x, int y, int z) {
  int sum;
  sum = x + y + z;
  if (sum < x*y)
    return x + y;
  if (sum <= 2*x*y)
    return y + z;
  return x + z;
}


Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#3 bradleycmetz   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 17-September 12

Re: Handling Functions

Posted 15 October 2012 - 09:23 AM

So for example I want to start by doing
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

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

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:
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..
Was This Post Helpful? 0
  • +
  • -

#4 mojo666   User is offline

  • D.I.C Addict
  • member icon

Reputation: 409
  • View blog
  • Posts: 885
  • Joined: 27-June 09

Re: Handling Functions

Posted 15 October 2012 - 09:47 AM

View Postbradleycmetz, on 15 October 2012 - 09:23 AM, said:

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:
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;


f(a+b, a+c, b+c); does some calculations and returns a value based on the values of a,b, and c (respectively 2,3,1).

c=f(a+b, a+c, b+c); does the same thing, but also stores the result of f in c overwritting the previous value, c now contains 8 instead of 1. So, every line after this in main will use 8 whenever c is referred to.

EDIT-
to tace it out
int a=2, b=3, c=1

c=f(a+b,a+c,b+c);
//becomes
c=f(5,3,4);
//becomes
c=8;
//so the next f will use the updated c value
b=f(a,b,c);
//becomes
b=f(2,3,8);


This post has been edited by mojo666: 15 October 2012 - 09:55 AM

Was This Post Helpful? 1
  • +
  • -

#5 NathanMullenax   User is offline

  • D.I.C Head
  • member icon

Reputation: 103
  • View blog
  • Posts: 218
  • Joined: 23-September 12

Re: Handling Functions

Posted 15 October 2012 - 10:40 AM

b = f(a, b, c);            // assigning a new value to b
c = f(a+b, a+c, b+c);      // c gets a new value 
a = f( a, b, f(c, b, a) ); // a gets a new value



The first one ultimately becomes:
b = 10;



After the right-hand side of an assignment expression is completely evaluated, the assignment is carried out. The fact that the value of (main's) b changes has to do with the assignment rather than the function call. Main's b exists until execution reaches the end of main, just as f's b exists until the end of an invocation of f.

This might seem absurd, but I think it's useful to look at what a compiler might generate for a simplified version of your program.

Consider:
int f(int a, int b, int c )
{
    return a + b + c;
}

int main()
{
    int x(1),y(2),z(3);
    x = f(x,y,z);       // x = f(x,y,z) -> x = 6
    y = f(z,z,z);       // y = f(z,z,z) -> y = 9
    return 0;
}



And its corresponding assembly on an x86:

f:
	pushl	%ebp                ;; save ebp
	movl	%esp, %ebp          ;; put stack pointer in ebp
	movl	12(%ebp), %eax      ;; eax = (param)
	movl	8(%ebp), %edx       ;; edx = (param)
	leal	(%edx,%eax), %eax   ;; eax = eax + edx
	addl	16(%ebp), %eax      ;; eax = eax + (param)
	popl	%ebp                ;; restore ebp
	ret                         ;; pop return address off of the stack
                                    ;; and go there.

_main:
LFB1:
	pushl	%ebp            ;; save ebp
LCFI3:
	movl	%esp, %ebp      ;; ebp = esp
LCFI4:
	andl	$-16, %esp      ;; make stack pointer word-aligned
LCFI5:
	subl	$32, %esp       ;; make room for main's variables
LCFI6:
	call	___main         ;; go initialize c++ runtime
                                ;; or something
	movl	$1, 28(%esp)    ;; 28(%esp) is where x is stored
	movl	$2, 24(%esp)    ;; 24(%esp) is y
	movl	$3, 20(%esp)    ;; 20(%esp) is z
	movl	20(%esp), %eax  ;; push value of z onto the stack
	movl	%eax, 8(%esp)   ;;
	movl	24(%esp), %eax  ;; push value of y onto the stack
	movl	%eax, 4(%esp)   ;;
	movl	28(%esp), %eax  ;; push value of x onto the stack
	movl	%eax, (%esp)    ;;
	call	f               ;; go compute f(1,2,3) and come back here
                                ;; 'call' pushes the current address onto
                                ;; the stack so f knows where to return.
	movl	%eax, 28(%esp)  ;; f returned a value in %eax, store it in 
                                ;; the memory location x
	movl	20(%esp), %eax  ;; push the value of z onto the stack 3 times
	movl	%eax, 8(%esp)   ;;
	movl	20(%esp), %eax  ;;
	movl	%eax, 4(%esp)   ;; 
	movl	20(%esp), %eax  ;;
	movl	%eax, (%esp)    ;;
	call	f               ;; invoke f
	movl	%eax, 24(%esp)  ;; save the value of f(z,z,z) value in location y
	movl	$0, %eax        ;; "return 0"
	leave                   ;; clean up the stack--this is where x, y, z disappear
LCFI7:
	ret                     ;; hand execution back to the OS



I think the important thing to note is that each function call has its own stack frame. That is, every time a function is invoked, it gets a chunk of memory to do its work. When it's done, it cleans up after itself and stores its return value in some location (here eax by convention). Function parameters are passed by value in this example, so there's no way a function can make any (binding) change to its arguments.
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Handling Functions

Posted 15 October 2012 - 10:41 AM

I suggest you study the links provided in my signature. They should help you better understand functions.

Jim
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1