Lecture20 user definedfunctions.ppt

6,554 views

Published on

user defined functions

7 Comments
5 Likes
Statistics
Notes
  • we can serch the downlod opthin and serch it
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • how can improv c language
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Thanks alot!
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • how to download this material
    it is excellent
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • thank you, please do share this slide with your friends and colleagues
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
6,554
On SlideShare
0
From Embeds
0
Number of Embeds
226
Actions
Shares
0
Downloads
425
Comments
7
Likes
5
Embeds 0
No embeds

No notes for slide

Lecture20 user definedfunctions.ppt

  1. 1. USER DEFINED FUNCTIONS IN ‘C’Prakash Khaire Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
  2. 2. IntroductionStrength of ‘C’ language is C functions●They are easy to define and use●We are very much familiar with main(), printf() &●scanf() functionPrakash Khaire, B V Patel Inst. of BMCBMCGopal Vidyanagar B V Patel Inst. of & IT, & IT, Gopal VidyanagarLecturer
  3. 3. In this chapter, you will see…● How a function is designed ?● How a function is integrated into a program ?● How two or more functions are put together /● How they communicate with one another ?Prakash Khaire,Prakash Khaire, B V Patel Inst. of BMCBMCGopal Vidyanagar B V Patel Inst. of & IT, & IT, Gopal VidyanagarLecturerLecturer
  4. 4. Definition - function●A set of statements working together withcommon goal is known as function.●Also known as subprograms which are used tocompute a value or perform a specific task.●They can’t run independently and are alwayscalled by the main() program or by some otherfunction.Prakash Khaire,Prakash Khaire, B V Patel Inst. of BMCBMCGopal Vidyanagar B V Patel Inst. of & IT, & IT, Gopal VidyanagarLecturerLecturer
  5. 5. Categories of fucntions●In ‘C’ language, functions are classified into thefollowing two categories●Library functions●User-Defined functions●scanf(), printf(), getch(), strlen(), strcmp(),strcat(), sqrt(), pow() are this are libraryfunctions.●main() is user-defined functionsPrakash Khaire,Prakash Khaire, B V Patel Inst. of BMCBMCGopal Vidyanagar B V Patel Inst. of & IT, & IT, Gopal VidyanagarLecturerLecturer
  6. 6. User Defined Functions●User defined functions are self-contained blocksof statements which are written by the user tocompute or perform a task.●They can be called by the main programrepeatedly as per the requirement.Prakash Khaire,Prakash Khaire, B V Patel Inst. of BMCBMCGopal Vidyanagar B V Patel Inst. of & IT, & IT, Gopal VidyanagarLecturerLecturer
  7. 7. Uses of functionsThey are very much useful when a block of●statements has to be written/executed again andagain.They are useful when program size are too large●and complex.It works like top-down modular programming technique to●solve a problem.They are also used to reduce the difficulties during●debugging a program.Prakash Khaire,Prakash Khaire, B V Patel Inst. of BMCBMCGopal Vidyanagar B V Patel Inst. of & IT, & IT, Gopal VidyanagarLecturerLecturer
  8. 8. Uses of functions●The length of a source program can be reducedby using functions at appropriate places.●It is easy to locate and isolate a faulty functionfor further investigations.●A function can be used by many otherprograms. Thus C programmer can build theirown library.Prakash Khaire,Prakash Khaire, B V Patel Inst. of BMCBMCGopal Vidyanagar B V Patel Inst. of & IT, & IT, Gopal VidyanagarLecturerLecturer
  9. 9. Top-down modular programming using functions Main program Function A Function B Function C B B 1 1Prakash Khaire, B V Patel Inst. of BMC & IT, Gopal VidyanagarLecturer
  10. 10. A MULTI-FUNCTION PROGRAMvoid main(){printline();printf(“I love my parents !!!”);printline();}void printline(){ int I; for(i=0;i<40;i++) printf(“-”);printf(“n”);}Prakash Khaire,Prakash Khaire, B V Patel Inst. of BMCBMCGopal Vidyanagar B V Patel Inst. of & IT, & IT, Gopal VidyanagarLecturerLecturer
  11. 11. ELEMENTS OF USER DEFINEDFUNCTIONSome similarities between functions and●variables●Both functions names and variables are consideredas identifiers and therefore they must follow the rulesfor identifiers●Like variables, functions have type associated withthem●Like variables, function names and their types mustbe declared and defined before they are used inprogramPrakash Khaire,Prakash Khaire,Lecturer B V Patel Inst. of BMCBMCGopal Vidyanagar B V Patel Inst. of & IT, & IT, Gopal VidyanagarLecturer
  12. 12. ELEMENTS OF USER-DEFINEDFUNCTION●In order to make use of user-defined functions, we need to establishthree elements that are related to functions.●Function definition●Function Call●Function declarationPrakash Khaire, B V Patel Inst. of BMC & IT, Gopal VidyanagarLecturer
  13. 13. FUNCTION DEFINITION●The function definition is an independent programmodule that is specially written or implement therequirements of the function.●To use this block or function, we need to invoke it atthe required place in the program, known as thefunctions●The program that calls the function is referred to asthe calling program or calling functionsPrakash Khaire,Prakash Khaire, B V Patel Inst. of BMCBMCGopal Vidyanagar B V Patel Inst. of & IT, & IT, Gopal VidyanagarLecturerLecturer
  14. 14. main() { ….. function1(); …. …. function2(); } function1() { … … } function2() { … function1(); }Prakash Khaire, B V Patel Inst. of BMC & IT, Gopal VidyanagarLecturer
  15. 15. FLOW OF FUNCTION ● When the program is executed (that is, run) execution always begins at the first statement in the function main no matter where it is placed in the program. ● Other functions are executed only when they are called. ● Function prototypes appear before any function definition, so the compiler translates these first. The compiler can then correctly translate a function call. ● A function call statement results in the transfer of control to the first statement in the body of the called function. ● After the last statement of the called function is executed, the control is passed back to the point immediately following the function call. ● A value-returning function returns a value. Therefore, for value-returning functions, after executing the function when the control goes back to the caller, the value that the function returns replaces the function call statement.Prakash Khaire,Prakash Khaire, B V Patel Inst. of BMCBMCGopal Vidyanagar B V Patel Inst. of & IT, & IT, Gopal VidyanagarLecturerLecturer
  16. 16. FUNCTION DEFINITIONA function definition includes the following●elements●Function name●Function type●List of parameters●Local variable declarations●A return statementPrakash Khaire,Prakash Khaire, B V Patel Inst. of BMCBMCGopal Vidyanagar B V Patel Inst. of & IT, & IT, Gopal VidyanagarLecturerLecturer
  17. 17. A general format of functiondefinition Function headerdatatype functionName(parameter list) 1. function type{ 2. function name local variable declarations; 3. formal parameter list executable statement1; executable statement2; function body … … return statement returns the value - optional }Prakash Khaire, B V Patel Inst. of BMC & IT, Gopal VidyanagarLecturer
  18. 18. A general format of functiondefinition●If no return data type is specified than by default‘C’ will assume that it is an integer type.●If the function is not going to return any value thenwe have to specify the return type as void.●Function name follows the rules of identifier.Prakash Khaire, B V Patel Inst. of BMC & IT, Gopal VidyanagarLecturer
  19. 19. A general format of functiondefinition●Parameter list are list of variables that willreceive the data sent by the callingprogram.●int sum(int a, int b) { ………………… }●float mul(float x, float y) { ………………. }Prakash Khaire, B V Patel Inst. of BMC & IT, Gopal VidyanagarLecturer
  20. 20. Calling a function●A function is called by the calling program using thefunction name with the required number of arguments inparenthesis.●The function call comes in assignment statement or in anoutput statement.●printf(“%d”,sum(a,b));●ans = sum(a,b);A function is called using its name with required number of●arguments in paranthesis.Prakash Khaire,Prakash Khaire, B V Patel Inst. of BMCBMCGopal Vidyanagar B V Patel Inst. of & IT, & IT, Gopal VidyanagarLecturerLecturer
  21. 21. Formal and Actual Argumentsint sum(int, int); //declaration //function definitionvoid main() int sum(int x, int y){ int a=5, b=6,ans; { formal arguments int val; ans =sum(a , b);//calling function arguments actual val = x +y; return val; printf(“Answer : %d”,ans); }}The argument listed in the function calling The arguments used in the function declaration arestatement are referred to as actual arguments referred as formal arguments Prakash Khaire,B V Patel Inst. of BMC B VIT, Gopalof BMC & IT, Gopal VidyanagarPrakash Khaire, Lecturer & Patel Inst. Vidyanagar Lecturer
  22. 22. Formal and Actual Arguments●The argument listed in the ●The agrument used in thefunction calling statement function declaration areare referred to as actual referred as formalarguments. arguments.●They are the actual values ●They are simply formalpassed to a function to variables that accepts orcompute a value or to receive the values suppliedperform a task. by the calling program. Prakash Khaire,B VPrakash Khaire, of BMC V PatelGopal BMC & IT, Gopal Vidyanagar Patel Inst. B & IT, Inst. of Vidyanagar ●Lecturer Lecturer
  23. 23. Rules of call a function●Function is called by the main() or any other function●When the return type of the function is omitted, then by default thereturn type will be int.●A function can return a value any stage of its execution by using morethan one return statements.●The return statement is omitted if it does not return a value directly tothe calling program.●Arguments listed can be omitted, but not the paranthesis () followingthe function.Prakash Khaire,Prakash Khaire, B V Patel Inst. of BMCBMCGopal Vidyanagar B V Patel Inst. of & IT, & IT, Gopal VidyanagarLecturerLecturer
  24. 24. Function Declaration or prototypeTo match the number and data type of the actual and formal●arguments in the calling and called function respectively.To check this compiler will verify the function declaration or prototype.●●data_type function_name(data_type var1, data_type var2,…..,data_typevarn);●Example● int sum(int, int);The prototype declaration is always written above the main() function●definition.Prakash Khaire,Prakash Khaire, B V Patel Inst. of BMCBMCGopal Vidyanagar B V Patel Inst. of & IT, & IT, Gopal VidyanagarLecturerLecturer
  25. 25. Return values and their types●We can pass n numbers of values to the called function, but the calledfunction can only return one value per call.●The return statement can take one of the following form●return;●return(expression);●The plain return does not return any value●return statement with expression returns the value of the expression●There can be more than one return statement if there is use ofconditional statement.Prakash Khaire,Prakash Khaire, B V Patel Inst. of BMCBMCGopal Vidyanagar B V Patel Inst. of & IT, & IT, Gopal VidyanagarLecturerLecturer

×