Anda di halaman 1dari 10

Introduction to Computers

D. MALLIKARJUNA REDDY Department of Statistics Kakatiya University

FUNCTIONS
(User defined)

INTRODUCTION
Definition: A function is a reusable block of code that gets executed on calling. It can be treated as a sub program.

Types of functions :
System defined functions : These are also called as built-in or library functions. They are defined by C compiler & are included in header files. Example : double pow(double,double); This math function is system defined and is included in math.h header file. User defined functions : User can include any number of functions in the program, for which declaration and definition must be provided.

Syntax :

return-type function-name (Argument list.) { local declarations . . code . . } In the above structure return type is related to result returned by a function. By default it is taken as integer. The functions which do not return any value are known as void functions. Every function returns only one value.

Advantages of Functions
It improves the understandability of a program. It reduces the code. It allows reusability. A function can be called any number of times.

Programs with functions need less maintenance.


It performs same set of instructions on different sets of data.

WAP to display user address using functions. //program to display user address using functions #include<stdio.h> #include<conio.h> void address(); void main() { clrscr(); Output : address(); getch(); SAHASRA COLLEGE } MULUGU ROAD void address() WARANGAL { printf(SAHASRA COLLEGE\n); printf(MULUGU ROAD\n); printf(WARANGAL); }

WAP to find the square of a given number using functions. //program to find the square of a given number using functions #include<stdio.h> #include<conio.h> void square(int); Memory void main() { clrscr(); n int n; 5 printf(Enter a number :\n); p 5 scanf(%d,&n); square(n); r 25 getch(); } void square(int p) Output : { int r; Enter a number r = p * p; 5 printf(Square is %d,r); } Square is 25

WAP to determine whether the given value is Armstrong or not //program to find the given value is Armstrong or not #include<stdio.h> #include<conio.h> void arm(int); void main() { int n; clrscr(); printf(Enter a value : \n); scanf(%d,&n); arm(n); } void arm(int m) { int r,sum=0,p = m; while(m>0) { r=m%10; sum=sum+r*r*r; m=m/10; } if(sum= =p) printf(Given number is Armstrong); else printf(Given number is not Armstrong); }

Memory

1)

501 502 153


101 102 153 101 102 15 101 102 1 101 102 0 201 202 r 201 202 r 3 201 202 r 5 201 202 r 1 sum sum sum sum 401 27 401 152 401 153 402 402 401 0 402 402 p 401 402 153

2) m

3) m

4) m

5) m Output :

Enter a value : 153 Given number is Armstrong

THANK YOU.

Thank you for attention

Anda mungkin juga menyukai