Jan 12, 2017

Function

Function 

Function could be a self contained block of statements that perform a coherent task of some kind. each c programme may be a concept of the gathering of functions. main( ) is additionally a Function

Types of Functions 

Library functions

These are the built in functions of  of ‘‘C’’library. These are already defined in header files.
e.g. printf ( ) is a function which is used to print at output. It is defined in‘‘stdio.h’’

User defined functions

Programmer can create their own function in C to perform specific task.
Function Prototype
General form of function prototype or declaration is:
 type function-name( argument list);

For example

 int sum ( int, int );
void display (void);

Example of user defined function
# include<stdio.h>
void message();
int main()
{ message();
}
void message()
{
printf(“Hello”);
}

Declaration of many function

# include<stdio.h>
void poly ();
void engg();
void agril();
 int main()
{
printf(“ I am in main”);
poly();
engg();
agri();
}
void poly()
{ printf(“I  am in polytechnic”);
}
void engg()
{ printf(“I am in engineering”);
}
void agri()
{ printf(“ I  am in agri”);}

Summarization

o Any C Program must contain at least one function.
o If program contains only one function it must be main().
o There is no limit on the number of functions present in a C program.
o Each function in a program is called in the sequence specified by functions call in main()
o After each function has done its thing, control return to main(). When main() run out of function calls program ends.
o C  program is a collection of one or more functions
o A function gets called when its name is followed by a semicolon.
 int main()
{ fun();
}
void fun()
{ statement 1;
  statement 2;
  statement n;
}

o Any function can be called by any other function.
int main()
{ printf(“I am in main”);
 fun();
}
void fun()
{ printf(“I am in fun”);
 main();
}
This will run in infinity

o Any function can be called by number times
int main()
{ printf(“I am in main”);
  fun();
fun();
fun();
}
void fun()
{ printf(“I am in fun”);

}

o Any function can call itself, it is called as a ‘recursion’
int main()
{ printf(“I am in main”);
 main();
}
o A function can be called from another function but can not be defined in another function. It should be defined outside of the main.


Why use functions?

o Writing functions avoids rewriting of the same
o code again and again in the program.

o Using a function it becomes easier to write program to keep track of what they are doing.

Passing values to functions.
o The values that we pass to the function called as function arguments.
int main()
{  int i=10,j=12, k=20;
calsum(i,j,k); // i,j,k are actual arguments
}
int calsum( int x, int y, int z) // x,y,z are formal argument
{ printf(“Sum is :%d”,(x+y+z));
}
Returning a value
o The keyword ‘ return’ to used to return value from function which is present in the parenthesis of return.
o On executing return statement it immediately transfers control back to the main program.
int main()
{ int a=30, j=14;
int k;
k=addin(a,j);
printf(“%d”,k);
}
int  addin(int x, int y)
{ int z;
z=(x+y)+1;
return (z);
}
Return
There is no restriction of return statements in a function.
Whenever the control returns from the function some value is definitely returned. It should be accepted in the calling program.
int sum;
sum=calsum(a,b,c);
Here the value returned by calsum must be of type int.
Some valid return statements.
return a;
return (23);
return (15.32);
return (‘v’);
return  0;
If you are not returning any from a function they it should be declared as void.( void is a keyword).
e,g. void display()
Display is not returning any value.

Function declaration and prototype.
Any C function by default returns an integer value. More specifically, whenever a call is made to a function, the compiler assumes that this function would return value of type int.
Function prototype:
#include<stdio.h>
int addin( int, int);
void main()
{ statements;
statements;
}
int addin( int a, int b)
{ statements;
}
Several functions with the same name but with different parameters
#include<stdio.h>
#include<conio.h>
const float pi=3.1416;
void area(float r);
void area(int r);
int main()
{int radius;
 clrscr();
 printf(“Enter radius:”);
scanf(“%d”,&radius);
 area(radius);
area((float)radius);
getch();
return 0;
}
void area(float r)
{printf(“\nFloating point input”);
 printf(“\n Area is :%.2f”,(pi*r*r));
}
void area(int r)
{printf(“\nInteger input”);
 printf(“\n Area is :%.2f”,(pi*r*r));}

No comments:

Post a Comment