Presentation is loading. Please wait.

Presentation is loading. Please wait.

Agenda  Perform Quiz#2 (20 Minutes)  Functions / Continued … –Functions - Definition –Types of Functions: Functions that do not accept or return a value.

Similar presentations


Presentation on theme: "Agenda  Perform Quiz#2 (20 Minutes)  Functions / Continued … –Functions - Definition –Types of Functions: Functions that do not accept or return a value."— Presentation transcript:

1 Agenda  Perform Quiz#2 (20 Minutes)  Functions / Continued … –Functions - Definition –Types of Functions: Functions that do not accept or return a value Functions that do not accept but return a value Functions that accept but do not return a value Functions that accept and return a value –Examples

2 Functions  As discussed in the last class, user-defined functions are used to separate specific programming tasks into “parts” or “modules”.  Function prototypes are usually defined above the main program and the function heading (and function body) usually appear below the main program  The main program can execute or “call” these user- defined functions. When the user-defined function ends, the program that called that function resumes.  User-defined functions can be used to call other user- defined functions as well.

3 Functions  Here is a diagram to help show the relationship between functions in a typical C program: Main Program (Function) Function #1 Function #2 Function #3 Run (call) function #1 Run (call) function #2 Run (call) function #3 call function #4 call function #5 Perform tasks Call function #6 commands Perform tasks Function #4 Function #5 Function #6

4 Functions  So far, we have seen how we can call a function that performs a simple task such as printing a title.  Other functions can allow values to be sent to a function for processing (so value contained in variable is already declared and assigned)  Also, functions can return a single value back to the original program or function.  When designing a program using functions, it is useful to draw a diagram to show which values (parameters) are sent to the function (if any), and what value (if any) is returned from the function.

5 Four Major Categories of Functions  Functions that do not accept or return a value –Functions that accept but do not return a value –Functions that do not accept but return a value –Functions that accept and return a value main value (int, double, char) No values (void) values (int, double, char) No value (void) value (int, double, char) values (int, double, char) main function

6 Functions that do not accept or return a value  #include  void print_title (void);  main (){  int value, i;  printf ("\nEnter an integer less than 13: ");  scanf ("%d", &value);  while ( value >= 13 ){  printf ("\nPlease pick a number less than 13: ");  scanf ("%d", &value);  }  print_title ();  for (i = 1; i <= 12; i++)  printf ("%d times %d is equal to %d\n", i, value, value * i);  printf ("\n");  }  void print_title (void){  printf("\n\nHere is my Times Table,\n");  printf("I don't know the value because\n");  printf("I didn't pass it to the function!\n\n");  } Indicates a function called print_title below main function: No values are to be sent to or returned from the function Call function Perform a Walk-through to see what happens...

7 Functions that accept but do not return a value  #include  void print_table (int num);  main (){  int value;  printf ("\nEnter an integer less than 13: ");  scanf ("%d", &value);  while ( value >= 13 ){  printf ("\nPlease pick a number less than 13: ");  scanf ("%d", &value);  }  printf ("\n");  print_table (value);  printf ("\n");  }  void print_table (int num){  int i;  for (i = 1; i <= 12; i++)  printf ("%d times %d is equal to %d\n", i, num, num * i);  } Indicates a function called print_table below main function: An integer is to be sent to function, but no value returned from function. Call function Perform a Walk-through…

8 In Class Exercise #1  Write a program called even_num_1.c to determine if a positive number is either an even or odd number. Use a function called Even_Num to prompt the user for a positive number, and make certain that only a positive number is entered. Have the function display whether the number is even or odd.

9 Functions that do not accept but return a value  #include  int calc_result (void);  main (){  int result;  printf ("\nCalling function to scan data and\n");  printf ("to calculate and return result to main:\n");  result = calc_result();  printf ("\nReturning value of calculation to be displayed\n");  printf ("in the main program:\n");  printf("\nThe result is %d.\n\n", result);  }  int calc_result (void){  int num_1, num_2, calc;  printf ("\nEnter an integer: ");  scanf ("%d", &num_1);  printf ("\nEnter another integer: ");  scanf ("%d", &num_2);  calc = num_1 * num_2;  return calc;  } Indicates a function called calc_result below main function: No value to be sent to function, but integer to be returned from function Call function The return command followed by a variable will send value back to main program. Notice that when function is called, it is assigning the returned value into a variable called result for storage (use same data type!) Perform a Walk-through…

10 In Class Exercise #2  Modify the program even_num_1.c to change the name of the function “Even_Num” to “Get_Input”, and have the function only prompt, scan and error-check the number. The correct number should be “passed-back” to the main program which will determine and display if the number is either even or odd. Save the modified program as even_num_2.c

11 Functions that accept and return a value  #include  int sqr (int x);  main (){  int num, result;  printf ("\nEnter an integer: ");  scanf ("%d", &num);  result = sqr (num);  printf("\nThe square of %d is %d.\n\n", num, result);  }  int sqr (int x){  int square_num;  square_num = x * x;  return square_num;  } Indicates a function called sqr below main function: An integer is to be sent to function, and an integer is to be returned from function Call function Return value back to main to be stored in variable result. NOTE: When defining a function to send a parameter (value), you can specify any value within brackets (as long as it the same data type) when the function is being called. This allows you to use the same function in your program at different times throughout the program. Perform a Walk-through…

12 In Class Exercise #3  Modify the program even_num_2.c to add a function called “Status_Num” that will only determine if the number is odd or even. The number’s status (odd or even) will be “passed- back” to the main program and the program will display the number as even or odd. Save the modified program as even_num_3.c


Download ppt "Agenda  Perform Quiz#2 (20 Minutes)  Functions / Continued … –Functions - Definition –Types of Functions: Functions that do not accept or return a value."

Similar presentations


Ads by Google