Thursday, 4 April 2019

Functions in C (Part 1-Introduction)

C programming
 Introduction to Functions
A function is an independent module or block of program code which deals with a particular task. A function can be called or used anywhere in the program by calling the name.
Each function has a name or identifier by which is used to refer to it in a program. A function can accept a number of parameters or values which pass information from outside, and consists of a number of statements and declarations, enclosed by curly braces { }, which make up the doing part of the object.
The main is a special function called at the beginning of the program. All other functions are directly or indirectly called from main. That’s why main function is called driver function.

Reasons for using functions
There are many reasons for using functions.
(i).Reusability: A function can be reused in many different contexts without repeating parts of the program text.
(ii).Manageability: Using functions, program will be divided to separate blocks. Each block will do a specific job Designing, understanding and managing smaller blocks of code is easier.
(iii).Readability : Since each function has few lines of code which is easy to read and understand.

Parts of a function
(i).Function prototype
(ii).Function Definition
(iii).Function Calling

(i).Function Prototype / Function Declaration
If we want to use a function before we define it, we must declare it just like a variable to inform the compiler about the function. This declaration is called the function prototype.
The point at which a name has a type associated with it. No storage is reserved at this point.

Syntax:
returnType functionName(dataType var1, dataType var2,……,dataType varN);

 The variable names are not required when declaring a function prototype.

returnType functionName(dataType, dataType,……,dataType);

Examples:
          int square(int number);
          void display(void);


(ii).Function Definition
The block of code between braces following the function header is called the function body.
The function header defines the name of the function, the function parameters (which specify the number and types of values that are passed to the function when it’s called), and the type for the value that the function returns.
The function body contains the statements that are executed when the function is called, and these have access to any values that are passed as arguments to the function.

The general form of a function looks like this:
Return_type Function_name( Parameters - separated by commas )
{
// Statements...
}
Also a declaration, but at this point some storage is reserved for the named object.
The arguments used in the function definition are called formal parameters.
Example:
int square(int num){
   int result;
   result=num*num;

   return result;
}
(iii).Calling a function
A function is called (i.e. control is passed to the function) by using its name with the usual brackets () to follow it, along with the values which are to be passed to the function.
When a function is called, the statements within the body of that function are executed, and when the function has finished executing, control returns to the point at which that function was called.
    Syntax:
                   functionName(parameters);
The  arguments used when the function is actually called are called actual parameters. In other words, the values that the formal parameters will have on entry to the function.

Example:
   int res=square(number);
Complete example
#include<stdio.h>
 //function prototype
int square(int number);

int main(){
  int number,res;
  printf("Enter an integer:\t");
  scanf("%d",&number);
  //functional calling
  res=square(number); 
  printf("\nSquare of %d is %d.\n",number,res);
  return 0;
}

//function definition
int square(int num){
   int result;
   result=num*num;
   return result;
}

How function works?
Program execution start from main function.  Statements are executed in sequence in the normal way until a function call is found.
When function call is found, control passes to function definition along with parameter values and statement with in the called function are executed.
Execution of the program continues through the function statements until it encounters a return statement or reaches the closing brace marking the end of the function body. The return statement of closing brace ‘}’ indicates the end of function and control passes to the point immediately after the point the function was originally called.

A Function can different forms:-
Type 1: No return type, no argument
   void functionName(void){
          //function body
      }

Type 2 : return type, no argument
   dataType functionName(void){
          //function body
        return variable;
      }

Type 3 : return type, with argument list
dataType functionName(argument list){
          //function body
        return variable;
      }

Please comment and share if you find it useful.

No comments:

Post a Comment