Date post: | 30-Dec-2015 |
Category: |
Documents |
Upload: | myra-hurley |
View: | 29 times |
Download: | 0 times |
Function
User Defined Function (UDF)3 steps need for UDF
4 types of UDFCommon 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 because1) Function that we want to create is not available
in built in function2) 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.
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 (18) : error C2065: 'quiz1' : undeclared identifierLine (18) : error C2065: 'quiz2' : undeclared identifier
No error, but look at line 11 and 12, what happen?It will call function add twice - no problemBut what if the value entered are different.So why not keep in a variable total, and use the variable again
Line (9) : error C2065: 'quiz1' : undeclared identifierLine (9) : error C2065: 'quiz2' : undeclared identifier
quiz1, quiz2, total are float, so cannot use %d
Cannot return more than 1 valueSo the solution is global variable
quiz1, quiz2, total are float, so cannot use %d