Download
function n.
Skip this Video
Loading SlideShow in 5 Seconds..
Function PowerPoint Presentation

Function

104 Vues Download Presentation
Télécharger la présentation

Function

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Function User Defined Function (UDF) 3 steps need for UDF 4 types of UDF Common mistakes happen in UDF Global Variable

  2. Built In vs User Defined • Built in function is a function that already exist inside c. • E.g.: printf() and scanf() • In order to use built in function, programmer needs to include the header file. E.g.: stdio.h • User Defined function is a function that the programmer wants to create. • In order to create one user defined function, programmer needs to do 3 steps: • write function prototype • write function definition • write function call

  3. Why need user defined function • We need user defined function because • Function that we want to create is not available in built in function • We want to reuse the process more than 1 time

  4. Types of Function • There are 4 types of function • Function without return and without parameter • Function has return and without parameter • Function without return and has parameter • Function has return and has parameter. • We will try to learn how to create all these functions on next slide. • But first, we try to look at a simple program without user defined function.

  5. Without function main quiz2 quiz1 total

  6. 1) Function without return and without parameter main add quiz2 quiz1 total

  7. 2) Function has return and without parameter main add quiz2 quiz1 total total

  8. Another option - I don’t create variable total in add function main add quiz2 quiz1 total

  9. 3) Function without return and has parameter main add q1 q2 quiz1 quiz2 total

  10. 4) Function has return and has parameter main add q1 q2 quiz1 quiz2 total total

  11. Common mistake/error in function

  12. Line (5) : error C2144: syntax error : 'void' should be preceded by ';‘ Function prototype should have semi-colon at the end

  13. Line (11) : error C2447: '{' : missing function header (old-style formal list?) No semicolon for function header

  14. Line (23) : error C4716: 'add' : must return a value

  15. Line (7) : error C2440: '=' : cannot convert from 'void' to 'float‘ Line (15) : error C2556: 'float add(void)' : overloaded function differs only by return type from 'void add(void)‘ Line (15) : error C2371: 'add' : redefinition; different basic types

  16. Line (22) : warning C4244: 'return' : conversion from 'float' to 'char', possible loss of data

  17. Line (18) : error C2065: 'quiz1' : undeclared identifier Line (18) : error C2065: 'quiz2' : undeclared identifier

  18. Line (11) : error C2440: '=' : cannot convert from 'void' to 'float'

  19. No error, but look at line 11 and 12, what happen? It will call function add twice - no problem But what if the value entered are different. So why not keep in a variable total, and use the variable again

  20. quiz1, quiz2, total are float, so cannot use %d Line (9) : error C2065: 'quiz1' : undeclared identifier Line (9) : error C2065: 'quiz2' : undeclared identifier

  21. quiz1, quiz2, total are float, so cannot use %d

  22. quiz1, quiz2, total are float, so cannot use %d Cannot return more than 1 value So the solution is global variable

  23. GLOBAL VARIABLE

  24. Without global variable main add quiz1 quiz2 quiz1 quiz2 total total

  25. With global variable quiz1 quiz2 main add total total

  26. FUNCTION RECURSION