280 likes | 391 Views
Function. User Defined Function (UDF) 3 steps need for UDF 4 types of UDF Common mistakes happen in UDF Global Variable. Built In vs User Defined. Built in function is a function that already exist inside c. E.g.: printf () and scanf ()
E N D
Function User Defined Function (UDF) 3 steps need for UDF 4 types of UDF Common mistakes happen in UDF Global Variable
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
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
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.
Without function main quiz2 quiz1 total
1) Function without return and without parameter main add quiz2 quiz1 total
2) Function has return and without parameter main add quiz2 quiz1 total total
Another option - I don’t create variable total in add function main add quiz2 quiz1 total
3) Function without return and has parameter main add q1 q2 quiz1 quiz2 total
4) Function has return and has parameter main add q1 q2 quiz1 quiz2 total total
Line (5) : error C2144: syntax error : 'void' should be preceded by ';‘ Function prototype should have semi-colon at the end
Line (11) : error C2447: '{' : missing function header (old-style formal list?) No semicolon for function header
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
Line (22) : warning C4244: 'return' : conversion from 'float' to 'char', possible loss of data
Line (18) : error C2065: 'quiz1' : undeclared identifier Line (18) : error C2065: 'quiz2' : undeclared identifier
Line (11) : error C2440: '=' : cannot convert from 'void' to 'float'
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
quiz1, quiz2, total are float, so cannot use %d Line (9) : error C2065: 'quiz1' : undeclared identifier Line (9) : error C2065: 'quiz2' : undeclared identifier
quiz1, quiz2, total are float, so cannot use %d Cannot return more than 1 value So the solution is global variable
Without global variable main add quiz1 quiz2 quiz1 quiz2 total total
With global variable quiz1 quiz2 main add total total