USER DEFINED FUNCTIONS 350142 - Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.

Slides:



Advertisements
Similar presentations
C Functions. What are they? In general, functions are blocks of code that perform a number of pre-defined commands to accomplish something productive.
Advertisements

BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
Tutorial #7 Summer sizeof int main() { char x; sizeof(x); sizeof(int); }
Imperative Programming Prof. Béat Hirsbrunner Amine Tafat, PhD Student Matthias Buchs and Raphaël Lesceux, Graduate Students Department of Informatics.
Functions / Procedures
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
1 Introduction to Computers and Programming Class 3 Introduction to C Professor Avi Rosenfeld.
1 10/30/06CS150 Introduction to Computer Science 1 Functions Chapter 3, page 313.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
CECS 121 EXAM 1. /* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
FUNCTION – Microprocessor Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
IF-ELSE IF-ELSE STATEMENT SWITCH-CASE STATEMENT Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
RELATIONAL OPERATORS LOGICAL OPERATORS CONDITION Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
A First C Program /* Print a Message */ #include main() { printf("This is a test!\n"); } The program is compiled and run as cc pg32.c  a.out  This is.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
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.
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
Functions #include int sum(int i, int j); main() { int a, b, res; a = 1; b = 2; res = sum(a, b); printf("%d + %d = %d\n", a, b, res); } int sum(int i,
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
DATA TYPE AND DISPLAY Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
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;
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
1 Unstructured Programming The Need for Procedures/Functions Procedural Programming Function Declaration/Prototypes/Invocation Example Functions Function.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
What we will cover A crash course in the basics of C “Teach yourself C in 21 days”
Functions Programming Applications. Functions every C program must have a function called main program execution always begins with function main any.
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
Functions Dr. Sajib Datta Functions A function is a self-contained unit of program code designed to accomplish a particular task. Some functions.
Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
MULTI-DIMENSION ARRAY STRING Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Functions Students should understand the concept and basic mechanics of the function call/return pattern from CS 1114/2114, but some will not. A function.
EKT120: Computer Programming
MT262A Review.
C is readable... :-) What does this function do?
CSE 220 – C Programming Pointers.
Functions Dr. Sajib Datta
INC 161 , CPE 100 Computer Programming
Command-Line Arguments
Iteration statement while do-while
Programmazione I a.a. 2017/2018.
CSI-121 Structured Programming Language Lecture 16 Pointers
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
INC 161 , CPE 100 Computer Programming
Functions Declarations CSCI 230
2011/11/10: Lecture 21 CMSC 104, Section 4 Richard Chang
Functions.
Functions, Part 1 of 3 Topics Using Predefined Functions
Functions and Modular Programming
Functions, Part 2 of 3 Topics Functions That Return a Value
A function with one argument
Command Line Parameters
Outline Defining and using Pointers Operations on pointers
Functions, Part 1 of 3 Topics Using Predefined Functions
Functions Extra Examples.
ETE 132 Lecture 6 By Munirul Haque.
Functions, Part 1 of 3 Topics Using Predefined Functions
Iteration Statement for
FUNCTION ||.
Functions, Part 2 of 42 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
CPS125.
Presentation transcript:

USER DEFINED FUNCTIONS Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat

Structure of C Functions

How function works Input Output

User-defined functions type function-name (type arg-1, type arg-2, … ) { local variable declaration; statement-1; statement-2; …. statement-n; return(value); } Function’s output data type Function name Function’s input Local variable Statement in function Return statement

Function types  We can categorize functions in C by the way its take inputs and return outputs into 3 categories  Function that takes no input and returns nothing  Function that takes inputs and returns nothing  Function that takes inputs and returns an output

Function that takes no input and returns nothing  Function that takes no input and returns nothing means  Function does not take any arguments or parameters as input  Function does not return anything to user  It’s the easiest type of function that normally used for display some messages. void showProgram( ) { printf(“= Today Program =\n”); printf(“1. Mummy\n”); printf(“2. Star war\n”); printf(“3. Spiderman\n\n”); }

Example #include void showProgram( ) { printf(“=== Today Program ===\n”); printf(“1. Mummy\n”); printf(“2. Star war\n”); printf(“3. Spiderman\n\n”); } int main(int argc, char **argv) ‏ { char ch; printf("Do you want to check showing program (y/n) : "); ch = getchar( ); if((ch == ‘Y’) || (ch == ‘y’)) { showProgram( ); } else { printf(“Thank you\n”); }

Function that takes input and returns nothing  This type of function can accept 1 or more input parameters. void average(int x, int y) { printf(“Average of %d and %d = %f\n”, x, y, (x + y)/2.0); }

Example 1 #include void average(int x, int y) { printf(“Average of %d and %d = %f\n”, x, y, (x + y)/2.0); } int main(int argc, char **argv) ‏ { int a = 5, b = 10; average(a, b); } average Main 5 10 a b x y 5

Example 2 #include void average(int b, int a) { printf(“Average of %d and %d = %f\n”, b, a, (b + a)/2.0); } int main(int argc, char **argv) ‏ { int a = 5, b = 10; average(a, b); } average Main 5 10 a b b a 5

Example 3 #include void average(int x, int y) { printf(“Average of %d and %d = %f\n”, x, y, (x + y)/2.0); } int main(int argc, char **argv) ‏ { average((3+2), (100/10)); } average Main /10 x y 5 10

Example 4 #include void average(int a, int b) { a = 10; b = 20; } int main(int argc, char **argv) ‏ { int a = 5, b = 10; average(a, b); } average Main 5 10 a b a b 5 20

QUIZ 1 : Find the output of program #include void disp(int a) { int i; for(i = 1; i <= 12; i++) printf(“%d x %d = %d\n”, a, i, (a*i)); } int main(int argc, char **argv) ‏ { disp(5); }

QUIZ 2 : Find the output of program #include void plus5(int x) { x = x + 5; printf(“X in plus5 = %d\n”, x); } int main(int argc, char **argv) ‏ { int x = 10; printf(“X in main before calling function = %d\n”, x); plus5( x ); printf(“X in main after calling function = %d\n”, x); }

QUIZ 3 : Find the output of program #include void swap(int a, int b) { int tmp; tmp = a; a = b; b = tmp; printf(“In swap : A = %d, B = %d\n”, a, b); } int main(int argc, char **argv) ‏ { int a = 5, b = 10; printf(“Before swap A = %d, B = %d\n”, a, b); swap(a, b); printf(“After swap A = %d, B = %d\n”, a, b); }

Function that takes inputs and returns an output  This type of function is mostly used for calculating mathematics expression and return the result back to user.  Return value must be the same data type as the type of function.  Function can return only 1 value int power3(int x) { return( x * x * x); }

Example 1 #include int power3(int x) { return( x * x * x); } int main(int argc, char **argv) ‏ { int num; num = power3(2); printf(“2^3 = %d\n”, num); } power3 Main ? num x 2 8

Example 2 #include float average(int x, int y) { return( (x + y)/2.0 ); } int main(int argc, char **argv) ‏ { int a = 5, b = 10; float f; f = average(a, b); printf(“Average of %d and %d = %f\n”, a, b, f); } average Main 5 10 a b x y 5 ? f 7.5

QUIZ 4 #include int multiply(int a, int b) { return (a * b); } int main(int argc, char **argv) ‏ { int a = 5, b = 10; printf(“%d\n”, multiply(3,2)); printf(“%d\n”, multiply(a, 5)); printf(“%d\n”, multiply((3+2), (b – 6)); printf(“%d\n”, multiply( multiply(2,3), multiply(2, multiply(3, 2 -1)))); }

Function Prototype  Function prototype is the definition of function  We need to make C Compiler know the definition of functions before we can use them  Remember  #include  In stdio.h file contains the definition of functions that are related to input and output functions.  That’s why, we can use printf(), scanf(), etc..

Example #include int power3(int x) { return( x * x * x); } int main(int argc, char **argv) ‏ { printf(“2^3 = %d\n”, power3(2)); } #include int main(int argc, char **argv) ‏ { printf(“2^3 = %d\n”, power3(2)); } int power3(int x) { return( x * x * x); } CorrectIncorrect

int power3(int x) { Prototype Declaration  The way to write a prototype is like writing the first line of your function block  ตัวอย่าง type function-name (type arg-1, type arg-2, … ); int power3(int x) { return( x * x * x); } FunctionPrototype ;

Example #include int power3(int x) { return( x * x * x); } int main(int argc, char **argv) ‏ { printf(“2^3 = %d\n”, power3(2)); } #include int main(int argc, char **argv) ‏ { printf(“2^3 = %d\n”, power3(2)); } int power3(int x) { return( x * x * x); } CorrectIncorrect int power3(int x); Correct