EKT120: Computer Programming

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Lecture 5: Modular Programming (functions – part 1 BJ Furman 27FEB2012.
Computer Science 210 Computer Organization Introduction to C.
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
CECS 121 EXAM 1. /* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
1. Function prototype Function prototype is a declaration; indicates the function exists Should have function name, return type and parameter Placed before.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
1 ICS103 Programming in C Lecture 7: Introduction to Functions.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Functions (1)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)
CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;
Computer Programming Rattapoom Waranusast Department of Electrical and Computer Engineering Faculty of Engineering, Naresuan University.
Principles of Programming - NI Chapter 6: Function In this chapter, you will learn about Introduction to function User define function Function prototype.
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
Chapter 5 Modular Design and Function C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
Functions. Why use functions? They can break your problem down into smaller sub-tasks (modularity).  easier to solve complex problems They make a program.
1 TOPICS TO DISCUSS : FUNCTIONS TYPES OF FUNCTIONS HEADER FILES PRESENTED BY : AVISHEK MAJUMDAR(837837) GUNJAN AGARWAL(856587) SATYAPRIYA DEY(856624)
Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.
Lecture 7: Modular Programming (functions) B Burlingame 05 October, 2016.
EKT120: Computer Programming
Chapter 6 - Functions modular programming general function format
Computer Science 210 Computer Organization
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Functions, Part 2 of 2 Topics Functions That Return a Value
Programming Languages and Paradigms
FUNCTIONS.
Quiz 11/15/16 – C functions, arrays and strings
C Short Overview Lembit Jürimägi.
PGT 106: Computer Programming
Week 5 – Functions (1) EKT120: Computer Programming.
Programming Paradigms
Deitel- C:How to Program (5ed)
EKT120: Computer Programming
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Formatted and Unformatted Input/Output Functions
Computer Science 210 Computer Organization
Chapter 5 - Functions Outline 5.1 Introduction
Scope, Parameter Passing, Storage Specifiers
Lexical Elements, Operators, and the C Cystem
Functions Declarations CSCI 230
Lexical Elements, Operators, and the C Cystem
CSCE 206 Lab Structured Programming in C
Functions, Part 1 of 3 Topics Using Predefined Functions
بنام خدا زبان برنامه نویسی C (21814( Lecture 4 Chapter 5
Functions, Part 2 of 3 Topics Functions That Return a Value
A function with one argument
Dr Tripty Singh Tutorial for Fuctions
Function In this lesson, you will learn about Introduction to Function
Functions, Part 1 of 3 Topics Using Predefined Functions
In C Programming Language
Introduction to Problem Solving and Programming
Functions Department of Computer Science-BGU יום שישי 26 אפריל 2019.
Functions Imran Rashid CTO at ManiWeber Technologies.
Programming Languages and Paradigms
CSCE 206 Lab Structured Programming in C
Functions, Part 1 of 3 Topics Using Predefined Functions
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
CPS125.
Presentation transcript:

EKT120: Computer Programming Week 5 – Functions (1) UniMAP SemII-11/12 EKT120: Computer Programming

EKT120: Computer Programming Outline Why use functions? Functions in C Pre-defined functions User-defined functions Function prototypes Function definitions Function calls What about number, order and type of parameter? Functions that do not return a value Functions that return a value Miscellaneous about functions Sample application Scope and mechanics of passing values to functions UniMAP SemII-11/12 EKT120: Computer Programming

EKT120: Computer Programming Why use functions? Let say you want to print one row of number 8 and one row of number 9 #include <stdio.h> int main() { int iLoop1, iLoop2; //print one row of number 8 for(iLoop1=1; iLoop1<=10; iLoop1++) printf(“8"); printf("\n"); //go to new line //print one row of number 9 for(iLoop2=1; iLoop2<=10; iLoop2++) printf(“9“); return 0; } UniMAP SemII-11/12 EKT120: Computer Programming

EKT120: Computer Programming Why use functions? It seems that you are doing the same thing twice!!(i.e. printing two rows of numbers) This is wasting time and not flexible!! So, need to use function UniMAP SemII-11/12 EKT120: Computer Programming

EKT120: Computer Programming Why use functions? #include <stdio.h> void fnDisplay(int); //function prototype int main() { fnDisplay(8); //function call fnDisplay(9); //function call return 0; } void fnDisplay(int value) //function definition { int iLoop; for(iLoop=1; iLoop<=10; iLoop++) printf("%d", value); printf("\n"); //go to new line UniMAP SemII-11/12 EKT120: Computer Programming

EKT120: Computer Programming Functions in C Functions can be created to execute small, frequently-used tasks In C, there are predefined functions or sometimes called standard functions, and there are user-defined functions. Predefined functions are already available functions that can be used, called library The usage is like stdio.h, in which the library name must be #included at the top of the source code (preprocessor directive) UniMAP SemII-11/12 EKT120: Computer Programming

Predefined Functions (Library) Common libraries are stdio.h, math.h, string.h, and stdlib.h stdio.h related functions: printf, scanf,etc math.h related functions: sin, cos, exp, pow, sqrt, etc. string.h related functions: strcmp, strcpy, strlen, etc. stdlib.h related functions: abs, fabs UniMAP SemII-11/12 EKT120: Computer Programming

Predefined Functions (Library)-example #include <stdio.h> #include <math.h> #include <string.h> void main() { string sName; int iVol1, iVol2, iN,iR, iKTemp, iLength; strcpy(sName, “Marina”); iVol2 = iVol1 * exp(iN * iR * iKTemp); iLength = strlen(“Mahathir”); } UniMAP SemII-11/12 EKT120: Computer Programming

User-Defined Functions What do we need to define and make use of user-defined functions? Function prototypes Function definitions Function calls UniMAP SemII-11/12 EKT120: Computer Programming

EKT120: Computer Programming Function Prototypes Function prototype is a declaration; indicates the function exists Should have function name, return type and parameter Argument name is not compulsory in function header Function prototype has the following form: <return_type> <function_name> (arg_type arg_name, ...); int fnSum (int iNum1,int iNum2); int fnSum (int,int); //is also acceptable semicolon UniMAP SemII-11/12 EKT120: Computer Programming

EKT120: Computer Programming Function Definitions Function definition includes the body of a function Function definition has the following form: <return_type> <function_name> (arg_type arg_name, ...) { … statements … } int fnSum (int iNum1,int iNum2) int iAdd; iAdd = iNum1 + iNum2; return(iAdd); Notice that argument name is used in the function body Unlike function prototype, argument name in function definition must be included in function header no semicolon function header UniMAP SemII-11/12 EKT120: Computer Programming

EKT120: Computer Programming Function Calls Consists of a function name followed by an argument expression list enclosed in parentheses Function call has the following form: <function_name> (exp, exp ...) exp is an expression – can be variable or constant iResult = fnSum(x,y); UniMAP SemII-11/12 EKT120: Computer Programming

Example of function in program //This program sums up two numbers #include <stdio.h> int fnSum(int, int); //function prototype int main() { int iX, iY, iResult; printf( “Enter x and y : ”); scanf(“%d %d”, &iX, &iY); iResult = fnSum(iX,iY); //function call printf(“Sum is : %d”, iResult); return 0; } int fnSum(int iNum1, int iNum2)//function definition { int iAdd; iAdd = iNum1+iNum2; return(iAdd);} function header UniMAP SemII-11/12 EKT120: Computer Programming

What about number, order and type of parameter? Number, order and type of parameters in the argument list of a function call and function definition MUST match. If function prototype and definition have three parameters then the function call must have three parameters. If the types are int, float and double in the prototype, the types in the function call should be int, float and double, respectively. UniMAP SemII-11/12 EKT120: Computer Programming

What about number, order and type of parameter?(e.g1) prototype void fnFunction2(int iN, double dX); function header void fnFunction2(int iN, double dX) function call fnFunction2 (iM, dY); Note that there are two arguments for function prototype, function definition and function call; the first is int and the second is double. With these three we have met the number, order and type requirements. UniMAP SemII-11/12 EKT120: Computer Programming

What about number, order and type of parameter?(e.g2) int fnSum(int, int); //function prototype int fnSum(int iNum1, int iNum2) //function definition fnSum(iX,iY); //function call Refer to program in slide 13 Number, order and type parameter are met because: there are two parameters, the parameters are listed in order i.e respectively and first parameter is int and second parameter is int. UniMAP SemII-11/12 EKT120: Computer Programming

Functions that do not return a value //This program sums up two numbers #include <stdio.h> void fnSumPrint(int, int); //function prototype void fnFunction1(); //function prototype int main() { int iX,iY; fnFunction1(); //function call printf(“Enter x and y: ”); scanf(“%d %d”, &iX, &iY); fnSumPrint(iX,iY); //function call return 0; } void fnSumPrint(int iNum1, int iNum2) //function definition { int iAdd; iAdd = iNum1+iNum2; printf(“Sum is: %d”,iAdd); void fnFunction1() { printf(“Welcome to this program\n”); } UniMAP SemII-11/12 EKT120: Computer Programming

Functions that return a value //This program sums up two numbers #include <stdio.h> int fnSum(int,int); //function prototype int main() { int iX,iY,iResult; printf(“Enter x and y: ”); scanf(“%d %d”, &iX, &iY); iResult = fnSum(iX,iY); //function call printf(“Sum is : %d”,iResult); return 0; } int fnSum(int iNum1, int iNum2) //function definition { int iAdd; iAdd = iNum1+iNum2; return(iAdd); UniMAP SemII-11/12 EKT120: Computer Programming

Miscellaneous about functions Function call used as logical expression int fnCalc(int,int); //function prototype int main(void) { int iNum1, iNum2; scanf(“%d %d”,&iNum1,&iNum2); if(fnCalc(iNum1,iNum2)>100) //function call used as logical expression printf(“result greater than 100”); else printf(“result less than 100”); return 0; } int fnCalc(int iN1,int iN2) { int iAnswer; iAnswer=iN1+iN2; return(iAnswer); UniMAP SemII-11/12 EKT120: Computer Programming

Miscellaneous about functions Function call used in printf statement int fnCalc(int,int); //function prototype int main(void) { int iNum1,iNum2; scanf(“%d %d”,&iNum1,&iNum2); printf(“Sum = %d”,fnCalc(iNum1, iNum2)); //function call returns a //value and puts in printf return 0; } int fnCalc(int iN1,int iN2) { int iAnswer; iAnswer=iN1+iN2; return(iAnswer); UniMAP SemII-11/12 EKT120: Computer Programming

Miscellaneous about functions Rules regarding naming convention for variables iNum1 passes value to iN1, iNum2 passes value to iN2 Better use different variable names for parameters in main AND parameters in function definition int fnCalc(int,int); //prototype function int main(void) { int iNum1,iNum2,iResult; //declare like this scanf(“%d %d”,&iNum1,&iNum2); iResult = fnCalc(iNum1,iNum2); //function call printf(“sum = %d“,iResult); return 0; } //function definition int fnCalc(int iN1,int iN2) //simply declare like this { int iAnswer; iAnswer=iN1+iN2; return(iAnswer); UniMAP SemII-11/12 EKT120: Computer Programming

EKT120: Computer Programming Sample application Write a C program that calculates and prints addition and subtraction of numbers. Your program should have functions: fnAdd : adds two numbers fnSubtract : subtracts two numbers fnPrintResult : prints results from calculation UniMAP SemII-11/12 EKT120: Computer Programming

Sample application(cont) #include <stdio.h> int fnAdd(int,int); int fnSubtract(int,int); void fnPrintResult(int); int main() { int iNum1,iNum2,iAnswer; char cOp; printf(“Enter two numbers and operator:”); scanf(“%d %d %c”, &iNum1,&iNum2,&cOp); switch(cOp) { case ‘+’ :iAnswer=fnAdd(iNum1,iNum2);break; case ‘-’ :iAnswer=fnSubtract(iNum1,iNum2);break; default: printf(“Invalid operator”); } fnPrintResult(iAnswer); return 0; int fnAdd(int iX,int iY) { int iSum; iSum = iX+iY; return(iSum); } int fnSubtract(int iX,int iY) int iSub; iSub=iX-iY; return(iSub); void fnPrintResult(int iAns) printf(“Answer is %d”, iAns); UniMAP SemII-11/12 EKT120: Computer Programming

EKT120: Computer Programming 1 /* Fig. 5.4: fig05_04.c 2 Finding the maximum of three integers */ 3 #include <stdio.h> 4 5 int fnMaximum(int, int, int); /* function prototype */ 6 7 int main()‏ 8 { 9 int iA, iB, iC; 10 11 printf( "Enter three integers: " ); 12 scanf( "%d %d %d", &iA, &iB, &iC ); 13 printf( "Maximum is: %d\n", fnMaximum( iA, iB, iC ) ); 14 15 return 0; 16 } 17 18 /* Function maximum definition */ 19 int fnMaximum(int iX, int iY, int iZ)‏ 20 { 21 int iMax = iX; 22 23 if ( iY > iMax )‏ 24 iMax = iY; 25 26 if ( iZ > iMax )‏ 27 iMax = iZ; 28 29 return iMax; 30 } 1. Function prototype (3 parameters)‏ 2. Function call 3. Function definition Program Output Enter three integers: 22 85 17 Maximum is: 85 UniMAP SemII-11/12 EKT120: Computer Programming

Scope and Mechanics of Passing Values to Functions Scope refers to the region in which a declaration is active File scope is also called global variable declared at the top of a source file declarations not placed in any functions can be used by any statements that are being executed in the system Function scope is also called local variable declared in a block { … } scope is within its block – lifetime while the block is executed UniMAP SemII-11/12 EKT120: Computer Programming

Global Variable : Example #include <stdio.h> int iGlobal = 3; //This is the global variable void fnChangeGlobal( ); int main(void) { printf("%d\n“, iGlobal); //Reference to global //variable in a function fnChangeGlobal(); printf("%d\n", iGlobal); return 0; } void fnChangeGlobal( ) { iGlobal = 5; } //Reference to global UniMAP SemII-11/12 EKT120: Computer Programming

Global Variable : Example The output will be: 3 5 UniMAP SemII-11/12 EKT120: Computer Programming

Local Variable : Example #include <stdio.h> void fnChangeLocal(); int main(void) { int iLocal = 3; //This is a local variable printf("%d\n", iLocal); //Reference to local //variable in a function fnChangeLocal(); printf("%d\n", iLocal); return 0; } void fnChangeLocal() { int iLocal = 5; //This is another local variable printf("%d\n", iLocal); } UniMAP SemII-11/12 EKT120: Computer Programming

Local Variable : Example The output will be: 3 5 UniMAP SemII-11/12 EKT120: Computer Programming

EKT120: Computer Programming End Week 5 – Functions (1) Q & A! UniMAP SemII-11/12 EKT120: Computer Programming