Sunday, November 16, 2014

3.2.1 FUNCTIONS - INTRODUCTION


§  C functions can be classified into two categories, namely library functions and user-defined functions.

§  printf() and scanf() belong to the category of library functions and their definition is written in <stdio.h> header file. Similarly, sqrt(),cos(), strcat(),, etc are also library functions.

§  It is difficult to implement a large program even if its algorithm is available.

To implement such a program with ease, it should be split into a number of

            independent tasks, which can be easily designed, implemented and managed.

 

This process of splitting a large program into small manageable tasks and designing them independently is popularly called modular programming.

 

This approach results in a number of advantages:

1)      The high level logic of the overall problem is solved first while the details of each lower-level function are addressed later.

2)      The length of a source program can be reduced by using functions at appropriate places. This factor is particularly critical with microcomputers where memory space is limited.

3)      It is easy to locate and isolate a faulty function for further investigations.

4)      A function may be used by many other programs. This means that a C programmer can build on what others have already done, instead of starting all over again from scratch. 


 

               Fig 17: Modular Programming
 

User-defined Functions

Def:

A function is a self-contained block of program statements that perform a particular task.

 

Why are functions needed?

The use of functions provides several benefits. It makes programs significantly easier to understand and maintain by breaking them up into easily manageable chunks.

 

Advantages:

·         Code reuse: The main() function can consist of a series of function calls rather than countless lines of code.

·         Flexible debugging: Functions make the code shorter and more readable i.e. making it less likely to have mistakes.

·         Code sharing: A well written function may be reused in multiple programs.

·         Data protection: Functions can be used to protect data. This is related with the concept of local data. The local data is available only within a function when the function is being executed.

 

Elements of User-defined Functions

§  In order to make use of o user-defined function we need to establish three elements that are related to functions - Function Definition, Function Call, and Function Declaration

§  The function definition is an independent program module that is specially written to implement the requirements of the function.

§  In order to use the function we need to invoke it at a required place in the program. This is known as the function call.

§  The program (or a function) that calls the function is referred to as the calling program or calling function. The calling program should declare any function that is to be used later in the program. This is known as the function declaration or function prototype.

 

WAP to find the greatest among two numbers using a user-defined function

#include<stdio.h>

#include<conio.h>

int max(int x, int y);                            //Function declaration

void main()

{

   int a,b,c;

   printf("Enter two numbers: ");

   scanf("%d%d",&a,&b);

   c=max(a,b);                                      //Function call

   printf("\nGreatest Number=%d",c);

   getch();

}

 

int max(int x,int y)                              //Function definition

{

  if(x>y)

    return x;

  else

  return y;

}

 

OUTPUT

Enter two numbers: 23

76

 

Greatest Number=76

 

Function Definition

A general format of a function definition is given below:

            function_type   function_name(parameter list)

{

            local variable declaration;

            executable statement1;

            executable statement2;

            …..

            return statement;

}

where

ü  function_type specifies the type of value (like int or float ) that the function is expected to return to the program calling the function.

ü  function_name is any valid C identifier and therefore must follow the same rules of formation as other variable names in C.

ü  parameter list declares the variables that will receive the data sent by the calling program. They serve as input data to the function to carry out the specified task.

ü  executable statements perform the task of the function.

ü  return statement  returns the value evaluated by the function.

 

Function Call

A function can be called by simply using the function name followed by a list of actual parameters, if any, enclosed in parentheses.

e.g. 


 
In the above given example, the function computes the product x and y, assigns the result to the local variable p, and then returns the value 50 to the main() where it is assigned to y again.

 

Function Declaration

Like variables, all functions in a C program must be declared, before they are invoked. The general format is as given below:

            function_type   function_name (parameter list);

 

NOTE:

Parameters (also known as, arguments) are used in three places:

1.      in declaration (prototype),

2.      in function call, and

3.      in function definition

 

The parameters used in declaration (prototypes) and function definitions are called formal parameters and those used in function calls are called actual parameters.

 

The formal and actual parameters must match exactly in type, order and number. Their names however, do not need to match.

 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.