Download presentation
Presentation is loading. Please wait.
Published byRandell Stephens Modified over 9 years ago
1
FUNCTIONS A function is a set of instructions that can perform a specific task accordingly. A function is a program that performs a specific task according to the necessity or requirement. A function is a self contained block of code that performs a particular task [1]
2
Necessity Program may become too large Debugging, testing and maintenance is difficult Number of functions can be written and can be combined Much easier to understand, debug and test Avoids redundancy They are called subprograms
3
Efficiency (Saves time and space). Easy to locate faulty functions Categories: 1.Library functions 2.User defined functions Library functions: In built functions that are available in C Eg. Printf(), scanf(), sqrt(), sin(), cos() etc.,
4
USER DEFINED FUNCTIONS It is a subprogram written by the user to perform some task according to the user’s necessity. Rules: 1.Function should have a type. 2.Function names must be declared and defined
5
Elements: 1.Function declaration 2.Function call 3.Function definition Every function has to be declared in the program before it is used in the program is called function declaration. Function declaration: Functiontype functionname (argument list) optional.
6
Function call: All the functions must be called to the main program when it returns a value called function call. Function definition: Each and every user defined function must be defined in the program apart from the main program. It is an independent module that is specially written to implement the requirement of the function.
7
Definition of functions Function name Function typeFunction header List of parameters Local variable declaration Function statements Function body Return statement
8
Example sample #include void main() void printline (void); { printfline(); printf(“Chettinad college of engineering & Technology Karur”); Printline(); }
9
void printline (void) { int I; for (I = 1; i<=50; i++) printf(“*”); printf(“\n”); }
10
Categories of user defined functions NO ARGUMENTS, NO RETURN VALUES NO ARGUMENTS, RETURN VALUES WITH ARGUMENTS, RETURN VALUE WITH ARGUMENTS, NO RETURN VALUE FUNCTION RETURNS MULTIPLE VALUES
11
RECURSION Function that calls itself is called recursive function. long int fact(int); int n; printf(“give value for n”); scanf(“%d”,&n); printf(“The factorial is %d”,fact(n)); }
12
Ling int fact(int number) { if(number>1) return(number*fact(number-1)); else return(1); }
13
PROGRAMMING EXAMPLES #include Void main() { Int a,b; Clrscr(); Printf(“\nEnter a number”); Scanf(“%d”,&n); K=armstrong(n);
14
If (k==n) Printf(“\n Armstrong”); Else Printf(“\n not armstrong”); Getch(); } Armstrong(int m) { If (m==0) Return(0); Else Return(pow(m%10,3)+armstrong(m/10)); }m
15
GCD Int main(void) { Int a,b,gcd1; Int gcd(int,int); Printf(“enter any two nos”); Scanf(%d%d,&a,&b); Gcd1=gcd(a,b); Printf(gcd is gcd1); Return 0; }
16
Int gcd(int p,int q) { Int remainder; Remainder = p-(p/p*q); If (remainder ==0) Rerutn q; Else Return p; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.