Published on


Published in: Education, Technology, Business
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. Function A function is a set of statements, written in a particular language that performs a particular task ,when ever called.
  2. 2. Types of functions 1.Inbuilt or system defined or Library Functions 2.User defined Functions
  3. 3. Inbuilt functions These are the functions whose defintions is already defined and stored in a respective header files of c library . Example of this type of functions are gets(),puts(),clrscr(),getche(),printf(),scanf(). Etc.
  4. 4. User defined functions  The functions which are created by the user to, perform the desired task,are called user defined functions. User defined functions provide the following Benefits:1.By using user defined functions,we can divide a large problem into small independence modules,each having a defined purpose.the process is called modulation.
  5. 5. benefits 2.Smaller modules are easy to design and debug. 3.We can create own user defined functions,in the way,a function can be created once and used for many times.i.e sharing of the code. 4Modulation provide greater clarity to the programs.
  6. 6. Defining a function Syntax :<return type><name>(parameterlist)> { Statement 1; Statement 2; ----------------Statement n; Return (expression); }
  7. 7. Elements of functions 1. Return type :- is datatype of the value ,returned by the function .void is used when the function does not return any value,int and float are used when functions returns integer or floating point values. 2.Name:-is the name of the function 3.Parameter list:-contain variables carrying information from the main function. 4 Function call is represented by writing the name of the function followed by braces and semicolon;
  8. 8. Example describes a function to calc sum of two no. Int sum(int a,int b) { // function body Printf(“the sum of no is being calculated”); Return(a+b); } Void main() { Int j=0; Int k=10; Int m=12; J=sum(k,m); //call to sum function Printf(“the sum is %d”,j); }
  9. 9. Return type of the function is “int”.the name of the function is “sum” .the parameter are” a” and “b” of integer type. C allows us to define user defined function in various ways.we can define the functions in following ways: 1. Functions with no argument and no return type. 2. Function with argument and no return type. 3. Functions with argument and with return type like int ,float.
  10. 10. Functions with no argument and no return type. These are the simple function which do not take any data from calling functions, in the form of parameters and do not return anything to calling functions, after execution.
  11. 11. The example describes the use of func. with no parameter and no return type Simple function. #include<stdio.h> #include<conio.h> Void main() { void punjab(); //function prototyping Void delhi(); //function prototyping Printf(“n I m from main”); Punjab(); // call to punjab()
  12. 12. Printf(“nt I m back in main”); Himachal(); Print f(“nt t I m from main again”); } Void punjab() { Printf(“n I m from punjab”); } Void himachal() { Print(“n I m from himachal”); }
  13. 13. output I m from main I m from punjab I m back in main I m from himchal I m from main again
  14. 14. The example describes the use of func. with no parameter and return type Example to calculate the sum of two numbers using #include<stdio.h> #include<conio.h> Void main() { Int sum(); Int result();
  15. 15. Clrscr(); Printf(“calculate sum of two numbers “); Getch(); Result=sum(); call to function sum //result contains the value entered Printf(“n the result after adding is number %d”,result); Getch(); } Int sum() body of the function {
  16. 16. Int a,b; Printf(“n enter 2 numbers”); Scanf(“%d%d”,&a,&b); Return(a+b); }
  17. 17. Out put Ready to calculate sum of two numbers-press any key Enter 2 number 12 23 The result after adding numbers is 35
  18. 18. Functions with argu and no return type  To demonstrate the functions with parameter and no return #include<stdio.h> #include<conio.h> Void main() { Void sum(int,int); Int a,b; Printf(“enter 2 numbers”); Scanf(“%d%”,&a,&b); Sum(a,b); Getch();
  19. 19. } //end of main Void sum(int f1,int f2) { Int result; //variable in function Result=(f1+f2); Printf(“n the result after addition is%d”,result); }
  20. 20. Out put Enter 2 numbers 10 10 The result after addition is 20
  21. 21. Func with argu and return type #include<stdio.h> #include<conio.h> Void main() { Int sum(int,int); Int a,b,ans; Printf(“enter 2 numbers”); Sanf(“%d%d”,&a,&b); Ans=sum(a,b); Printf(“n the result after addition is %d”,ans);
  22. 22. Getch(); } Int sum(int f1,int f2); { Int result; Result=f1+f2; Return(result); }
  23. 23. output  Enter 2 numbers 12 12  The result after addition is 24