Thursday, 20 June 2013

functions i C language

 FUNCTIONS
A program can be used for solving a simple problem or a large and complex problem. Programs solving simple problems are easier to understand and identify mistakes, if any, in them. Complex programs are usually larger. If a program is large, it is difficult to understand the steps involved in it. Hence, it is subdivided into a number of smaller programs called subprograms or modules. Each subprogram specifies one or more actions to be performed for the larger program. Such programs are called as functions/subroutines.
DEFINITION:
A function contains sequence of instructions to perform any task, which is used (or invoked) by the main program or other subprograms. A subprogram receives values called arguments from a calling program, performs calculations and returns the results to the calling program.
Advantages
1. The length of the source program can be reduced by using functions at appropriate places.
2. A function can be used to avoid rewriting the same sequence of code at two or more locations in a program.
3. If the program is large, then it is divided into subprograms, each subprogram can be written by as a function.
4. Programs with using functions are compact & easy to understand.
TYPES OF FUNCTIONS
Based on the nature of creation, functions are classified as
1) Built-in functions
            Built-in functions are predetermined and supplied along with the compiler and these can be used in any C program. These are also known as library functions.
2) User-defined functions
The functions defined by the users are called as user-defined functions.
  To make use of the user defined functions, the programmer must be able to
Define a function
Declare the prototype of a function and
Invoke/call the function
 FUNCTION DEFINITION
  A function definition describes what a function does, that is a function definition specifies the operation of the function. The general format for defining a function is
 Return-type function name (parameter list)
  {
  Declaration(s);
  Statement(s);
               Return (expression);
  }
Example :
mul (int a,int b)
{
int y;
y=a+b;
Return y;
}

When the value of y which is the addition of the values of a and b. the last two statements
ie, y=a+b; and return y; can be combined as return(a+b) or return a+b;

FUNCTION DECLARATION
 A function declaration is made by declaring the return type of the function, name of the function and the data types of the arguments of the function.  A function declaration is same as the declaration of the variable. The function declaration is always terminated by the semicolon.
Before function calling, we should declare that function. Function call may come before the main() or before clrscr().
The general form of the declaration is:-
return_type function_name(parameter list);
For example function declaration can be
            int factorial(int n1,float j1);
 Another method can be
      int factorial(int , float);
 The variables in the function declaration can be optional but data types are necessary.
Formal parameters
These are the names used inside a function to refer to its arguments.
Actual arguments
These are the values used as arguments when the function is actually called. In other
words, the values that the formal parameters will have on entry to the function.
Rules:
The sequence of the arguments in the function call should be same as the sequence of the parameters in the parameter list of the function declaration. The data types of the arguments should correspond with the data types of the parameters. When a function call is made the value of the actual arguments will replace the formal parameters of the function.

FUNCTION PROTOTYPES

A function prototype is a function declaration or definition which includes information about the number and types of the arguments that the function takes.

THE RETURN STATEMENT AND RETURN VALUES
A return statement is used to exit from the function where it is. It returns the execution of the program to the point where the function call was made. It returns a value to the calling function. The general form of the return statement is:-
             return expression;
 The expression evaluates to a value which has type same as the return type specified in the function declaration.
TYPES OF FUNCTIONS
1. Functions with no arguments and no return values.
2. Functions with arguments and no return values.
3. Functions with arguments and return values.
4. Functions with no arguments and return values.

1. FUNCTIONS WITH NO ARGUMENTS AND NO RETURN VALUE.
A C function without any arguments means you cannot pass data (values like int, char etc) to the called function. Similarly, function with no return type does not pass back data to the calling function. It is one of the simplest types of function in C.
Example:
1. #include<stdio.h>
2. #include<conio.h>
3. void printline()
4. {
5. int i;
6. printf("\n");
7. for(i=0;i<30;i++)
8. {
9. printf("-");
10. }
11. printf("\n");
12. }
13. void main()
14. {
15. clrscr();
16. printf("Welcome to function in C");
17. printline();
18. printf("Function easy to learn.");
19. printline();
20. getch();
21. }
Source Code Explanation:
Line 3-12: This block is a user defined function (UDF) whose task is to print a horizontal line. In line no. 7 I have declared a “for loop” which loops 30 time and prints “-” symbol continuously.
Line 13-21: These line are “main()”  function. Line no. 16 and 18 simply prints two different messages. And line no. 17 and 19 calls our user defined function “printline()”.
FUNCTIONS WITH ARGUMENTS AND NO RETURN VALUE.
This type of function can accept data from calling function. In other words, you send data to the called function from calling function but you cannot send result data back to the calling function. Rather, it displays the result on the terminal.
Example:
1. #include<stdio.h>
2. #include<conio.h>
3. void add(int x, int y)
4. {
5. int result;
6. result = x+y;
7. printf("Sum of %d and %d is %d.\n\n",x,y,result);
8. }
9. void main()
10. {
11. clrscr();
12. add(30,15);
13. add(63,49);
14. add(952,321);
15. getch();
16. }

.
Source Code Explanation:
This program simply sends two integer arguments to the UDF “add()” which, further, calculates its sum and stores in another variable and then prints that value.
Line 3-8: This block is “add()” which accepts two integer type arguments. This UDF also has a integer variable “result” which stores the sum of values passed by calling function (in this example “main()”). And line no. 7 simply prints the result along with argument variable values.
Line 9-16: This block is a “main()” function. In 12, 13, 14 lines we have called same function “add()” three times but with different values and each function call gives different output.
FUNCTIONS WITH ARGUMENTS AND RETURN VALUE.
This type of function can send arguments (data) from the calling function to the called function and wait for the result to be returned back from the called function back to the calling function. The data returned by the function can be used later in our program for further calculations.
1. #include<stdio.h>
2. #include<conio.h>
3. int add(int x, int y)
4. {
5. int result;
6. result = x+y;
7. return(result);
8. }
9. void main()
10. {
11. int z;
12. clrscr();
13. z = add(952,321);
14. printf("Result %d.\n\n",add(30,55));
15. printf("Result %d.\n\n",z);
16. getch();
17. }
Source Code Explanation:
This program sends two integer values (x and y) to the UDF “add()”, “add()” function adds these two values and sends back the result to the calling function (in this program to “main()” function). Later result is printed on the terminal.
Line No. 3-8: Look line no. 3 carefully, it starts with int. This int is the return type of the function, means it can only return integer type data to the calling function. If you want any function to return character values then you must change this to char type. On line no. 7 you can see return statement, return is a keyword and in bracket we can give values which we want to return.
Line No. 9-17: In this block, we have declared an integer “z” which we used in line no. 13. Why we are using integer variable “z” here? Bec UDF “add()” returns an integer value on calling. To store that value we have declared an integer value. We have passed 952, 321 to the “add()” function, which finally return 1273 as result. This value will be stored in “z” integer variable. Now we can use “z” to print its value or to other function.
Line no. 14 and 15 does the same job. In line no. 15 we have used an extra variable whereas on line no. 14 we directly printed the value without using any extra variable. This was simply to show you how we can use function in different ways.
FUNCTIONS WITH NO ARGUMENTS BUT RETURNS VALUE.
We may need a function which does not take any argument but only returns values to the calling function then this type of function is useful
1. #include<stdio.h>
2. #include<conio.h>
3. int send()
4. {
5. int no1;
6. printf("Enter a no : ");
7. scanf("%d",&no1);
8. return(no1);
9. }
10. void main()
11. {
12. int z;
13. clrscr();
14. z = send();
15. printf("\nYou entered : %d.", z);
16. getch();
17. }





No comments:

Post a Comment