Search This Blog

Thursday, 8 February 2018

Functions in C

1.    Why User Defined Function is needed?
2.      Definition
3.      Function in C Language
4.      Return Values and their Types
5.      Arguments and Return value combination.
6.      Scope and lifetime of variables in functions
7.      Storage Classes
8.      Nesting of Functions
9.      Recursion
10.  Functions  and Arrays
11.  Summary

1.      Why User Defined Function is needed?
        ·         The large programs are divided into functional parts, then each part may be coded independently and then combined into a single unit.
       ·         Some type of operations are repeatedly used throughout the program. Such part can be written as a function and called whenever required .
       ·         It saves space and time also.
       ·         It reduces the length of the source program.
       ·         Top-down modular programming is done. i.e high level logic of the problem is solved first then details of lower level function solved.
2.      Definition
       Function is a set of statements which perform particular task together.
3.      Function in C Language
        C functions are divided into two categories
1.      Library function
Library functions are predefined. They are not needed to write by user.
e.g. scanf(),prinf,exit,
2.      User-defined function
These are defined by user at the time of writing program.
Syntax:

4.      Return Values and their Types
5.      Arguments and Return value combination.
6.      Scope and lifetime of variables in functions
Definitions:
Scope : It is a parts of program where variable is available for use.


Lifetime: It is a period during which variable retains a given value during execution of program.
Categories of variable:
1.      Local / Internal variable
A variables which are declared within particular function or block of the program.





















2.      Global / external variable
    A variables which are declared outside of any function.

7.      Nesting of Functions
Nesting of function call is allowed in C Language.

e.g main function calls Function1(),Function1 calls Function2 and which in turns call another function such type of nesting is allowed
*Note: One cannot write function definition within another function definition 

                   Another example:
                                         function1(a, function2(p) );
8.      Recursion:
Definition:
 A Function which calls itself is known as recursion.
           e.g
int  Factorial(int n)
{
1.      if(n==1)
2.      return 1;
3.      else
4.      return(n*factorial(n-1));
}

Let us see detail execution of the recursion
In above function ,
n=5;
Now  value 1 is returned from factorial(1) function call is put into the previous function factorial(2) who calls factorial(1) and so on….
Let us see the sequence of execution .

Whole sequence of execution is as follows:
Red color -indicates statements are executed.
Blue color -indicates statements are in execution phase but not completed due to recursive call.
Black solid arrow-show the transfer of control because of recursive call.
Red dotted arrow-shows returning of control after recursive function call finishes.




9.      Functions  and Arrays
How to pass array to a function?
In Calling function: Pass an array name without subscript and size of an array
e.g            maximum(a,n);
                        Where a is an array name and n is the size of array.
In Function Definition:
Sample Program:

10.  Summary
·         Function is used to modularize the program.
·         Function can return only one value.
·         Function may or may not return a value.
·         Function return integer value by default.
·         Actual and formal parameter must be same in data type.
·         Function with return value must return some value with specified data type.
·         Function can have more than one return statements.
·         A Return statement can be written anywhere within function.
·         A function can be written before or after main function.
·         Function can not be defined inside another function.
·         A variable declared inside function is local to that function .It is destroyed when function is exited.




Next    Prev

No comments:

Post a Comment