UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Functions (1)

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.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 3: Functions.
Chapter 6: User-Defined Functions I
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
1. Function prototype Function prototype is a declaration; indicates the function exists Should have function name, return type and parameter Placed before.
CPS120: Introduction to Computer Science Decision Making in Programs.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
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.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
CSCI 171 Presentation 8 Built-in Functions, Preprocessor Directives, and Macros.
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;
User defined functions
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.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
UniMAP Sem2-09/10 DKT121:Fundamental of Computer Programming1 Functions (2)
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 06 FUNCTIONS 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER) 1.
1 ICS103 Programming in C Lecture 8: Functions I.
EC201- FUNCTIONS CONTROL STATEMENT DTK, JKE, PTSB (V 10.12)
Functions. Why use functions? They can break your problem down into smaller sub-tasks (modularity).  easier to solve complex problems They make a program.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Principle Prog Revision. Question 1 (a) Identify errors in the following program segment and how the errors can be corrected. void main(){ constant int.
Functions The fruitful & parameterized functions.
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
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.
C Short Overview Lembit Jürimägi.
PGT 106: Computer Programming
Week 5 – Functions (1) EKT120: Computer Programming.
Programming Paradigms
EKT120: Computer Programming
CSCI 161: Introduction to Programming Function
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
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
Chapter 6: User-Defined Functions I
Functions, Part 1 of 3 Topics Using Predefined Functions
In C Programming Language
Introduction to Problem Solving and Programming
Fundamental Programming
Functions Department of Computer Science-BGU יום שישי 26 אפריל 2019.
Functions Imran Rashid CTO at ManiWeber Technologies.
Programming Languages and Paradigms
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:

UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Functions (1)

UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming2 Outline Why use functions? Functions in C Pre-defined functions User-defined functions Function prototype Function definition Function call 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 Sem2-10/11 DKT121: Fundamental of Computer Programming3 Why use functions? Let say you want to print one row of number 8 and one row of number 9 #include int main() {int i, j; //print one row of number 8 for(i=1; i<=10; i++) printf(“8"); printf("\n"); //go to new line //print one row of number 9 for(j=1; j<=10; j++) printf(“9“); printf("\n"); //go to new line return 0; }

UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming4 Why use functions?(cont) 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 Sem2-10/11 DKT121: Fundamental of Computer Programming5 Why use functions?(cont) #include void display(int); //function prototype int main() { display(8); //function call display(9); //function call return 0; } void display(int value) //function definition { int i; for(i=1; i<=10; i++) printf("%d", value); printf("\n"); //go to new line }

UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming6 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 & 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 Sem2-10/11 DKT121: Fundamental of Computer Programming7 Predefined function (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 Sem2-10/11 DKT121: Fundamental of Computer Programming8 Predefined function (Library)- example #include int main() {string name; int vol1,vol2,n, R, kTemp, length; strcpy(name, “Marina”); vol2 = vol1 * exp(n * R * kTemp); length = strlen(“Mahathir”); }

UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming9 User-defined function What do we need to define and make use of user-defined function? Function prototype Function definition Function call

UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming10 Function prototype 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 have the following form: (arg_type arg_name,...); int sum (int num1,int num2); int sum (int,int); //is also acceptable have semicolon

UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming11 Function definition Function definition includes the body of a function Function definition have the following form: (arg_type arg_name,...) { … statements … } int sum (int num1,int num2) {int add; add = num1 + num2; return(add); }  Notice that argument name is used in the function body  Unlike function protoype, argument name in function definition must be included in function header no semicolon function header

UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming12 Function call Consists of a function name followed by argument expression list enclosed in parentheses Function call have the following form: (exp, exp...) exp is an expression – can be variable or constant result = sum(x,y);

UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming13 Example of function in program //This program sum up two numbers #include int sum(int, int);//function prototype int main() { int x,y, result; printf( “Enter x and y : “); scanf(“%d %d”, &x, &y); result = sum(x,y); //function call printf(“Sum is : %d”, result); return 0; } int sum(int num1, int num2) //function definition { int add; add = num1+num2; return(add); } function header

UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming14 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 Sem2-10/11 DKT121: Fundamental of Computer Programming15 What about number, order and type of parameter?(e.g1) 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 Sem2-10/11 DKT121: Fundamental of Computer Programming16 What about number, order and type of parameter?(e.g2) int sum(int, int);//function prototype int sum(int num1, int num2) //function definition sum(x,y); //function call Refer to program in slide 13 Number, order and type parameter is 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 Sem2-10/11 DKT121: Fundamental of Computer Programming17 Functions that do not return a value //This program sum up two numbers #include void sum_print(int, int); //function prototype void function1();//function prototype int main() { int x,y; function1(); //function call printf( “Enter x and y : “); scanf(“%d %d”, &x, &y); sum_print(x,y); //function call return 0; } void sum_print(int num1, int num2) //function definition { int add; add = num1+num2; printf(“Sum is : %d”, add); } void function1() {printf(“Welcome to this program\n”); }

UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming18 Functions that do not return a value (e.g.Orazio p271)

UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming19 Functions that return one value //This program sum up two numbers #include int sum(int, int);//function prototype int main() { int x,y, result; printf( “Enter x and y : “); scanf(“%d %d”, &x, &y); result = sum(x,y); //function call printf(“Sum is : %d”, result); return 0; } int sum(int num1, int num2) //function definition { int add; add = num1+num2; return(add); }

UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming20 Functions that return one value (e.g.Orazio p285)

UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming21 Miscellaneous about function1 Function call used as logical expression int calc(int,int); //prototype function int main(void) {int num1, num2; scanf(“%d %d”,&num1,&num2); if( calc(num1, num2) > 100) // function call used as logical expression cout <<“result greater than 100”; else cout <<“result less than 100”; return 0; } int calc(int n1, int n2) {int jwp; jwp=n1+n2; return(jwp); }

UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming22 Miscellaneous about function2 Function call used in printf stmt int calc(int,int); //prototype function int main(void) {int num1, num2; scanf(“%d %d”,&num1,&num2); printf(“Jawapan : %d “, calc(num1, num2)); //function call return a //value and put in printf return 0; } int calc(int n1, int n2) {int jwp; jwp=n1+n2; return(jwp); }

UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming23 Miscellaneous about function3 Rules regarding naming convention for variable num1 pass value to n1, num2 pass value to n2 Better use different variable name for parameters in main AND parameters in function definition int calc(int,int); //prototype function int main(void) {int num1, num2, result; //declare like this scanf(“%d %d”,&num1, &num2); result = calc( num1, num2); // function call printf(“jawapan : %d“,result); return 0; } //function definition int calc(int n1, int n2) //simply declare like this {int jwp; jwp=n1+n2; return(jwp); }

UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming24 Sample application Write a C program that calculates and print addition and subtraction of numbers. Your program should have function: add –adds two numbers subtract-subtract two numbers print_result-print results from calculation

UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming 25 Sample application(cont) #include int add(int,int); int subtract(int,int); void print_result(int); int main() {int num1,num2,answer; char op; printf(“Enter two numbers and operator:”); scanf(“%d %d %s”, &num1,&num2,&op); switch(op) {case ‘+’ : answer=add(num1,num2);break; case ‘-’ : answer=subtract(num1,num2);break; default: printf(“Invalid operator”);exit(0); } print_result(answer); return 0; } int add(int x,int y) { int sum; sum = x+y; return(sum); } int subtract(int x,int y) { int sub; sub=x-y; return(sub); } void print_result(int ans) { printf(“Answer is %d”, ans); }

UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming26 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 Sem2-10/11 DKT121: Fundamental of Computer Programming27 Scope and Mechanics of Passing Values to Functions

UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming28 Scope and Mechanics of Passing Values to Functions

UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming29 End Functions (1) Q & A!