1 Lecture04: Function Slides modified from Yin Lou, Cornell CS2022: Introduction to C.

Slides:



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

Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Methods Java 5.1 A quick overview of methods
Functions. COMP104 Functions / Slide 2 Introduction to Functions * A complex problem is often easier to solve by dividing it into several smaller parts,
1 ICS103 Programming in C Lecture 5: Introduction to Functions.
CS 201 Functions Debzani Deb.
Functions. COMP104 Lecture 13 / Slide 2 Review of Array: Bubble Sort for (j=0; j List[j+1]) swap(List[j], List[j+1]); }
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
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:
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
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.
CSC 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Functions Gabriel Hugh Elkaim Spring 2012.
CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus.
1 Lecture04: Basic Types & Function Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
1 計算機程式設計 Introduction to Computer Programming Lecture01: Introduction and Hello World 9/10/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
 Using functions  Writing functions  Basics  Prototypes  Parameters  Return types  Functions and memory  Pointer parameters.
C Programming Lecture 8-2 : Function (advanced). Recursive Function (recursion) A function that calls itself (in its definition) Classic example : factorial.
1 ICS103 Programming in C Lecture 7: Introduction to Functions.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
CS 100Lecture 211 Announcements P5 due on Thursday FINAL EXAM Tuesday August 10, 8AM, Olin 155 Review sessions on Thursday and Friday Final grades posted.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
Administrative things
PHY 107 – Programming For Science. Announcements  Lectures may not cover all material from readings  Material that is most difficult or challenging.
Chapter 5 Modular Design and Function C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Functions. Flow of Control Review while for do while goto break & continue switch Relational operators short circuits.
1 Lecture03: Control Flow 9/24/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
APS105 Functions (and Pointers) 1. Modularity –Break a program into manageable parts (modules) –Modules interoperate with each other Benefits of modularity:
C Part 2 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens The Three Attributes of an Identifier Identifiers have three essential.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Introduction to Programming
CSE 220 – C Programming Pointers.
Administrative things
FUNCTIONS In C++.
The Three Attributes of an Identifier
A Lecture for the c++ Course
Programming Fundamentals Lecture #7 Functions
Deitel- C:How to Program (5ed)
Administrative things
Chapter 5 - Functions Outline 5.1 Introduction
User-defined Functions
Scope, Parameter Passing, Storage Specifiers
Functions and Program Structure
User-defined Functions
Simulating Reference Parameters in C
In C Programming Language
CS150 Introduction to Computer Science 1
CS1100 Computational Engineering
Introduction to Computing Lecture 08: Functions (Part I)
ETE 132 Lecture 6 By Munirul Haque.
Administrative things
Administrative things
Functions that return a value
Presentation transcript:

1 Lecture04: Function Slides modified from Yin Lou, Cornell CS2022: Introduction to C

Administrative things Assignment #4 is on the course webpage due next week. 2

Review from Last Week if, else-if ? : switch, case, default, break for ( ; ; ) while ( ) do while ( ); break, continue 3

Math Functions Many math functions are defined in math.h –pow(a, b) - Compute ab –exp(a) - Compute ea –log(a) - Compute natural logarithm –log10(a) - Compute common logarithm –sqrt(a) - Compute square root –fabs(a) - Compute absolute value –ceil/oor - Round up/down value –cos, sin, tan –acos, asin, atan 4

Functions Purpose of functions –Breaks a program into pieces that are easier to understand –Makes recursive algorithms easier to implement –Promotes code reuse Disadvantage of functions –Function calls add some memory and time overhead Functions in C –Similar to methods in Java –But C functions do not belong to a class. Every function is visible everywhere in the program. 5

A Simple Function Compute base exp 6 int power(int base, int exp) { int i, p = 1; for (i = 1; i <= exp; ++i) { p *= base; // p = p * base; } return p; }

Simple Function in Context #include int power(int base, int exp); // function prototype or declaration void main() { int i = 3, j = 4; printf("%d^%d is %d.\n", i, j, power(i, j));// function call } int power(int base, int exp) // function definition { int i, p = 1; for (i = 1; i <= exp; ++i) { p *= base; } return p; } 7

Function Return Values If a function returns type void, then no return statement is needed. If a function returns another type, then a return statement is required along all possible execution paths. What’s wrong with this? 8 #include int foo(int arg) { if (arg == 1) { return 1; } void main() { printf("%d\n", foo(0)); }

Call by Value Function arguments in C are passed by value –The value of the argument is passed, not a reference –Functions are given a new copy of their arguments –So a function can't modify the value of a variable in the calling function (unless you use pointers) Example 9 #include int foo(int a) { a = 3; return a; } void main() { int a = 1, b; b = foo(a); printf("%d %d\n", a, b); // Output 1 3 }

Call by Value: Example #include void swap(int a, int b) { int t = a; a = b; b = t; } void main() { int a = 1, b = 2; swap(a, b); printf("%d %d\n", a, b); // Output 1 2 } 10

Call by Value: Tradeoff Call by value has advantages and disadvantages Advantage: some functions are easier to write 11 int power(int base, int exp) { int result = 1; for (; exp >= 1; exp = exp - 1) { result *= base; } return result; }

Call by Value: Tradeoff Disadvantage: sometimes you’d like to modify an argument (e.g. swap() function) –What’s wrong with this? –We’ll see how to do this using pointers later 12 void swap (int x, int y) { int temp = x; x = y; y = temp; }

Recursion int fact(int n) { if (n == 0) { return 1; } else { return n * fact(n - 1); } 13

Declaration and Definition Declaration –A declaration announces the properties of a variable (primarily its type). –Example: extern int n; extern double val[]; Definition –A definition also causes storage to be set aside. –Example: int n; double val[MAX LEN]; 14

Manage Your Project It's always recommended to modularize your project. How? Write functions and paste them in new file? Definitions and declarations are shared among many source files. How to centralize this, so that there is only one copy to get and keep right as the program evolves? We could use header files. 15

Header File Place common material in a header file. Can be included as needed. 16 Example: mymath.h int fact(int n); int power(int power, int exp);

Example power.c #include "mymath.h" int power(int base, int exp) { int result = 1; int i; for (i = 1; i <= exp; ++i) { result *= base; } return result; } fact.c #include "mymath.h" int fact(int n) { if (n == 0) { return 1; } else { return n * fact(n - 1); } 17

Example main.c #include #include "mymath.h" void main() { printf("%d\n", power(5, 3)); printf("%d\n", fact(5)); } 18

How to compile in CBs? TA will talk about this 19