ASIC Verification: C Functions

Saturday, February 23, 2008

C Functions

Now a days we rely on many persons in the life for so many things. You may call a mechanic to repair your car or rely on a store to supply you groceries every month. A computer program finds itself in a similar situation. It can't handle all the tasks on its own.

Function

A function is a block of statements that perform a coherent task of some kind. Every "C" program can be thought of as a collection of these functions. C functions are the equivalent of what in other languages would be called subroutines or procedures.

A function is simply a block of statements that you have grouped together and given a name. The importance of doing this is, that you can use that "chunk" of code repeatedly simply by writing its name. For example, if you want to create a function that prints the word "Hello C world" on the screen and adds one to variable called total then the chunk of C code that you want to turn into a function is just:
===================================================================

printf ("Hello C world");
total=total+1;

===================================================================

To turn it into a function you simply wrap the code in a pair of curly brackets to convert it into a single compound statement and write the name that you want to give it in front of the brackets:

===================================================================

demo()
{
printf("Hello C world");
total = total + 1;
}

===================================================================

Now we need to call this function demo() in main(). What do we mean when we say that the main() calls the function demo()? We mean that the control passes to the function demo(). The activity of main() is temporarily suspended; it falls asleep while the demo() function wakes up and goes to work. When the demo() function runs out of the statements to execute, the control returns to main(), which comes to life again and starts executing the code at the exact point where it left off.
===================================================================

main()
{
demo();
}
===================================================================

Functions and local variable

If you look at the main() program we have been using, you will notice that, it is in fact a function that just happens to be called "main"! So to make demo work we have to add the declaration of the variable total:
===================================================================

demo()
{
int total;
printf("Hello C world");
total = total + 1;
}
===================================================================

"total" is a variable that belongs to the demo() function. It cannot be used in other functions, it doesn't even exist in other functions and it certainly has nothing to do with any variable of the same name that you declare within other functions.

Here is the summary of Functions that we have learnt so far.

  • C program is collection of one or more functions.
  • A function gets called when the function name is followed by a semicolon.
  • A function is defined when a function name is followed by a pair of braces.
  • Any function can be called from any other function.
  • A function can be called any number of times.
  • A function can call itself, called "recursive" function.
  • A function can't be defined in another function.
Passing values between functions

It would be nice to have a little more control over what the functions do. We need to communicate b/w "calling" and "called" functions. Consider the following "C" code.
===================================================================
main()
{
int a, b, c, sum;
printf("Enter any 3 nos:\n");
scanf("%d%d%d", &a, &b, &c);
sum = calcsum (a,b,c);
printf("Sum = %d\n", sum);
}

calcsum (int x, int y, int z)

{
int d;
d= x+y+z;
return(d);
}
===================================================================
And here is the output :

Enter any 3 nos:
10 20 30
Sum = 60

Advanced Features of Functions

Function declaration & Prototypes : Whenever a call is made to a function, the compiler assumes that this function would return a value of the type "int", then it is necessary to implicitly mention 'so' in the calling as well as in the called function. Consider the program:
===================================================================
main()
{
float a, b;
printf("Enter any no:\n");
scanf("%f", &a);
b = square (a);
printf("Square of %f is %f \n", a, b);
}

square (float x)
{
float y;
y= x*x;
return(y);
}
===================================================================
and here are the 3 sample runs of this program:

Enter any no:
3
Square of 3 is 9.00000
Enter any no:
1.5
Square of 1.5 is 2.0000.....(This is because any C function by default, always return an "int" value) . square is not capable of returning a "float" value. How do we overcome this? The function square() must be declared as float in main() as well. float square() means that this is a function which will return a float value.


Call by value & call by reference

If you observe carefully, whenever we called a function and passing something to it we have always passed the 'values' of variables to the called function -- Call by value.
If we pass the address of the variable to a function, then that function is called Call by reference.
For this we need to address some of the basics of Pointers.

swapping the two variables -- Call by value
===================================================================
main()
{
int a = 10, b=20;
swap(a,b);
printf("a = %d\n, b = %d\n", a, b);
}
swap(int x, int y)

{
int t;
t=x; x=y; y=t;
printf("x=%d\n, y=%d\n", x,y);
}
===================================================================
output : x=20 y=10
a=10 b=20

swapping the two variables -- Call by ref
===================================================================
main()
{
int a = 10, b=20;
swap(&a,&b);
printf("a = %d\n, b = %d\n", a, b);
}

swap(int *x, int *y)
{
int t;
t=*x; *x=*y; *y=t;
printf("x=%d\n, y=%d\n", x,y);
}
===================================================================
output : x=20 y=10
a=10 b=20

Recursion

A function is called recursive if a statement within the body of a function calls the same function.
Here is a program to calculate the factorial value using recursive function.
===================================================================
main()
{
int a, fact;
printf("Enter any number\n");
scanf("%d", &a);
fact=rec(a);
printf("Factorial vaue = %d\n", fact);
}
rec(int x)

{
int f;

if(x==1)
return (1);

else
f =x*rec(x-1);

return(f);

}
===================================================================
consider when the number entered through scanf() is 2, we reach the statement f=x*rec(x-1); How do we handle the expression x*rec(x-1)? we multiply x by rec(x-1). Since the current value of x is 2, it is same as (2*rec(1)). Thus the statement x*rec(x-1) evaluates t0 2, which is stored in the variable f and is returned to main().

Qustions
===================================================================
What is the purpose of main( ) function?
Write a code for swapping of two numbers without using temporary variable.
Is using exit() the same as using return?
Is it possible to execute code even after the program exits the main() function?
When function say abc() calls another function say xyz(), what happens in stack?
Is it better to use a macro or a function?
Why should I prototype a function?
What is a function and built-in function?
What is an argument ? differentiate between formal arguments and actual arguments?
What are the advantages of the functions?
What is the output of printf("%d")?
What is the difference b/w calloc() and malloc()?
===================================================================
Reference

"Let Us C" by Yashavant Kanethar.

No comments: