140 likes | 290 Views
FUNCTIONS. Definition of function Types of Function Built-in User Defined Catagories of Function No argument No return value Argument but No return value No argument with return value argument with return value. Definition of function.
E N D
Definition of function Types of Function Built-in User Defined Catagories of Function No argument No return value Argument but No return value No argument with return value argument with return value
Definition of function A function is a self contained block of code that performs a particular task. C functions can be classified into two categories namely Built-In function and User Defined function BACK
Built-In Function/Library Function Library function are not required to be written by users. examples of library functions are scanf , printf , sqrtetc • User Defined Function User Defined Function have to be developed by the user. main is an example of user defined function. The use of User Defined Function allows a large program into a number of smaller self contained components, each of which has some unique purpose BACK
scanf() printf() getc() putc() FUNCTIONS Built-In Functions User-Defined Functions A series of Instructions that are to be executed more than once
USER DEFINED FUNCTION : SYNTAX : retu_datatype func_name(arguments) { Body of the function statements; return; } call the function from main() : syntax : func_name(arguments );
#include<stdio.h> void hello() //definition { printf(" Welcome to functions\n"); printf("Good Morning\n"); } void main() { clrscr(); printf("Main, Welcome to functions\n"); hello(); //calling printf("Bye"); getch(); } BACK Gowtham@freshupdates.in
Category of Functions (Based on Return values and passing Arguments) • NO ARGUMENT NO RETURN VALUES • ARGUMENT BUT NO RETURN VALUES • NO ARGUMENT WITH RETURN VALUES • WITH ARGUMENT WITH RETURN VALUES BACK
/* To perform Addition of two numbers */ /* NO ARGUMENT NO RETURN VALUES */ #include<stdio.h> void add(); void add() void main() { { inta,b,c; add(); printf("Enter two numbers\n"); printf("Prg ends"); scanf("%d%d",&a,&b); add(); c=a+b; } printf("The sum is %d\n",c); } BACK
/* To perform Addition of two numbers */ /* WITH ARGUMENT BUT NO RETURN VALUES*/ #include<stdio.h> void add(int,int); void main() { intx,y; printf("Enter two number"); scanf("\t\t%d %d",&x,&y); add(x,y); /* Actual Arguments */ } void add(inta,int b) /* Formal Arguments */ { int c=a+b; printf("\t\tThe C Value is %d",c); } BACK
Return Expression The return statement is used to return from a function. It causes execution to return to the point at which the call to the function was made. The return statement can have a value with it, which it returns to the program
/* To perform Addition of two numbers Without Argument and With Return values */ #include<stdio.h> #include<conio.h> int add(); //declaration void main() { int c; c=add(); /* Return Variable - c */ printf("The sum of two numbers is %d",c); } int add() { inta,b,c; printf("Enter two Numbers="); scanf("%d %d",&a,&b); c=a+b; return(c); } BACK Gowtham@freshupdates.in
/* To perform Addition of two numbers With Argument and With Return values */ #include<stdio.h> int add(int,int); //Function prototype declaration void main() { int c; printf("Enter two Numbers="); scanf("%d %d",&a,&b); c=add(a,b); /* Actual Arguments */ printf("The sum of two numbers is %d",c); } int add(intx,int y) /* Formal arguments */ { int c; c=x+y; return(c); } Gowtham@freshupdates.in
THANKS BACK