More on Functions, Scope Rules, Math Library Functions


Lecture 10

More on Functions

Let’s see the last program from our last lecture-
#include<stdio.h>
#include<conio.h>

int maximum(int,int,int); // function prototype

main(){
      int x,y,z;
      printf(“Enter three integers: ”);
      scanf(“%d %d %d”,&x,&y,&z);
      printf (“The maximum is %d”,maximum(x,y,z));
}

//function definition

int maximum(int a,int b, int c){
      int max=a;
      if(b>max)
            max=b;
      if(c>max)
            max=c;
      return max;
}


The variables x, y and z are called ‘actual arguments’, whereas the variables a, b and c are called ‘formal arguments’. Any number of arguments can be passed to a function being called. However, the type, order and number of the actual and formal arguments must always be same.

Instead of using different variable names x, y and z, we could have used the same variable names a, b and c. But the compiler would still treat them as different variables since they are in different functions.

There is no restriction on the number of return statements that may be present in a function. Also, the return statement need not always be present at the end of the called function. The following program illustrates these facts.


All the following are valid return statements.
return ( a ) ;
return ( 23 ) ;
return ( 12.34 ) ;
return ;

In the last statement a garbage value is returned to the calling function since we are not returning any specific value. Note that in this case the parentheses after return are dropped.

A function can return only one value at a time. Thus, the following statements are invalid.
return ( a, b ) ;
return ( x, 12 ) ;

If the value of a formal argument is changed in the called function, the corresponding change does not take place in the calling function. For example,
The output of the above program would be:
60
30

Thus, even though the value of b is changed in fun( ), the value of a in main( ) remains unchanged. This means that when values are passed to a called function the values present in actual arguments are not physically moved to the formal arguments; just a photocopy of values in actual argument is made into formal arguments.

Calling Conventions

Calling convention indicates the order in which arguments are passed to a function when a function call is encountered. There are two possibilities here:
  1. Arguments might be passed from left to right.
  2. Arguments might be passed from right to left.

C language follows the second order.

Consider the following function call:
fun (a, b, c, d ) ;
In this call it doesn’t matter whether the arguments are passed from left to right or from right to left. However, in some function call the order of passing arguments becomes an important consideration. For example:

int a = 1 ;
printf ( "%d %d %d", a, ++a, a++ ) ;

It appears that this printf( ) would output 1 2 2.  This however is not the case. Surprisingly, it outputs 3 3 1.

Scope Rules

The scope of an identifier is the portion of the program in which an identifier is referenced. When we declare a local variable in a block, it can be referenced only in that block or in blocks nested within that block. The four scopes for an identifier are-
1.    Function scope
2.    File scope
3.    Block scope
4.    Function prototype scope

Labels are only identifiers with function scope. Label is an identifier with name followed by a colon. Often used with goto statement. Though, use of goto statements is discouraged, we are taking a look at a program that uses goto and a label
The sample output of the program is as follows-


An identifier declared outside any function has file scope. Such an identifier is known to all functions from the point it is declared until the end of the file. Global variables, function definitions, function prototypes placed outside a function all have file scope.

Identifiers declared inside a block have block scope. Block scope ends at the terminating right brace (}) of the block. Local variables declared at the beginning of a function have block scope. Another example is function parameters. When blocks are nested, and an identifier in an outer block has the same name as an identifier in the inner block, the identifier in the outer block is hidden until the inner block terminates.

Identifiers with function prototype scope are those used in the parameter list of a function prototype.

#include <stdio.h>
#include <conio.h>

void a ();
void b();
void c();

int x=1; // Global Variable

void main(){
      int x=5;
      printf (“Local x in outer scope of main is %d”, x);
            {
            int x=7;
            printf (“Local x in inner scope of main is %d”, x);
            }
      printf (“Local x in outer scope of main is %d”, x);
      a();
      b();
      c();
      a();
      b();
      c();
      printf (“Local x in main is %d”, x);
      getch();
}

void a(){
      int x=25;
      printf (“Local x in a after entering is %d”,x);
      x++;
      printf (“Local x in a before exiting is %d”,x);
}

void b(){
      static int x=50;
      printf (“Local static x in b after entering is %d”,x);
      x++;
      printf (“Local static x in b before exiting is %d”,x);
}

void c(){
      printf (“Global x in c after entering is %d”,x);
      x++;
      printf (“ Global x in c before exiting is %d”,x);
}

The output of the program is as follows-

Local x in outer scope of main is 5
Local x in inner scope of main is 7
Local x in outer scope of main is 5

Local x in a after entering is 25
Local x in a before exiting is 26

Local static x in b after entering is 50
Local static x in b before exiting is 51

Global x in c after entering is 1
Global x in c before exiting is 2

Local x in a after entering is 25
Local x in a before exiting is 26

Local static x in b after entering is 51
Local static x in b before exiting is 52

Global x in c after entering is 2
Global x in c before exiting is 3

Math Library Functions

Math library functions allow the programmer to perform certain common mathematical calculations. We will discuss many of these math library functions in this lecture.

A programmer desiring to calculate and print the square root of 900.0 can write-

printf(“%.2f”, sqrt (900.0));

When the statement is executed, sqrt() is called to calculate the square root of the number contained in the parenthesis (900.0). The number 900.0 is the argument of sqrt() function. The statement will print 30.00. sqrt() function takes an argument of type double and returns a result of type double.
Note- All math library functions return the data type double.

printf(“%.2f”, sqrt (c1+d*f));

If c1=13.0, d=3.0 and f=4.0, then the line would print 5.00.

Some other useful math library functions-

Function

Description

exp(x)
Exponential function
log(x)
Natural logarithm (base e)
log10(x)
Logarithm (base 10)
fabs(x)
Absolute value
ceil(x)
Rounds x to the smallest integer not less than x
floor(x)
Rounds x to the largest integer not greater than x
pow(x,y)
x to the power y
fmod(x,y)
Remainder of x/y as a floating point number
sin(x)
Trigonometric sine of x
cos(x)
Trigonometric cosine of x
tan(x)
Trigonometric tangent of x

1 comments:

Unknown said...

This is great blog! Keep it up. chemical wash

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by CelebrityDisk | Written by Alamin - link | Grants For Single Moms