SlideServe
Menu
Préférences
© SlideServe · v2026

Understanding User-Defined Functions (UDF) in C Programming

myra-hurley avatar
28 diapositives · 427 Views · Nov 30, 2014

This guide explores User-Defined Functions (UDF) in C programming, detailing the three essential steps to create a UDF: writing the function prototype, defining the function, and calling it. We also discuss the four types of functions, highlighting the importance of UDFs in reusability and problem-solving. Common mistakes encountered during UDF implementation are outlined, along with examples. Additionally, the differences between built-in functions and UDFs are explained, along with the concept of global variables and recursion.

Understanding User-Defined Functions (UDF) in C Programming - slide 1
01 / 28Understanding User-Defined Functions (UDF) in C Programming
1 / 17

Télécharger la présentation

Understanding User-Defined Functions (UDF) in C Programming
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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