Functions. Functions. Identify Repeated Code. Identify Repeated Code. Identify Similar Code. Use Parameters to Customize 2/25/14

Similar documents
Chapter 9: Functions. Chapter 9. Functions. Copyright 2008 W. W. Norton & Company. All rights reserved.

COP 3275: Chapter 09. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

Declaration Syntax. Declarations. Declarators. Declaration Specifiers. Declaration Examples. Declaration Examples. Declarators include:

CSCI 2132 Software Development. Lecture 19: Generating Permutations

Chapter 12: Pointers and Arrays. Chapter 12. Pointers and Arrays. Copyright 2008 W. W. Norton & Company. All rights reserved.

Programming for Electrical and Computer Engineers. Pointers and Arrays

Fundamentals of Programming Session 12

OBJECTIVE QUESTIONS: Choose the correct alternative:

CSCI 2132 Software Development. Lecture 17: Functions and Recursion

Weeks 10/11 C Preprocessor Macros and Functions

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

CSCI 2132 Software Development. Lecture 20: Program Organization

CSE 2421: Systems I Low-Level Programming and Computer Organization. Functions. Presentation C. Predefined Functions

BSM540 Basics of C Language

Pointers. Pointer Variables. Chapter 11. Pointer Variables. Pointer Variables. Pointer Variables. Declaring Pointer Variables

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

Lecture 04 FUNCTIONS AND ARRAYS

CSCI 2132 Software Development. Lecture 18: Functions

Sorting: Quick Sort. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

CS240: Programming in C

Statements. Control Flow Statements. Relational Operators. Logical Expressions. Relational Operators. Relational Operators 1/30/14

Pointers and Arrays. adopted from KNK C Programming : A Modern Approach

User Defined Functions

Slide Set 3. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Goals of this Lecture

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

CSCI 2132 Software Development Lecture 18: Implementation of Recursive Algorithms

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

// walk through array stepping by step amount, moving elements down for (i = unsorted; i >= step && item < a[i-step]; i-=step) { a[i] = a[i-step];

Functions. Computer System and programming in C Prentice Hall, Inc. All rights reserved.

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Slide Set 3. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Functions. (transfer of parameters, returned values, recursion, function pointers).

It is necessary to have a single function main in every C program, along with other functions used/defined by the programmer.

Maltepe University Computer Engineering Department. BİL 133 Algoritma ve Programlama. Chapter 8: Arrays and pointers

EECE.2160: ECE Application Programming Spring 2018

Slide Set 1. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

Functions. Arash Rafiey. September 26, 2017

C Programming Language

Chapter 7 Functions. Now consider a more advanced example:

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

What are the most likely declarations of "month" in the old and new versions of the program?

Subject: Fundamental of Computer Programming 2068

MAHALAKSHMI ENGINEERING COLLEGE B TIRUCHIRAPALLI

ECE264 Fall 2013 Exam 1, September 24, 2013

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0)

Computer Science & Engineering 150A Problem Solving Using Computers

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 4

C: How to Program. Week /Apr/23

Grade Distribution. Exam 1 Exam 2. Exams 1 & 2. # of Students. Total: 17. Total: 17. Total: 17

In addition to name. Each variable in C program is characterized by its Type Scope Storage Class. Type Have already discussed

C Programming Language

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information.

Array Initialization

CS240: Programming in C

CS125 : Introduction to Computer Science. Lecture Notes #38 and #39 Quicksort. c 2005, 2003, 2002, 2000 Jason Zych

UNIT 3 FUNCTIONS AND ARRAYS

Computer Programming: C++

A Fast Review of C Essentials Part II

APS105. Collecting Elements 10/20/2013. Declaring an Array in C. How to collect elements of the same type? Arrays. General form: Example:

Weeks 6&7: Procedures and Parameter Passing

C Programming Lecture V

Structured Programming. Functions and Structured Programming. Functions. Variables

This is CS50. Harvard University Fall Quiz 0 Answer Key

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Functions. Systems Programming Concepts

2-D Arrays. Of course, to set each grid location to 0, we have to use a loop structure as follows (assume i and j are already defined):

CS1150 Principles of Computer Science Methods

CS 0449 Sample Midterm

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

CS 223: Data Structures and Programming Techniques. Exam 2

Fundamental of Programming (C)

Chapter 2: Overview of C. Problem Solving & Program Design in C

Chapter 11 Introduction to Programming in C

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Unit 7. Functions. Need of User Defined Functions

Carleton University Department of Systems and Computer Engineering SYSC Foundations of Imperative Programming - Winter 2012

Computer Science & Engineering 150A Problem Solving Using Computers

Chapter 8: Function. In this chapter, you will learn about

CS16 Exam #1 7/17/ Minutes 100 Points total

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Lecture 9 - C Functions

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

Function Call Stack and Activation Records

Chapter 11 Introduction to Programming in C

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

C: How to Program. Week /Apr/16

Variation of Pointers

Lecture 24 Tao Wang 1

CSC 270 Survey of Programming Languages. What is a Pointer?

Government Polytechnic Muzaffarpur.

Week 3 More Formatted Input/Output; Arithmetic and Assignment Operators

Functions. Functions are everywhere in C. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

Programming Language A

Introduction. C provides two styles of flow control:


Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

Declaration of Statically-Allocated Arrays

Transcription:

Functions Functions Based on slides from K. N. King and Dianna Xu Bryn Mawr College CS246 Programming Paradigm Function: Unit of operation o A series of statements grouped together with a given name Must have the main function C functions are stand-alone Most programs contain multiple function definitions o Must be declared/defined before being used Identify Repeated Code printf("=== Expert System ===\n"); printf("question1:...\n"); printf( "1. Yes\n" "0. No\n" "Enter the number corresponding to your choice: "); if (choice == 1) /* yes */ printf("question 2:...\n"); printf( "1. Yes\n" "0. No\n" "Enter the number corresponding to your choice: "); /* skipped */ Identify Repeated Code int menuchoice() printf( "1. Yes\n" "0. No\n" "Enter the number corresponding to your choice: "); return choice; printf("=== Expert System ===\n"); printf("question1:...\n"); choice = menuchoice(); if (choice == 1) /* yes */ printf("question 2:...\n"); choice = menuchoice(); /* skipped */ Identify Similar Code double km, mile; switch (choice) case 1: printf("enter a mile value: "); scanf("%lf", &mile); km = mile * 1.6; printf("%f mile(s) = %f km\n", mile, km); caes 2: printf("enter a km value: "); scanf("%lf", &km); mile = km / 1.6; printf("%f km = %f mile(s)\n", km, mile); default: printf("\n*** error: invalid choice ***\n"); Similar unit Similar unit Use Parameters to Customize void km_mile_conv(int choice) int input; printf("enter a %s value: ", choice==1?"mile":"km"); scanf("%lf", &input); if (choice == 1) printf("%f mile(s) = %f km(s)\n", input, input*1.6); else printf("%f km(s) = %f mile(s)\n", input, input/1.6); switch (choice) case 1: km_mile_conv(choice); caea 2: km_mile_conv(choice); /* more cases */ More readable main 1

Function-oriented C came before OO concept C program resemble java programs with a single giant class C is procedural o Program organization and modularization is achieved through function design o Carefully plan your function return type and parameter list o Write small functions! Function Call void km_to_mile() printf("enter a mile value: "); scanf("%lf", &mile); km = mile * 1.6; printf("%f mile(s) = %f km\n", mile, km); km_to_mile(); km_to_mile(); return 0; Function Return and Parameters The syntax for C functions is the same as Java methods void keyword can be omitted void km_to_mile(void) mile_to_km() Use of return in void Functions Exit from the function void getinput() while (1) switch (choice) case 1: /* some action */ case 0: return; /* exit from getinput */ The exit Function Executing a return statement in main is one way to terminate a program. Another is calling the exit function, which belongs to <stdlib.h>. The statement return expression; in main is equivalent to exit(expression); To indicate normal termination, we d pass 0: exit(0); /* normal termination */The difference between return and exit is that exit causes program termination regardless of which function calls it. The return statement causes program termination only when it appears in the main function. Function Prototype A prototype is a function declaration which includes the return type and a list of parameters A way to move function definitions after main Need not name formal parameters /* function prototypes */ double km2mile(double); double mile2km(double); /* actual function definitions */ double km2mile(double k) double mile2km(double m) 2

When a function parameter is a one-dimensional array, the length of the array can be left unspecified: int f(int a[]) /* no length specified */ We can supply the length if the function needs it as an additional argument. Example: int sum_array(int a[], int n) int i, sum = 0; for (i = 0; i < n; i++) sum += a[i]; return sum; Since sum_array needs to know the length of a, we must supply it as a second argument. The prototype for sum_array has the following appearance: int sum_array(int a[], int n); We can omit the parameter names if we wish: int sum_array(int [], int); When sum_array is called, the first argument will be the name of an array, and the second will be its length: #define LEN 100 int main(void) int b[len], total; total = sum_array(b, LEN); Notice that we don t put brackets after an array name when passing it to a function: total = sum_array(b[], LEN); /*** WRONG ***/ Suppose that we ve only stored 50 numbers in the b array, even though it can hold 100. We can sum just the first 50 elements by writing total = sum_array(b, 50); Be careful not to tell a function that an array argument is larger than it really is: total = sum_array(b, 150); /*** WRONG ***/ sum_array will go past the end of the array, causing undefined behavior. A function is allowed to change the elements of an array parameter, and the change is reflected in the corresponding argument. A function that modifies an array by storing zero into each of its elements: void store_zeros(int a[], int n) int i; for (i = 0; i < n; i++) a[i] = 0; 3

If a parameter is a multidimensional array, only the length of the first dimension may be omitted. If we revise sum_array so that a is a two-dimensional array, we must specify the number of columns in a: #define LEN 10 int sum_two_dimensional_array(int a[][len], int n) int i, j, sum = 0; for (i = 0; i < n; i++) for (j = 0; j < LEN; j++) sum += a[i][j]; return sum; The return Statement A non-void function must use the return statement to specify what value it will return. The return statement has the form return expression ; The expression is often just a constant or variable: return 0; return status; More complex expressions are possible: return n >= 0? n : 0; The exit Function Executing a return statement in main is one way to terminate a program. Another is calling the exit function, which belongs to <stdlib.h>. The argument passed to exit has the same meaning as main s return value: both indicate the program s status at termination. To indicate normal termination, we d pass 0: exit(0); /* normal termination */ The statement The exit Function return expression; in main is equivalent to exit(expression); The difference between return and exit is that exit causes program termination regardless of which function calls it. The return statement causes program termination only when it appears in the main function. Local/Global Variables Variables declared inside a function are local Function arguments are local to the function passed to A global variable is a variable declared outside of any function. In a name conflict, the local variable takes precedence When local variable shadows function parameter? int x = 0; int f(int x) int x = 1; return x; int x; x = f(2); Local Variables Since C99 doesn t require variable declarations to come at the beginning of a function, it s possible for a local variable to have a very small scope: 4

Scope of Global Variables The scope of a global variable starts at the point of its definition. Globals should be used with caution o Avoid changing a global inside a function o Change a global by setting it the return value of a function o If using globals at all, declare them at the top. int x; int f() int y; int g() Call by Value Same as Java, modification to function arguments during function execution has no effect outside of function void f(int x) x = x * x; printf("%d", x); int x = 3; f(x); printf("%d", x); return 0; The variable x in f gets a copy of the value of the variable x in main. Does not change the value of x in main. Storage Classes auto o The default life time is the defining function o De-allocated once function exits static (w.r.t. local variables) o Life time is the entire program defined and initialized the first time function is called only o Scope remains the same Scope In a C program, the same identifier may have several different meanings. The most important scope rule: When a declaration inside a block names an identifier that s already visible, the new declaration temporarily hides the old one, and the identifier takes on a new meaning. At the end of the block, the identifier regains its old meaning. void f() static int counter = 0; counter++; Scope In the example on the previous slide, the identifier i has four different meanings: o In Declaration 1, i is a variable with static storage duration and file scope. o In Declaration 2, i is a parameter with block scope. o In Declaration 3, i is an automatic variable with block scope. o In Declaration 4, i is also automatic and has block scope. C s scope rules allow us to determine the meaning of i each time it s used (indicated by arrows). 5

static: globals and functions Using the keyword static in front of a global or a function changes the linkage, that is, the scope across multiple files. static changes the linkage of an identifier to internal, which means shared within a single (the current) file We will discuss more of linkage and related keywords, as well as header files when we discuss multiple source files Documenting Functions A comment for each function Use descriptive function name, parameter names #include <stdio.h> #include <math.h> /* truncate a value to specific precision */ double truncate(double val, int precision) double adj = pow(10, precision); int tmp; tmp = (int) (val * adj); return tmp / adj; Keep main Uncluttered Your main function should consist mainly of function calls One main input loop or conditional is okay Write your main and choose your function name in such a way so that o the main algorithm and program structure is clearly represented o the reader can get an idea how your program works simply by glancing at your main Recursion A function is recursive if it calls itself. The following function computes n! recursively, using the formula n! = n (n 1)!: int fact(int n) if (n <= 1) return 1; else return n * fact(n - 1); To see how recursion works, let s trace the execution of the statement i = fact(3); Recursion fact(3) finds that 3 is not less than or equal to 1, so it calls fact(2), which finds that 2 is not less than or equal to 1, so it calls fact(1), which finds that 1 is less than or equal to 1, so it returns 1, causing fact(2) to return 2 1 = 2, causing fact(3) to return 3 2 = 6. Recursion The following recursive function computes x n, using the formula x n = x x n 1. int power(int x, int n) if (n == 0) return 1; else return x * power(x, n - 1); 6

Recursion We can condense the power function by putting a conditional expression in the return statement: int power(int x, int n) return n == 0? 1 : x * power(x, n - 1); Both fact and power are careful to test a termination condition as soon as they re called. All recursive functions need some kind of termination condition in order to prevent infinite recursion. The Quicksort Algorithm Assume that the array to be sorted is indexed from 1 to n. Quicksort algorithm 1. Choose an array element e (the partitioning element ), then rearrange the array so that elements 1,, i 1 are less than or equal to e, element i contains e, and elements i + 1,, n are greater than or equal to e. 2. Sort elements 1,, i 1 by using Quicksort recursively. 3. Sort elements i + 1,, n by using Quicksort recursively. The Quicksort Algorithm Example of partitioning an array: Patitioning element Program: Quicksort The qsort.c program reads 10 numbers into an array, calls quicksort to sort the array, then prints the elements in the array: Enter 10 numbers to be sorted: 9 16 47 82 4 66 12 3 25 51 In sorted order: 3 4 9 12 16 25 47 51 66 82 The code for partitioning the array is in a separate function named split. qsort.c /* Sorts an array of integers using Quicksort algorithm */ #include <stdio.h> #define N 10 void quicksort(int a[], int low, int high); int split(int a[], int low, int high); int main(void) int a[n], i; printf("enter %d numbers to be sorted: ", N); for (i = 0; i < N; i++) scanf("%d", &a[i]); quicksort(a, 0, N - 1); printf("in sorted order: "); for (i = 0; i < N; i++) printf("%d ", a[i]); printf("\n"); return 0; void quicksort(int a[], int low, int high) int middle; if (low >= high) return; middle = split(a, low, high); quicksort(a, low, middle - 1); quicksort(a, middle + 1, high); 7

Lab Understanding Recursion int split(int a[], int low, int high) int part_element = a[low]; for (;;) while (low < high && part_element <= a[high]) high--; if (low >= high) a[low++] = a[high]; while (low < high && a[low] <= part_element) low++; if (low >= high) a[high--] = a[low]; a[high] = part_element; return high; Given an array of 2n integers in the following format a1 a2 a3 an b1 b2 b3 bn. Shuffle the array to a1 b1 a2 b2 a3 b3 an bn without any extra memory. Assumption: n=2 i where i = 0, 1, 2, 3, etc. Algorithm (hint: use recursion)? Implement your algorithm. Print out running traces for each recursive call. 8