Unformatted text preview:

C Functions Agenda What is a function Types of C functions Standard functions User defined functions C function structure Function signature Function body Declaring and Implementing C functions 2 Sharing data among functions through function parameters Value parameters Reference parameters Const reference parameters Scope of variables Local Variables Global variable Functions and subprograms The Top down design appeoach is based on dividing the main problem into smaller tasks which may be divided into simpler tasks then implementing each simple task by a subprogram or a function A C function or a subprogram is simply a chunk of C code that has A descriptive function name e g computeTaxes to compute the taxes for an employee isPrime to check whether or not a number is a prime number A returning value The computeTaxes function may return with a double number representing the amount of taxes The isPrime function may return with a Boolean value true or false 3 C Standard Functions C language is shipped with a lot of functions which are known as standard functions These standard functions are groups in different libraries which can be included in the C program e g Math functions are declared in math h library Character manipulation functions are declared in ctype h library C is shipped with more than 100 standard libraries some of them are very popular such as iostream h and stdlib h others are very specific to certain hardware platform e g limits h and largeInt h 4 Example of Using Standard C Math Functions include iostream h include math h void main Getting a double value double x cout Please enter a real number cin x Compute the ceiling and the floor of the real number cout The ceil x ceil x endl cout The floor x floor x endl 5 Example of Using Standard C Character Functions include iostream h input output handling include ctype h character type functions void main Explicit casting char ch cout Enter a character cin ch cout The toupper ch char toupper ch endl cout The tolower ch char tolower ch endl if isdigit ch cout ch is a digit n else cout ch is NOT a digit n 6 User Defined C Functions Although C is shipped with a lot of standard functions these functions are not enough for all users therefore C provides its users with a way to define their own functions or userdefined function For example the math h library does not include a standard function that allows users to round a real number to the ith digits therefore we must declare and implement this function ourselves 7 How to define a C Function Generally speaking we define a C function in two steps preferably but not mandatory Step 1 declare the function signature in either a header file h file or before the main function of the program Step 2 Implement the function in either an implementation file cpp or after the main function 8 What is The Syntactic Structure of a C Function A C function consists of two parts The function header and The function body The function header has the following syntax return value name parameter list The function body is simply a C code enclosed between 9 Example of User defined C Function double computeTax double income if income 5000 0 return 0 0 double taxes 0 07 income 5000 0 return taxes 10 Example of User defined C Function Function header double computeTax double income if income 5000 0 return 0 0 double taxes 0 07 income 5000 0 return taxes 11 Example of User defined C Function Function header Function body double computeTax double income if income 5000 0 return 0 0 double taxes 0 07 income 5000 0 return taxes 12 Function Signature The function signature is actually similar to the function header except in two aspects The parameters names may not be specified in the function signature The function signature must be ended by a semicolon Example Unnamed Parameter Semicolon double computeTaxes double 13 Why Do We Need Function Signature For Information Hiding If you want to create your own library and share it with your customers without letting them know the implementation details you should declare all the function signatures in a header h file and distribute the binary code of the implementation file For Function Abstraction By only sharing the function signatures we have the liberty to change the implementation details from time to time to Improve function performance make the customers focus on the purpose of the function not its implementation 14 Example include iostream include string using namespace std Function Signature double getIncome string double computeTaxes double void printTaxes double void main Get the income double income getIncome Please enter the employee income Compute Taxes double taxes computeTaxes income Print employee taxes printTaxes taxes 15 double computeTaxes double income if income 5000 return 0 0 return 0 07 income 5000 0 double getIncome string prompt cout prompt double income cin income return income void printTaxes double taxes cout The taxes is taxes endl Building Your Libraries It is a good practice to build libraries to be used by you and your customers In order to build C libraries you should be familiar with How to create header files to store function signatures How to create implementation files to store function implementations How to include the header file to your program to use your user defined functions 16 C Header Files The C header files must have h extension and should have the following structure ifndef compiler directive define compiler directive May include some other header files All functions signatures with some comments about their purposes their inputs and outputs endif compiler directive 17 TaxesRules Header file ifndef TAXES RULES define TAXES RULES include iostream include string using namespace std double computeTaxes double purpose to compute the taxes for a given income input a double value representing the income output a double value representing the taxes double getIncome string purpose to get the employee income input a string prompt to be displayed to the user output a double value representing the income void printTaxes double purpose to display taxes to the user input a double value representing the taxes output None endif 18 TaxesRules Implementation File include TaxesRules h double computeTaxes double income if income 5000 return 0 0 return 0 07 income 5000 0 double getIncome string prompt cout prompt double income cin income return income 19 void printTaxes double taxes cout The taxes is taxes endl Main Program File


View Full Document

AUC CSCE 106 - Lecture notes

Loading Unlocking...
Login

Join to view Lecture notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture notes and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?