Functions / Procedures

Slides:



Advertisements
Similar presentations
Lectures 10 & 11.
Advertisements

 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
Engineering Computing I Chapter 1 – Part B A Tutorial Introduction continued.
1 Homework Assignments Turn in HW1 (If not done yet, catch up!) Questions about HW1? Anyone still stuck on apply / UNIX account? Everyone have the books?
COMP1180 Review Date: 4 March, 2009 Time: 10:30am - 12:20pm Venue: –CS students -- FSC801C and FSC801D –IS and other students -- OEE1017 Remarks: – 1)
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
1 Review of Class on Oct Outline of Chapter 4  How to write a function?  Function Prototypes  Function Invocation  Function Definition  The.
流程控制: while loop 迴圈 Test condition Enter loop Yes (non-0) Execute Loop body no exit F=0 F=F+20 … F=F
Imperative Programming Prof. Béat Hirsbrunner Amine Tafat, PhD Student Matthias Buchs and Raphaël Lesceux, Graduate Students Department of Informatics.
0 Arrays (1/2) #include /* count digits, white space, others */ main() { int c, i, nwhite, nother; int ndigit[10]; nwhite = nother = 0; for (i = 0; i
CS100A, Fall 1997, Lectures 221 CS100A, Fall 1997 Lecture 22, Tuesday 18 November Introduction To C Goal: Acquire a reading knowledge of basic C. Concepts:
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Computer Science 210 Computer Organization Introduction to C.
C Programming A Modern Approach
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
CECS 121 EXAM 1. /* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
By Sidhant Garg.  C was developed between by Dennis Ritchie at Bell Laboratories for use with the Unix Operating System.  Unlike previously.
Programming I Introduction Introduction The only way to learn a new programming language is by writing programs in it. The first program to.
Programming Language  C Tutorial Introduction 主講人:虞台文.
1/16 Programski jezik C Vladimir Filipović
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
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)
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 4.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
Computer programming Outline Functions [chap 8 – Kochan] –Defining a Function –Arguments and Local Variables Automatic Local.
1 Functions  A function is a named, independent section of C++ code that performs a specific task and optionally returns a value to the calling program.
FUNCTION Dong-Chul Kim BioMeCIS UTA 12/7/
CCSA 221 Programming in C CHAPTER 8 – PART 1 WORKING WITH FUNCTIONS 1.
1 CSE1301 Computer Programming Lecture 13 Functions (Part 1)
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
USER DEFINED FUNCTIONS Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?
Programming Language  C Functions and Program Structure 主講人:虞台文.
Administrative things
1 Unstructured Programming The Need for Procedures/Functions Procedural Programming Function Declaration/Prototypes/Invocation Example Functions Function.
Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd.
CSC Programming for Science Lecture 8: Character Functions.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Characters and Strings Functions.
CS 1704 Introduction to Data Structures and Software Engineering.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
Objectives: How to define and call functions. Function declarations and how they differ from function definitions. How arguments are passed to functions.
Functions, Part 2 of 2 Topics Functions That Return a Value
Principles of programming languages 4: Parameter passing, Scope rules
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions I Creating a programming with small logical units of code.
CSI 121 Structured Programming Language Lecture 13 Functions (Part 1)
Functions, Part 1 of 3 Topics Using Predefined Functions
Functions, Part 2 of 3 Topics Functions That Return a Value
INC 161 , CPE 100 Computer Programming
Your questions from last session
Conversion Check your class notes and given examples at class.
Functions, Part 1 of 3 Topics Using Predefined Functions
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions I Creating a programming with small logical units of code.
Functions, Part 2 of 3 Topics Functions That Return a Value
CPS125.
Presentation transcript:

Functions / Procedures In C, a function is equivalent to a procedure or a function in Pascal A function provides a convenient way to encapsulate some computation, which can then be used without worrying about its implementation Main idea: “To ignore how a job is done; knowing what is done is sufficient” Functions we have used so far: printf, scanf, getchar, putchar Statement structure return-type function-name (parameters declarations, if any) { declarations statements return expression; }

Simple functions #include <stdio.h> void prn_message( ); { printf(“Message for you: “); printf(“Have a nice day!\n”); } int main( ) prn_message( ); return 0; #include <stdio.h> void prn_message(int n); void prn_message(int n) { int i; printf(“Message for you: “); for (i = 0; i < n; i++) printf(“%d. Cheers :D \n”, i); printf(“Have a nice day!\n”); } int main( ) int n; printf(“How many CHEERS? “); scanf(“%d”, &n); prn_message(n); return 0;

Function power(x, y) #include <stdio.h> int power(int x, int y); /* declares the parameters types and names */ int main() { int i; for (i = 0; i < 10; i++) printf(“%d %d %d\n”, i, power(2, i), power(-3,i)); return 0; } /* Power: raise base to n-th power; n >=0 */ int power(int base, int n) int i, p; p = 1; for (i = 1; i <= n; i++) p = p * base; return p;

Something related to functions Main program is a function, too Function definitions can appear in any order What are parameters, formal & actual arguments Formal arguments are local variables The scope of the variables A function need not return a value, a return statement with no expression cause control, but no useful value to be returned to the caller The function-type -- the data type when the function returns Return-types conversion

Functions C2F(int), and Function F2C(int) #include <stdio.h> /* Instead of float C2F(int x); */ float C2F(int); float F2C(int); int main() { float tmp; int i; for (i = 0; i < 100; i++){ tmp = C2F(i); printf(“%d C = %f F\n”, i, tmp); } for (i = 0; i < 213; i++){ tmp = F2C(i); printf(“%d F = %f C\n”, i, tmp); return 0; float C2F(int C) { float tmp; tmp = (C * 9.0 / 5.0) + 32.0; return tmp; } float F2C(int F) tmp = (F - 32.0) * 5.0 / 9.0;

The scope of the variables #include <stdio.h> void proc1(int a, int b); void proc2(int a, int b); int i = 0, j = 0; int main(){ int i; printf("i=%d j=%d\n", i, j); i = 5; j = 10; proc1(i, j); proc2(i, j); return 0; } void proc1(int a, int b){ printf("i=%d j=%d a=%d b=%d\n", i, j, a, b); a = 15; b = 20; i = 25; j = 30; void proc2(int a, int b){ int j = 35; a = 40; b = 45; i = 50; j = 55; Output: i=2147307520 j=0 i=5 j=10 i=0 j=10 a=5 b=10 i=25 j=30 a=15 b=20 i=5 j=30 i=25 j=35 a=5 b=30 i=50 j=55 a=40 b=45

Arguments -- Call by value In C, all function arguments are passed by value The called function is given the values of its arguments in temporary variables rather than the originals #include <stdio.h> Program Output: void try_to_change(int x); i = 5 int main() x = 5 { x = 10 int i = 5; i = 5 printf(“i = %d\n”, i); try_to_change(i); return 0; } void try_to_change(int x) { printf(“x = %d\n”, x); x = 10;

Advantage for call by value int power(int base, int n) int power(int base, int n) { { int i, p; int p; p = 1; for (p = 1; n > 0; n--) for (i = 1; i <= n; i++) p = p * base; p = p * base; return p; return p; } } Structure Programming (char arrays & function) Problem: To write a program that reads a set of text lines and prints the longest line out. Using Top-Down approach: 1. declarations and initializations 2. reading and text processing 3. printing out the longest line

Refinements 2. Processing the text 2.2 (refinement) 2.2.1 (refinement) 2.1 While there is another line do 2.2 The corresponding actions when you find a longer line 2.2 (refinement) 2.2.1 if (it’s longer than the previous longest line) do 2.2.1 (refinement) 2.2.1.1 save its length 2.2.1.2 save the line for later printing After we all these refinements, we can build our program by substituting the smallest refinements by C code Functions or procedures could be used to encapsulate some computations

Final Coding #include <stdio.h> int getline(char s[], int lim) #define MAXLINE 1000 /* max. input line size */ { int getline(char line[], int maxline); int c, i; void copy(char to[], char from[]); for (i = 0; i < lim-1 && /* print longest input line */ (c = getchar()) != EOF && int main() c != ‘\n’; i++) { s[i] = c; int len; if (c == ‘\n’){ int max; s[i] = c; char line[MAXLINE]; i++; char longest[MAXLINE]; } max = 0; s[i] = ‘\0’; while ((len = getline(line, MAXLINE)) > 0) return i; if (len > max){ } max = len; copy(longest, line); } if (max > 0) printf(“%s”, longest); return 0; void copy(char to[], char from[]) { int i = 0; while ((to[i] = from[i]) != ‘\0’) i++;

Function atoi() Converting a string to its number equivalent. (atoi) Steps: 1. Skip white space, if any 2. Get sign, if any 3. Get integer part and convert it #include <ctype.h> n = 0; int atoi(char s[]) while (isdigit(s[i])){ { n = n * 10 + (s[i] - ‘0’); int i, n, sign; i++; i = 0; } while (isspace(s[i])) i++; return sign * n; if (s[i] == ‘-’){ } sign = -1; i++; }else{ sign = 1; if (s[i] == ‘+’) i++; }

Function reverse() Reverse: reverse string S in place #include <string.h> void reverse(char s[]) { int c, i, j; i = 0; j = strlen(s) - 1; while ( i < j){ c = s[i]; s[i] = s[j]; s[j] = c; i++; j--; }