Posted on 5th September 2009One Response
What is Functions in Programming Language

FUNCTION

C program is divided into units called FUNCTIONS. C-Language has only one function main () irrespective of number of functions in a program. The operating system always passes control to main () when a C program is run. The function name is always followed by parentheses, i.e. “ () “which may or may not be empty.

What is Function:-

A function is a self contained program segment carries out a specific, well-defined task.
In other words a function is a set of one or more program statement that can be executed by referring to the function name.
Actually, we will be looking at two things a function that calls or activates the function and the function itself.

Main ()
{
Message ();
Print f (“\n cry, and you stop the monotony!”);
}
Message ()
{
Print f (“\n smile, and the world smiles with you…”);
}

Here, main () itself is a function and through it we are calling the function message ()
What do we mean when say that main () ‘calls’ the function message ()? We mean that the control passes to the function message (). The activity of main () is temporarily suspended; it falls asleep while the message () function wakes up and goes to work , when the message () function run out of statement to execute, the control returns to main () becomes to exact point where it left off. Thus main () becomes the calling function, where as message () becomes the called function.

Use of Function:-

Functions are generally used in C as abbreviation for a series of instruction that are to be executed more a once. This is not the same as loops because loop can repeat a series of instruction only if each iteration immediately follows the previous one. Whereas calling a function causes a series of instruction to be repeated at any given point within the program.
We use function to divide the main program in small parts and these small parts or program are called functions.
By parts we can easily execute the program we can easily maintain it, easily run it and it take lesser time as compare to main program.
Writing functions avoids rewriting the same code over and over.
Using functions it becomes easier to write program and keep track of what they are doing.
Separating the code into modular functions also makes the program easier to design and understand.

Features of Function

(a) Function declaration
(b)Function prototypes
(c) Calling functions by values or reference
(d) Recursion

Function Declaration:- A function is to be declared in main (), before it is defined or used. This has to be done in case the function is called before it is defined. However, it is not necessary if the function is returning an in type of value.
Example:
Main ()
{
.
.
Sum 1 ()
.
}
Sum 2 ()
{
.
.}

(b) Function Prototypes:- A function prototype is a function declaration that specifies the data types of the arguments. The traditional way to declare a function is by specifying the type of value to be returned by the function, and the function name. A function abc () having two arguments “x” and ”y” of type int, and returning a char type,

Can be declared as:
Char abs ();
Or
Char abs (int x, int y)

(c) The later definition is called the function prototype.

Calling Function

1. Call by reference
2. Call by value

(1) Call by value:- Whenever we call the function and pass the value of the variable to call the function. So the function s called as “Call by value”.

Main ()
{
int a=10,b=20;
Change (a, b);
Print f (“\n a=%d b=%d”, a ,b);
}
Change (int x , int y)
{
int t;
T=x;
X=y;
Y=t;
Print f (“\n x=%d y=%d”, x , y);
}
(2) Call by reference:- In call by reference, instead of passing the value if we pass the address of the variable so it is called the “call by reference”.

Example:
Main ()
{
int a=10,b=20;
Change (&a , &b);
Print f (“\n a=%d b=%d”, a , b);
}
Change (int x, int y)
{
int t;
T=*x;
X=y;
*Y=t;
Print f (“\n x=%d y=%d”, x, y);
}

(d) Recursion Functions:- It is the process in which function call itself..

In other words : A process in which a function itself for different values of x.

Main ()
{
Int a, F;
Print f (“\n enter number”);
Scan f (“%s”, & a);
F = Time (a);
Print f (value=%d”, F);
}
Time (int x)
{
int f ;
If (x==1)
Return (1);
Else
F=x* Time (x-1);
Return (F);
}

Popularity: 1% [?]

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • BarraPunto
  • Bitacoras.com
  • BlinkList
  • blogmarks
  • BlogMemes Fr
  • BlogMemes Sp
  • Blogosphere News
  • blogtercimlap
  • co.mments
  • connotea
  • Current
  • Design Float
  • Diigo
  • DotNetKicks
  • DZone
  • eKudos
  • email
Comments
comment by stephen
Posted on December 13, 2009 at 5:30 am

thats really a great article on Function its really help me for my project

Leave a Response