Ch4 functions

1,613 views
1,469 views

Published on

Published in: Education, Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,613
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
108
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Ch4 functions

  1. 1. Chapter 4: Functions Introduction to Function
  2. 2. Objectives <ul><li>To construct programs modularly from functions. </li></ul><ul><li>To use common math functions available in the C Standard Library. </li></ul><ul><li>To create functions with multiple parameters. </li></ul><ul><li>The mechanisms for passing information between functions and returning results. </li></ul><ul><li>How the function call/return mechanism is supported by the function call stack and activation records. </li></ul><ul><li>To write and use recursive functions. </li></ul>
  3. 3. Intro <ul><li>Divide and conquer. </li></ul><ul><li>Use for various individual tasks. </li></ul><ul><li>Functions enable grouping of commonly used code into a reusable and compact unit. </li></ul><ul><li>In this chapter we will see </li></ul><ul><ul><li>how functions are defined and how they are accessed from various places within a C program. </li></ul></ul><ul><ul><li>Overview of function, and a portion of the C Standard Library functions. </li></ul></ul><ul><ul><li>learn how to declare a function, and the manner in which information is passed to a function. </li></ul></ul>
  4. 4. Intro <ul><li>Advantages of using functions </li></ul><ul><ul><li>avoids the need for redundant (repeated) programming of the same instructions. </li></ul></ul><ul><ul><li>easier to write and easier to debug. </li></ul></ul><ul><ul><li>logical structure is more apparent than programs which lack this type of structure, especially true of lengthy, complicated programs. </li></ul></ul><ul><ul><li>avoids repetitive programming between programs </li></ul></ul>
  5. 5. Function Overview <ul><li>Every C program consists of one or more functions. </li></ul><ul><ul><li>One of these functions must be called main . </li></ul></ul><ul><li>A function will carry out its intended action whenever it is accessed or called from some other portion of the program. </li></ul><ul><li>The same function can be accessed from several different places within a program. </li></ul><ul><li>Once the function has carried out its intended action, control will be returned to the point from which the function was accessed. </li></ul>
  6. 6. Function Overview <ul><li>Two type of functions </li></ul><ul><ul><li>Library functions </li></ul></ul><ul><ul><ul><li>used to carry out a number of commonly used operations such as printf(…) , getchar(), and putchar(). </li></ul></ul></ul><ul><ul><li>User/Programmer-defined functions </li></ul></ul><ul><ul><ul><li>used to define their own functions for carrying out various individual tasks. </li></ul></ul></ul><ul><li>Information is passed to the function via special identifiers called arguments (also called parameters), and returned via the return statement. </li></ul>
  7. 7. Library Functions <ul><li>4 categories: </li></ul><ul><ul><li>Library functions that carry out standard I/O operations (e.g., read and write characters, and numbers, etc.). </li></ul></ul><ul><ul><li>Functions that perform operations on characters (e.g., convert from lower- to uppercase, test for end of file, etc.). </li></ul></ul><ul><ul><li>Functions that perform operations on strings (e.g., copy a strings, compare strings, concatenate strings, etc.). </li></ul></ul><ul><ul><li>Functions that carry out various mathematical calculations (e.g. evaluate trigonometric, logarithmic and exponential functions,, etc.). </li></ul></ul>
  8. 8. Library Functions <ul><li>3 types of library functions: </li></ul><ul><ul><li>return a data item to their access point. </li></ul></ul><ul><ul><li>indicate whether a condition is true or false by returning a 1 or a 0, </li></ul></ul><ul><ul><li>carry out specific operations on data items but do not return anything. </li></ul></ul>
  9. 9. Library Functions: Standard Library Header Description <stdio.h> Functions for standard input and output <float.h> Floating point size limits <limits.h> Contains integral size limits of system <stdlib.h> Functions for converting numbers to text and text to numbers, memory allocation, random numbers, other utility functions <math.h> Math library functions <string.h> String processing functions <stddef.h> Common definitions of types used by C
  10. 10. Library Functions: Math.h Functions Function Returns sqrt(x) Square root exp(x) Exponential function log(x) Natural logarithm (base e) log10(x) Logarithm (base 10) fabs(x) Absolute value pow(x,y) X raised to the power of y sin(x) Trignometric sine (x in radians) cos(x) Trignometric cosine (x in radians) tan(x) Trignometric tangent (x in radians) atan(x) Arctangent of x (returned value is in radians)
  11. 11. Using Functions <ul><li>Include the header file for the required library using the preprocessor directive </li></ul><ul><ul><li>#include <libraryname.h> </li></ul></ul><ul><ul><li>Note: no semi colon after this </li></ul></ul><ul><li>Variables defined in functions are local variables </li></ul><ul><li>Functions have a list of parameters </li></ul><ul><ul><li>Means of communicating information between functions </li></ul></ul><ul><li>Functions can return values </li></ul><ul><li>printf and scanf good examples of function calls </li></ul>
  12. 12. User-defined Functions: Function Definition <ul><li>A function definition has two principal components: </li></ul><ul><ul><li>the first line (including the argument declarations), and </li></ul></ul><ul><ul><li>the body of the function. </li></ul></ul><ul><li>The first line of a function definition contains </li></ul><ul><ul><li>the type specification of the value returned by the function </li></ul></ul><ul><ul><li>followed by the function name, and </li></ul></ul><ul><ul><li>(optionally) a set of arguments, separated by commas and enclosed in parentheses. </li></ul></ul>
  13. 13. User-defined functions: Function Definition <ul><li>Format of a function definition </li></ul>Return-value-type function-name(parameter-list) { declarations statements } A return value of type void indicates a function does not Return a value.
  14. 14. User-defined Functions: Function Definition <ul><li>Function Definition: argument/parameter </li></ul><ul><ul><li>The arguments are called formal argument </li></ul></ul><ul><ul><ul><li>because they represent the names of data items that are transferred into the function from the calling portion of the program. </li></ul></ul></ul><ul><ul><li>There are also known as parameters or formal parameters. </li></ul></ul><ul><ul><li>The identifiers used as formal arguments are “local” </li></ul></ul><ul><ul><ul><li>because they are not recognized outside of the function. </li></ul></ul></ul><ul><ul><ul><li>the names need not to be same as the names of the actual arguments in the calling portion of the program. </li></ul></ul></ul>
  15. 15. User-defined Functions: Function Definition <ul><li>Function Definition: compound statement/body </li></ul><ul><ul><li>compound statement that defines the action to be taken by the function. </li></ul></ul><ul><ul><li>can contain expression statements, other compound statements, control statements, and so on. </li></ul></ul><ul><ul><li>It should include one or more return statements, in order to return a value to the calling portion of the program. </li></ul></ul>
  16. 16. User-defined Functions: Function Definition <ul><li>Function definition: return </li></ul><ul><ul><li>Return control to point from which function called </li></ul></ul><ul><ul><li>3 Ways to return </li></ul></ul><ul><ul><ul><li>Function does not return a result (void) control is returned when function right brace } is reached. </li></ul></ul></ul><ul><ul><ul><li>Execute the statement </li></ul></ul></ul><ul><ul><ul><ul><li>return; </li></ul></ul></ul></ul><ul><ul><ul><li>If the statement returns a value the following statement must be executed </li></ul></ul></ul><ul><ul><ul><ul><li>return expression; </li></ul></ul></ul></ul>
  17. 17. User-defined Functions: Example <ul><li>Function prototype & definition </li></ul>#include <stdio.h> int mod(int, int); /* Function Prototype */ main() { printf(&quot;The mod is: %d &quot;, mod(4,5)); } /* Function Definition */ int mod(int x, int y) { return x % y; }
  18. 18. User-defined Functions: Example <ul><li>Function with definition but without prototype </li></ul>#include <stdio.h> int sum(int x, int y) /* Function Definition */ { return x+y; } main() { printf(&quot;The sum is: %d &quot;, sum(4,5)); }
  19. 19. User-defined Functions: Function Prototype <ul><li>The function prototype </li></ul><ul><ul><li>Very important feature. </li></ul></ul><ul><ul><li>Must be added to a C program before the main function, if call that function before defining it. </li></ul></ul><ul><ul><li>It tells the compiler what type of value the function returns, number and types of parameters, and order in which these parameters are expected. </li></ul></ul><ul><ul><li>There is no need for a prototype if the function is called after its definition. </li></ul></ul>
  20. 20. User-defined Functions: Function Prototype <ul><li>Example function prototype </li></ul>long int sum (int x, int y); Where; x & y - Parameters int - Parameter type sum - Function name long int - Function return type
  21. 21. User-defined Functions: Function Calls <ul><li>A function can be accessed (i.e., called ) by </li></ul><ul><ul><li>specifying its name, followed by a list of arguments enclosed in parentheses and separated by commas. </li></ul></ul><ul><li>If the function call does not require any arguments </li></ul><ul><ul><li>empty pair of parentheses must follow the name of the function. </li></ul></ul><ul><li>The arguments appearing in the function call are referred to as actual arguments , in contrast to the formal arguments that appear in the first line of the function definition. </li></ul>
  22. 22. User-defined Functions: Function Calls <ul><li>In a normal function call </li></ul><ul><ul><li>there will be one actual argument for each formal argument. </li></ul></ul><ul><ul><li>may be expressed as constants, single variables , or more complex expressions. </li></ul></ul><ul><li>If the function returns a value, the function access is written a statement; e.g., </li></ul><ul><li>y = sum(x); </li></ul><ul><li>if the function does not return anything, the function access appears by itself; e.g., </li></ul><ul><li>view(a,b,c); </li></ul>
  23. 23. User-defined Functions: Function Calls <ul><li>A function can be called by </li></ul><ul><ul><li>the main function </li></ul></ul><ul><ul><li>other functions </li></ul></ul><ul><ul><li>itself (recursion) </li></ul></ul><ul><li>In C, functions calls can be </li></ul><ul><ul><li>by value </li></ul></ul><ul><ul><li>by reference </li></ul></ul>
  24. 24. User-defined Functions: Function Calls <ul><li>Call by value </li></ul><ul><ul><li>Copy of variable passed to function </li></ul></ul><ul><ul><li>If that variable is modified within the function then upon return from the function since only the copy has been modified, the actual variable is not modified </li></ul></ul><ul><li>Call by reference </li></ul><ul><ul><li>Pass the address of a variable (i.e. a pointer) to a function </li></ul></ul><ul><ul><li>The variable pointed to can be modified within that function </li></ul></ul>
  25. 25. User-defined Functions: Function Calls <ul><li>Example Call By Value </li></ul><ul><ul><li>finval=FuncByValue(finval); </li></ul></ul><ul><ul><li>The FuncByValue function </li></ul></ul>float FuncByValue(float fval) { return fval*fval; }
  26. 26. User-defined Functions: Function Calls <ul><li>Example By Reference </li></ul><ul><ul><li>FuncByReference(&finref), </li></ul></ul><ul><ul><ul><li>Use & to pass the address of a variable to the function; </li></ul></ul></ul><ul><ul><li>The FuncByReference function </li></ul></ul><ul><ul><li>Value of the referenced variable passed to the function is modified after returning from the function. </li></ul></ul>void FuncByReference(float *fvalptr) { *fvalptr = *fvalptr * *fvalptr; }
  27. 27. User-defined Functions: Recursion <ul><li>A function calls itself repeatedly, until some specified condition has been satisfied. </li></ul><ul><li>Each action is stated in terms of a previous result. </li></ul><ul><li>Many iterative (i.e., repetitive) problems can be written in this form. </li></ul><ul><li>In order to solve a problem recursively, two conditions must be satisfied. </li></ul><ul><ul><li>the problem must be written in recursive form. </li></ul></ul><ul><ul><li>the problem statement must include a stopping condition. </li></ul></ul>
  28. 28. User-defined Functions: Recursion <ul><li>Example </li></ul><ul><ul><li>Factorial </li></ul></ul>long int factorial (int n) /* calculate the factorial of n */ { if (n <= 1) return(1); else return(n * factorial(n-1) ); }
  29. 29. Additional Info: Header Files <ul><li>Standard libraries have header files containing function prototypes for all functions in that library </li></ul><ul><li>Programmer can create custom header files </li></ul><ul><ul><li>Should end in .h e.g. myfunctionlib.h </li></ul></ul><ul><li>Programmer function prototypes declared using the pre processor directive </li></ul><ul><ul><li>#include “myfunctionlib.h” </li></ul></ul>
  30. 30. <ul><li>Tuliskan satu fungsi takrifan pengguna yang diberi nama kuasa( ). Fungsi ini menerima input dua nombor integer iaitu x dan n, dan seterusnya mengira x n tanpa menggunakan fungsi pow(x,n). Kembalikan nilai tadi kepada fungsi yang memanggil </li></ul><ul><li>Tuliskan satu cara untuk menggunakan/memanggil fungsi kuasa( ) di atas. </li></ul>
  31. 31. <ul><li>Tulis satu fungsi dalam bahasa C untuk luas_bulatan( ) yang menerima argumen berjenis float iaitu jejari kepada bulatan, kemudian mengira luas bulatan dan seterusnya mengembalikan luas bulatan tersebut kepada fungsi pemanggil. </li></ul><ul><li>Tuliskan satu cara untuk menggunakan/memanggil fungsi luas_bulatan( ) di atas. </li></ul>
  32. 32. <ul><li>Based on the following function, write the function calls and prototype function for that function when x = 4.2 and y = 2.1. Then write the output. </li></ul><ul><li>void printresults( float x, float y) </li></ul><ul><li>{ </li></ul><ul><li>float num1, num2; </li></ul><ul><li>num1 = x; </li></ul><ul><li>num2 = y; </li></ul><ul><li>printf(“Result of add the value = %.2f”, num1+num2); </li></ul><ul><li>printf(“Result of multiply the value = %.2f”, num1*num2); </li></ul><ul><li>} </li></ul>
  33. 33. <ul><li>Based on the following function: </li></ul><ul><li>float calculate_dividen(float saving) </li></ul><ul><li>{ </li></ul><ul><li>float dividen; </li></ul><ul><li>dividen = saving * 0.16; </li></ul><ul><li>return (dividen); </li></ul><ul><li>} </li></ul><ul><li>Write an example of function calls. </li></ul><ul><li>What is the prototype of this function? </li></ul>
  34. 34. <ul><li>What is the first line syntax of function definition of the following statement? </li></ul><ul><li>A function called maximum accepts three integers (x, y, and z) and returns integer </li></ul><ul><ul><li>A. maximum (int, int, int) </li></ul></ul><ul><ul><li>B. maximum (x, y, z) </li></ul></ul><ul><ul><li>C. int maximum (x, y, z) </li></ul></ul><ul><ul><li>D. int maximum (int x, int y, int z) </li></ul></ul>
  35. 35. What is the output for the following C program if the entered input is 108? <ul><li>#include<stdio.h> </li></ul><ul><li>void final_value(int z) </li></ul><ul><li>{ </li></ul><ul><li>int a = 5; </li></ul><ul><li>while ( z > 0) </li></ul><ul><li>{ </li></ul><ul><li>a++; </li></ul><ul><li>z /=10; </li></ul><ul><li>} </li></ul><ul><li>printf(“ Final value of z = %d and a = %dn”, z, a); </li></ul><ul><li>} </li></ul><ul><li>main( ) </li></ul><ul><li>{ </li></ul><ul><li>int x, y = 0; </li></ul><ul><li>printf(“n Enter one integer value: “); </li></ul><ul><li>scanf (“%d”, &x); </li></ul><ul><li>final_value(x); </li></ul><ul><li>} </li></ul>
  36. 36. <ul><li>Which one of the following is the correct function call for function xyz? </li></ul><ul><ul><li>call xyz( ); </li></ul></ul><ul><ul><li>void xyz(void); </li></ul></ul><ul><ul><li>xyz; </li></ul></ul><ul><ul><li>p = xyz( ); </li></ul></ul>
  37. 37. <ul><li>What is the correct function prototype for the following statement? </li></ul><ul><li>A function called Average accepts two float arguments (a and b) and print the average of a and b. </li></ul><ul><ul><li>float Average (a, b); </li></ul></ul><ul><ul><li>void Average (a, b); </li></ul></ul><ul><ul><li>void Average (float, float); </li></ul></ul><ul><ul><li>float Average (float float); </li></ul></ul>
  38. 38. Contoh <ul><li>Setempat </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int i, j; </li></ul><ul><li>: </li></ul><ul><li>: </li></ul><ul><li>} </li></ul><ul><li>void fungsi() </li></ul><ul><li>{ : </li></ul><ul><li>: </li></ul><ul><li>} </li></ul><ul><li>Sejagat </li></ul><ul><li>int i, j; </li></ul><ul><li>void main(void) </li></ul><ul><li>{ </li></ul><ul><li>: </li></ul><ul><li>: </li></ul><ul><li>} </li></ul><ul><li>void fungsi1() </li></ul><ul><li>{ : </li></ul><ul><li>: </li></ul><ul><li>} </li></ul>
  39. 39. <ul><li>#include <stdio.h> </li></ul><ul><li>int i, j; </li></ul><ul><li>void fungsi1(); </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>fungsi1(); </li></ul><ul><li>printf(&quot;Main: i = %d, j = %dn&quot;, i, j); </li></ul><ul><li>} </li></ul><ul><li>void fungsi1() </li></ul><ul><li>{ </li></ul><ul><li>i = 1000; </li></ul><ul><li>j = 500; </li></ul><ul><li>printf(“Fungsi1: i = %d, j = %dn&quot;, i, j); </li></ul><ul><li>} </li></ul>
  40. 40. <ul><li>#include <stdio.h> </li></ul><ul><li>void fungsi1(); </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int i = 1000; </li></ul><ul><li>printf(&quot;Main: i = %dn&quot;, i); </li></ul><ul><li>fungsi1(); </li></ul><ul><li>printf(&quot;Main lagi: i = %dn&quot;, i); </li></ul><ul><li>} </li></ul><ul><li>void fungsi1() </li></ul><ul><li>{ </li></ul><ul><li>int i = 500; </li></ul><ul><li>printf(&quot;Fungsi1: i = %dn&quot;, i); </li></ul><ul><li>} </li></ul>
  41. 41. <ul><li>#include <stdio.h> </li></ul><ul><li>void fungsi1(int); </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int i = 1000; </li></ul><ul><li>fungsi1(i); </li></ul><ul><li>printf(&quot;Main: i = %dn&quot;, i); </li></ul><ul><li>} </li></ul><ul><li>void fungsi1(int j) </li></ul><ul><li>{ </li></ul><ul><li>j = 5 * j; </li></ul><ul><li>printf(&quot;Fungsi1: j = %dn&quot;, j); </li></ul><ul><li>} </li></ul>

×