Introduction to C Functions and Make Instructor Yin Lou 01 28 2011 Introduction to C CS 2022 Spring 2011 Lecture 3 Math Functions I Many math functions are defined in math h Introduction to C CS 2022 Spring 2011 Lecture 3 Math Functions I Many math functions are defined in math h I I I I I I I I I pow a b Compute ab exp a Compute e a log a Compute natural logarithm log10 a Compute common logarithm sqrt a Compute square root fabs a Compute absolute value ceil floor Round up down value cos sin tan acos asin atan Introduction to C CS 2022 Spring 2011 Lecture 3 Functions I Purpose of functions Introduction to C CS 2022 Spring 2011 Lecture 3 Functions I Purpose of functions I I I Breaks a program into pieces that are easier to understand Makes recursive algorithms easier to implement Promotes code reuse Introduction to C CS 2022 Spring 2011 Lecture 3 Functions I Purpose of functions I I I I Breaks a program into pieces that are easier to understand Makes recursive algorithms easier to implement Promotes code reuse Disadvantage of functions Introduction to C CS 2022 Spring 2011 Lecture 3 Functions I Purpose of functions I I I I Breaks a program into pieces that are easier to understand Makes recursive algorithms easier to implement Promotes code reuse Disadvantage of functions I Function calls add some memory and time overhead Introduction to C CS 2022 Spring 2011 Lecture 3 Functions I Purpose of functions I I I I Disadvantage of functions I I Breaks a program into pieces that are easier to understand Makes recursive algorithms easier to implement Promotes code reuse Function calls add some memory and time overhead Functions in C Introduction to C CS 2022 Spring 2011 Lecture 3 Functions I Purpose of functions I I I I Disadvantage of functions I I Breaks a program into pieces that are easier to understand Makes recursive algorithms easier to implement Promotes code reuse Function calls add some memory and time overhead Functions in C I I Similar to methods in Java But C functions do not belong to a class Every function is visible everywhere in the program Introduction to C CS 2022 Spring 2011 Lecture 3 A Simple Function Compute base exp int power int base int exp int i p 1 for i 1 i exp i p base return p Introduction to C CS 2022 Spring 2011 Lecture 3 Simple Function in Context include stdio h int power int base int exp function prototype void main function definition int i 3 j 4 function call printf d d is d n i j power i j int power int base int exp function definition int i p 1 for i 1 i exp i p base return p Introduction to C CS 2022 Spring 2011 Lecture 3 Function Return Values I If a function returns type void then no return statement is needed I If a function returns another type then a return statement is required along all possible execution paths Introduction to C CS 2022 Spring 2011 Lecture 3 Function Return Values I If a function returns type void then no return statement is needed I If a function returns another type then a return statement is required along all possible execution paths What does this code do include stdio h int foo int arg if arg 1 return 1 void main printf d n foo 0 Introduction to C CS 2022 Spring 2011 Lecture 3 Call by Value I Function arguments in C are passed by value Introduction to C CS 2022 Spring 2011 Lecture 3 Call by Value I Function arguments in C are passed by value I I I 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 Introduction to C CS 2022 Spring 2011 Lecture 3 Call by Value I Function arguments in C are passed by value I I I 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 include stdio h 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 Introduction to C CS 2022 Spring 2011 Lecture 3 Call by Value Example include stdio h 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 12 Introduction to C CS 2022 Spring 2011 Lecture 3 Call by Value I Call by value has advantages and disadvantages Introduction to C CS 2022 Spring 2011 Lecture 3 Call by Value I Call by value has advantages and disadvantages I Advantage some functions are easier to write Introduction to C CS 2022 Spring 2011 Lecture 3 Call by Value I Call by value has advantages and disadvantages I Advantage some functions are easier to write int power int base int exp int result 1 for exp 1 exp result base return result Introduction to C CS 2022 Spring 2011 Lecture 3 Call by Value I Call by value has advantages and disadvantages I Advantage some functions are easier to write int power int base int exp int result 1 for exp 1 exp result base return result I Disadvantage sometimes youd like to modify an argument e g swap function I Well see how to do this using pointers later Introduction to C CS 2022 Spring 2011 Lecture 3 Recursion Example int fact int n if n 0 return 1 else return n fact n 1 Introduction to C CS 2022 Spring 2011 Lecture 3 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 Introduction to C CS 2022 Spring 2011 Lecture 3 Manage Your Project I It s always recommended to modularize your project How Introduction to C CS 2022 Spring 2011 Lecture 3 Manage Your Project I It s always recommended to modularize your project How I Write functions and paste them in new file Introduction to C CS 2022 Spring 2011 Lecture 3 Manage Your Project I It s always recommended to modularize your project How I Write functions and paste them in new file I Definitions and decelerations are shared among a lot of source files How to centralize this so that there is only one copy to get and keep right as the program evolves Introduction to C CS 2022 Spring 2011 Lecture 3 Manage Your Project I It s always recommended to modularize your project How I Write functions and paste them in new file I Definitions and decelerations are shared among a lot …
View Full Document
Unlocking...