SlideShare une entreprise Scribd logo
1  sur  3
UNIVERSITI TUN HUSSEIN ONN MALAYSIA
          FACULTY OF MECHANICAL AND MANUFACTURING ENGINEERING
          BTI 10202: COMPUTER PROGRAMMING


                                          LAB 9 : Functions

 NAME : _____________________________________
 MATRICS NO.: _______________ DATE : ____________                                              MARK



Objective: Introduce to the usage of a function, types of function and function

Theory:
   1. A function is a section of program that perform specific task. For an example; in real life, a
        big project is split into sub project (e.g: contactor manage subcontract to finish a building).
        The same method can be used to segregate certain task in programming.
            a. Main function: to call sub function accordingly
            b. Sub function one: to receive information and calculate
            c. Sub function to : to display the result

    2. Function definition: consist of two parts:
          a. Function header: divided into three parts:
                                          Prototypes declaration example:
                                    Return_type function_name (parameters_list);
                      i. A return type
                              • Similar to variable type: int (round number), char (letter and
                                  strings), float/double (decimal numbers), void (no returning value).
                     ii. A function name
                              • Use easy to understand sub function name that complies with the
                                  syntax rules
                    iii. Parameter lists
                              • The list of data for communication purpose with the calling
                                  functions. If there is no data to be use, the keyword void is use.

            b. Function body normally contains:
                    i. Local declaration
                   ii. Local definition
                  iii. Return expression (this does not apply to function without any returning
                       value)

    3. Function call is a postfix expression to call a sub functions/how to call function from any
       functions:
                                                 Example:
                  printf(“The result is %d”, add(a,b)); //a function can be call from printf
                  display();//direct instruction to jump to the sub function
                  total=add(a,b); //assigning sub function add to total
Exercise 1: type the coding below and answer the following questions:

      1      #include<stdio.h>
      2      #include<conio.h>
      3
      4      int fnAdd(int iValue1, int iValue2);
      5
      6      int main() {
      7              int iResult,iValue1=8, iValue2=9;
      8
      9               iResult = fnAdd(iValue1, iValue2);
      10              printf("Sum of %d and %d is %dn",iValue1, iValue2, iResult);
      11              getch();
      12             return 0; }
      13
      14     int fnAdd(int iValue1, int iValue2)
      15     {
      16              int iSum;
      17              iSum = iValue1 + iValue2;
      18              return (iSum); }


i.         What is the output of the coding? Output:




ii.        Change line 4 with int fnAdd(int iNum1, int iNum2) ;
           line 14 with int fnAdd(int iNum1, int iNum2) and
           line 17 iSum = iNum1 + iNum2;
           Compile and run the coding. Explain how this change affects the output. Output:




           _____________________________________________________________________
           _____________________________________________________________________

iii.       Revert to original coding. Remove coding in line 9
           Change line 10 with printf("Sum of %d and %d is %dn",iValue1, iValue2,
           fnAdd(iValue1, iValue2));
           Compile and run the coding. Explain how this change affects the output. Output:




           Explains what you learn:

           _____________________________________________________________________
           _____________________________________________________________________

iv.        Change line 7 with float iResult,iValue1=8, iValue2=9;
List the error and explains:
     _____________________________________________________________________
     _____________________________________________________________________
     _____________________________________________________________________


v.   Revert to original coding. Change line 14 with   int fnAdd(inum1, inum2)
     List the error and explains:

Contenu connexe

Tendances

C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)SURBHI SAROHA
 
Function overloading(C++)
Function overloading(C++)Function overloading(C++)
Function overloading(C++)Ritika Sharma
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11Rumman Ansari
 
C++ Question on References and Function Overloading
C++ Question on References and Function OverloadingC++ Question on References and Function Overloading
C++ Question on References and Function Overloadingmohamed sikander
 
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAM
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAMSIMPLE C PROGRAMS - SARASWATHI RAMALINGAM
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAMSaraswathiRamalingam
 
Function recap
Function recapFunction recap
Function recapalish sha
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8Rumman Ansari
 
Intro to c chapter cover 1 4
Intro to c chapter cover 1 4Intro to c chapter cover 1 4
Intro to c chapter cover 1 4Hazwan Arif
 
Expressions using operator in c
Expressions using operator in cExpressions using operator in c
Expressions using operator in cSaranya saran
 
1 introducing c language
1  introducing c language1  introducing c language
1 introducing c languageMomenMostafa
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7Rumman Ansari
 
Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04hassaanciit
 

Tendances (20)

C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)
 
Functions
FunctionsFunctions
Functions
 
Function overloading(C++)
Function overloading(C++)Function overloading(C++)
Function overloading(C++)
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11
 
C++ Question on References and Function Overloading
C++ Question on References and Function OverloadingC++ Question on References and Function Overloading
C++ Question on References and Function Overloading
 
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAM
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAMSIMPLE C PROGRAMS - SARASWATHI RAMALINGAM
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAM
 
Lecture 8- Data Input and Output
Lecture 8- Data Input and OutputLecture 8- Data Input and Output
Lecture 8- Data Input and Output
 
Lecture 18 - Pointers
Lecture 18 - PointersLecture 18 - Pointers
Lecture 18 - Pointers
 
Lecture 12 - Recursion
Lecture 12 - Recursion Lecture 12 - Recursion
Lecture 12 - Recursion
 
Function recap
Function recapFunction recap
Function recap
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8
 
Labsheet2
Labsheet2Labsheet2
Labsheet2
 
Functions
FunctionsFunctions
Functions
 
Intro to c chapter cover 1 4
Intro to c chapter cover 1 4Intro to c chapter cover 1 4
Intro to c chapter cover 1 4
 
C語言基本資料型別與變數
C語言基本資料型別與變數C語言基本資料型別與變數
C語言基本資料型別與變數
 
Expressions using operator in c
Expressions using operator in cExpressions using operator in c
Expressions using operator in c
 
1 introducing c language
1  introducing c language1  introducing c language
1 introducing c language
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7
 
Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04
 
Lecture 7- Operators and Expressions
Lecture 7- Operators and Expressions Lecture 7- Operators and Expressions
Lecture 7- Operators and Expressions
 

En vedette

MLP輪読スパース8章 トレースノルム正則化
MLP輪読スパース8章 トレースノルム正則化MLP輪読スパース8章 トレースノルム正則化
MLP輪読スパース8章 トレースノルム正則化Akira Tanimoto
 
Wordpress Portfolio - Marvin Libron
Wordpress Portfolio - Marvin LibronWordpress Portfolio - Marvin Libron
Wordpress Portfolio - Marvin LibronMarvin Libron
 
24 celula citoplasma
24 celula citoplasma24 celula citoplasma
24 celula citoplasmajuanapardo
 
14 o codigo_xenetico
14 o codigo_xenetico14 o codigo_xenetico
14 o codigo_xeneticojuanapardo
 
第4回淑女会 ~facebook講座~
第4回淑女会 ~facebook講座~第4回淑女会 ~facebook講座~
第4回淑女会 ~facebook講座~新一 佐藤
 
Adventure of i function man video
Adventure of i function man videoAdventure of i function man video
Adventure of i function man videochristianprey7
 
Test 1 skema q&a
Test 1 skema q&aTest 1 skema q&a
Test 1 skema q&aalish sha
 
فن استشر والترصيع بالقرميد
فن استشر والترصيع بالقرميدفن استشر والترصيع بالقرميد
فن استشر والترصيع بالقرميدSunflower Al-mahrouqi
 
Lab exe 2 dti 2143
Lab exe 2 dti 2143Lab exe 2 dti 2143
Lab exe 2 dti 2143alish sha
 
Pbl lesson plan a
Pbl lesson plan aPbl lesson plan a
Pbl lesson plan aMayci Neal
 
Maxim_Mozgovoy_Lamoda_cmf2013
Maxim_Mozgovoy_Lamoda_cmf2013Maxim_Mozgovoy_Lamoda_cmf2013
Maxim_Mozgovoy_Lamoda_cmf2013Evgeny Vasyuk
 
Dti2143 lab sheet 8
Dti2143 lab sheet 8Dti2143 lab sheet 8
Dti2143 lab sheet 8alish sha
 
Shrinkage Estimation of Linear Panel Data Models Under Nearly Singular Design
Shrinkage Estimation of Linear Panel Data Models Under Nearly Singular DesignShrinkage Estimation of Linear Panel Data Models Under Nearly Singular Design
Shrinkage Estimation of Linear Panel Data Models Under Nearly Singular Designraffaelesaggio
 
52206811 pequenos-musicos-vol-1
52206811 pequenos-musicos-vol-152206811 pequenos-musicos-vol-1
52206811 pequenos-musicos-vol-1Dinita Lemos
 
Avastusõpe Tartu Kivilinna Gümnaasiumis
Avastusõpe Tartu Kivilinna GümnaasiumisAvastusõpe Tartu Kivilinna Gümnaasiumis
Avastusõpe Tartu Kivilinna GümnaasiumisPiret Jõul
 
Los secretos de las presentaciones de Steve Jobs
Los secretos de las presentaciones de Steve JobsLos secretos de las presentaciones de Steve Jobs
Los secretos de las presentaciones de Steve JobsDavid Aguirre
 
Developing The Mongolian Wolf Spirit In Football
Developing The Mongolian Wolf Spirit In FootballDeveloping The Mongolian Wolf Spirit In Football
Developing The Mongolian Wolf Spirit In FootballMichele Ferraris
 
برنامج معالجة الكلمات
برنامج معالجة الكلماتبرنامج معالجة الكلمات
برنامج معالجة الكلماتSunflower Al-mahrouqi
 

En vedette (20)

MLP輪読スパース8章 トレースノルム正則化
MLP輪読スパース8章 トレースノルム正則化MLP輪読スパース8章 トレースノルム正則化
MLP輪読スパース8章 トレースノルム正則化
 
Wordpress Portfolio - Marvin Libron
Wordpress Portfolio - Marvin LibronWordpress Portfolio - Marvin Libron
Wordpress Portfolio - Marvin Libron
 
24 celula citoplasma
24 celula citoplasma24 celula citoplasma
24 celula citoplasma
 
14 o codigo_xenetico
14 o codigo_xenetico14 o codigo_xenetico
14 o codigo_xenetico
 
第4回淑女会 ~facebook講座~
第4回淑女会 ~facebook講座~第4回淑女会 ~facebook講座~
第4回淑女会 ~facebook講座~
 
Adventure of i function man video
Adventure of i function man videoAdventure of i function man video
Adventure of i function man video
 
Test 1 skema q&a
Test 1 skema q&aTest 1 skema q&a
Test 1 skema q&a
 
فن استشر والترصيع بالقرميد
فن استشر والترصيع بالقرميدفن استشر والترصيع بالقرميد
فن استشر والترصيع بالقرميد
 
Lab exe 2 dti 2143
Lab exe 2 dti 2143Lab exe 2 dti 2143
Lab exe 2 dti 2143
 
Pbl lesson plan a
Pbl lesson plan aPbl lesson plan a
Pbl lesson plan a
 
Maxim_Mozgovoy_Lamoda_cmf2013
Maxim_Mozgovoy_Lamoda_cmf2013Maxim_Mozgovoy_Lamoda_cmf2013
Maxim_Mozgovoy_Lamoda_cmf2013
 
Dti2143 lab sheet 8
Dti2143 lab sheet 8Dti2143 lab sheet 8
Dti2143 lab sheet 8
 
Shrinkage Estimation of Linear Panel Data Models Under Nearly Singular Design
Shrinkage Estimation of Linear Panel Data Models Under Nearly Singular DesignShrinkage Estimation of Linear Panel Data Models Under Nearly Singular Design
Shrinkage Estimation of Linear Panel Data Models Under Nearly Singular Design
 
HucksterBot 2.0
HucksterBot 2.0HucksterBot 2.0
HucksterBot 2.0
 
52206811 pequenos-musicos-vol-1
52206811 pequenos-musicos-vol-152206811 pequenos-musicos-vol-1
52206811 pequenos-musicos-vol-1
 
Avastusõpe Tartu Kivilinna Gümnaasiumis
Avastusõpe Tartu Kivilinna GümnaasiumisAvastusõpe Tartu Kivilinna Gümnaasiumis
Avastusõpe Tartu Kivilinna Gümnaasiumis
 
Los secretos de las presentaciones de Steve Jobs
Los secretos de las presentaciones de Steve JobsLos secretos de las presentaciones de Steve Jobs
Los secretos de las presentaciones de Steve Jobs
 
Developing The Mongolian Wolf Spirit In Football
Developing The Mongolian Wolf Spirit In FootballDeveloping The Mongolian Wolf Spirit In Football
Developing The Mongolian Wolf Spirit In Football
 
برنامج معالجة الكلمات
برنامج معالجة الكلماتبرنامج معالجة الكلمات
برنامج معالجة الكلمات
 
ΠΑΡΟΥΣΙΑΣΗ Ecomobility 2015
ΠΑΡΟΥΣΙΑΣΗ   Ecomobility     2015ΠΑΡΟΥΣΙΑΣΗ   Ecomobility     2015
ΠΑΡΟΥΣΙΑΣΗ Ecomobility 2015
 

Similaire à Lab 9 sem ii_12_13

Similaire à Lab 9 sem ii_12_13 (20)

Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
 
Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
 
Preprocessor directives
Preprocessor directivesPreprocessor directives
Preprocessor directives
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
User Defined Functions in C Language
User Defined Functions   in  C LanguageUser Defined Functions   in  C Language
User Defined Functions in C Language
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
 
Presentation 2 (1).pdf
Presentation 2 (1).pdfPresentation 2 (1).pdf
Presentation 2 (1).pdf
 
Function
FunctionFunction
Function
 
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
 
Functions
FunctionsFunctions
Functions
 
Fnctions part2
Fnctions part2Fnctions part2
Fnctions part2
 
Functions part1
Functions part1Functions part1
Functions part1
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
 
C function presentation
C function presentationC function presentation
C function presentation
 
PSPC-UNIT-4.pdf
PSPC-UNIT-4.pdfPSPC-UNIT-4.pdf
PSPC-UNIT-4.pdf
 
Function
FunctionFunction
Function
 
unit_2 (1).pptx
unit_2 (1).pptxunit_2 (1).pptx
unit_2 (1).pptx
 
C function
C functionC function
C function
 
Functions
Functions Functions
Functions
 
arrays.ppt
arrays.pptarrays.ppt
arrays.ppt
 

Plus de alish sha

T22016 – how to answer with ubs 9
T22016 – how to answer with ubs 9T22016 – how to answer with ubs 9
T22016 – how to answer with ubs 9alish sha
 
July 2014 theory exam (theory)
July 2014 theory exam (theory)July 2014 theory exam (theory)
July 2014 theory exam (theory)alish sha
 
Accounting basic equation
Accounting basic equation Accounting basic equation
Accounting basic equation alish sha
 
It 302 computerized accounting (week 2) - sharifah
It 302   computerized accounting (week 2) - sharifahIt 302   computerized accounting (week 2) - sharifah
It 302 computerized accounting (week 2) - sharifahalish sha
 
It 302 computerized accounting (week 1) - sharifah
It 302   computerized accounting (week 1) - sharifahIt 302   computerized accounting (week 1) - sharifah
It 302 computerized accounting (week 1) - sharifahalish sha
 
What are the causes of conflicts (Bahasa Malaysia)
What are the causes of conflicts (Bahasa Malaysia)What are the causes of conflicts (Bahasa Malaysia)
What are the causes of conflicts (Bahasa Malaysia)alish sha
 
Lab 5 2012/2012
Lab 5 2012/2012Lab 5 2012/2012
Lab 5 2012/2012alish sha
 
Purpose elaborate
Purpose elaboratePurpose elaborate
Purpose elaboratealish sha
 
Test 1 alish schema 1
Test 1 alish schema 1Test 1 alish schema 1
Test 1 alish schema 1alish sha
 
Lab 6 sem ii_11_12
Lab 6 sem ii_11_12Lab 6 sem ii_11_12
Lab 6 sem ii_11_12alish sha
 
Test 1 skema q&a
Test 1 skema q&aTest 1 skema q&a
Test 1 skema q&aalish sha
 
Final project
Final projectFinal project
Final projectalish sha
 
Final project
Final projectFinal project
Final projectalish sha
 
Attn list test
Attn list testAttn list test
Attn list testalish sha
 
Carry markdam31303
Carry markdam31303Carry markdam31303
Carry markdam31303alish sha
 
Final project
Final projectFinal project
Final projectalish sha
 
Final project
Final projectFinal project
Final projectalish sha
 
Dti2143 dam31303 lab sheet 8
Dti2143 dam31303 lab sheet 8Dti2143 dam31303 lab sheet 8
Dti2143 dam31303 lab sheet 8alish sha
 
Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7alish sha
 

Plus de alish sha (20)

T22016 – how to answer with ubs 9
T22016 – how to answer with ubs 9T22016 – how to answer with ubs 9
T22016 – how to answer with ubs 9
 
July 2014 theory exam (theory)
July 2014 theory exam (theory)July 2014 theory exam (theory)
July 2014 theory exam (theory)
 
Accounting basic equation
Accounting basic equation Accounting basic equation
Accounting basic equation
 
It 302 computerized accounting (week 2) - sharifah
It 302   computerized accounting (week 2) - sharifahIt 302   computerized accounting (week 2) - sharifah
It 302 computerized accounting (week 2) - sharifah
 
It 302 computerized accounting (week 1) - sharifah
It 302   computerized accounting (week 1) - sharifahIt 302   computerized accounting (week 1) - sharifah
It 302 computerized accounting (week 1) - sharifah
 
What are the causes of conflicts (Bahasa Malaysia)
What are the causes of conflicts (Bahasa Malaysia)What are the causes of conflicts (Bahasa Malaysia)
What are the causes of conflicts (Bahasa Malaysia)
 
Lab 5 2012/2012
Lab 5 2012/2012Lab 5 2012/2012
Lab 5 2012/2012
 
Purpose elaborate
Purpose elaboratePurpose elaborate
Purpose elaborate
 
Lab sheet 1
Lab sheet 1Lab sheet 1
Lab sheet 1
 
Test 1 alish schema 1
Test 1 alish schema 1Test 1 alish schema 1
Test 1 alish schema 1
 
Lab 6 sem ii_11_12
Lab 6 sem ii_11_12Lab 6 sem ii_11_12
Lab 6 sem ii_11_12
 
Test 1 skema q&a
Test 1 skema q&aTest 1 skema q&a
Test 1 skema q&a
 
Final project
Final projectFinal project
Final project
 
Final project
Final projectFinal project
Final project
 
Attn list test
Attn list testAttn list test
Attn list test
 
Carry markdam31303
Carry markdam31303Carry markdam31303
Carry markdam31303
 
Final project
Final projectFinal project
Final project
 
Final project
Final projectFinal project
Final project
 
Dti2143 dam31303 lab sheet 8
Dti2143 dam31303 lab sheet 8Dti2143 dam31303 lab sheet 8
Dti2143 dam31303 lab sheet 8
 
Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7
 

Lab 9 sem ii_12_13

  • 1. UNIVERSITI TUN HUSSEIN ONN MALAYSIA FACULTY OF MECHANICAL AND MANUFACTURING ENGINEERING BTI 10202: COMPUTER PROGRAMMING LAB 9 : Functions NAME : _____________________________________ MATRICS NO.: _______________ DATE : ____________ MARK Objective: Introduce to the usage of a function, types of function and function Theory: 1. A function is a section of program that perform specific task. For an example; in real life, a big project is split into sub project (e.g: contactor manage subcontract to finish a building). The same method can be used to segregate certain task in programming. a. Main function: to call sub function accordingly b. Sub function one: to receive information and calculate c. Sub function to : to display the result 2. Function definition: consist of two parts: a. Function header: divided into three parts: Prototypes declaration example: Return_type function_name (parameters_list); i. A return type • Similar to variable type: int (round number), char (letter and strings), float/double (decimal numbers), void (no returning value). ii. A function name • Use easy to understand sub function name that complies with the syntax rules iii. Parameter lists • The list of data for communication purpose with the calling functions. If there is no data to be use, the keyword void is use. b. Function body normally contains: i. Local declaration ii. Local definition iii. Return expression (this does not apply to function without any returning value) 3. Function call is a postfix expression to call a sub functions/how to call function from any functions: Example: printf(“The result is %d”, add(a,b)); //a function can be call from printf display();//direct instruction to jump to the sub function total=add(a,b); //assigning sub function add to total
  • 2. Exercise 1: type the coding below and answer the following questions: 1 #include<stdio.h> 2 #include<conio.h> 3 4 int fnAdd(int iValue1, int iValue2); 5 6 int main() { 7 int iResult,iValue1=8, iValue2=9; 8 9 iResult = fnAdd(iValue1, iValue2); 10 printf("Sum of %d and %d is %dn",iValue1, iValue2, iResult); 11 getch(); 12 return 0; } 13 14 int fnAdd(int iValue1, int iValue2) 15 { 16 int iSum; 17 iSum = iValue1 + iValue2; 18 return (iSum); } i. What is the output of the coding? Output: ii. Change line 4 with int fnAdd(int iNum1, int iNum2) ; line 14 with int fnAdd(int iNum1, int iNum2) and line 17 iSum = iNum1 + iNum2; Compile and run the coding. Explain how this change affects the output. Output: _____________________________________________________________________ _____________________________________________________________________ iii. Revert to original coding. Remove coding in line 9 Change line 10 with printf("Sum of %d and %d is %dn",iValue1, iValue2, fnAdd(iValue1, iValue2)); Compile and run the coding. Explain how this change affects the output. Output: Explains what you learn: _____________________________________________________________________ _____________________________________________________________________ iv. Change line 7 with float iResult,iValue1=8, iValue2=9;
  • 3. List the error and explains: _____________________________________________________________________ _____________________________________________________________________ _____________________________________________________________________ v. Revert to original coding. Change line 14 with int fnAdd(inum1, inum2) List the error and explains: