+ All Categories
Home > Documents > Function

Function

Date post: 30-Dec-2015
Category:
Upload: myra-hurley
View: 29 times
Download: 0 times
Share this document with a friend
Description:
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 () - PowerPoint PPT Presentation
28
Function User Defined Function (UDF) 3 steps need for UDF 4 types of UDF Common mistakes happen in UDF Global Variable
Transcript

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.

Without function

quiz1 quiz2

total

main

1) Function without return and without parameter

quiz1 quiz2

total

main add

2) Function has return and without parameter

quiz1 quiz2

total

main add

total

Another option - I don’t create variable total in add function

quiz1 quiz2main add

total

3) Function without return and has parameter

q1 q2

total

main addquiz1 quiz2

4) Function has return and has parameter

q1 q2

total

main addquiz1 quiz2

total

COMMON MISTAKE/ERROR IN 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 (23) : error C4716: 'add' : must return a value

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 identifierLine (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 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

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

GLOBAL VARIABLE

quiz1 quiz2

total

main add

total

Without global variable

quiz1 quiz2

quiz1 quiz2

total

main add

total

With global variable

FUNCTION RECURSION


Recommended