Skip to main content

Posts

Showing posts from May, 2019

4.) With argument and With return

In this type of function arguments and parameters must be passed to any function and function return a single value.The type of function depends up on the return value. syntax:- datatype of function name(datatype arg 1,datatype arg2) { statement; ........... ........... return(exp); }                   ex:-main() { int fact(int); printf("\n factorial =%d",fact(5)); getch(); } int fact(int n) { int f=1; while(n>1) { f=f*n; n--; } return(f); }

3.) No argument and with return

In this type of function,parameter and argument are not passed to any f\unction and function must return a value.So the datatype of function depends up on the datatype of return value of the function. syntax:- datatype function name() { statement; .......... ................ return(exp); }               * Ex:- void main() { int x; int sum(); x=sum(); printf("\n sum=%d",x); getch(); } int sum() { int a,b,c; printf("\n enter two number"); scanf("%d%d",&a,&b); c=a+b; return(c); }

2.) With argument and No return

In this type of function argument and parameter must be specified and function doesn't return any value so the datatype of function must be "void" syntax:- void function name(datatype arg 1,datatype arg 2) { statement; ......... .......... } example:-write  a program to input 3 number and calculate sum using function with argument and no return.           Ex:- void main() { int a,b,c; void sum(int,int,int); printf("\n enter three number"); scanf("%d%d%d",&a,&b,&c); sum(a,b,c); getch(); } void sum(int x,int y,int z) { int add; add=x+y+z; printf("\n sum of three number=%d",add); }

Type of user defined function.

1.)No argument and no return 2.)With argument and no return 3.)No argument and with return 4.)With argument and with return               1.)No argument and no return In this type of function,there is no argument and function doesn't return any value.So,the datatype of the function must be "void" syntax:- void function name() { statement; .......... ........ } for example:- write a program to input two number by the keyboards and calculate sum using function no argument and no return. void sum() { int s,a,b; printf("\n enter two numbers"); scanf("%d%d",&a,&b); s=a+b; printf("\n sum=%d",s); } void main() { sum(); getch(); }

#Advantage of function.

                    To define user defined function in our program file at the top of the main() function or below of the  main function.It can't be defined in the body of main function. user defined function can be written in to a saperate file also. syntax:- datatype function name (argument with data type) {    statement; ................... ................. ................. return(exp); } :- Were datatype of a function depends up on the datatype of return value by the function. :-The rule of function name is same as the rule of variable name. :-Argument is also called parameter.The variable name or name are declared with in the paranthesis() of a function is called arguments. Note :- If a function doesn't return any function the data type of function is void(null datatype). In c language it is optimal.

#What is function?

A Function is a self contained sub program that is meant to do some well defined task. A function is also called procedure,subroutine,subprogram.It is suitable set of instruction which performs a task is called function.In another word small module of program is called function. * Type of function There are two type function 1.)Inbuilt/predefined/library function printf() scanf() clrscr() getchar() getch() putchar() strcmp() etc... 2.) User defined function:- The user can create there own function for performing any specific task. Three thing required to create user defined function are:- a.) function definition b.)function prototype c.)function call