1 / 12

Agenda

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

jamesjwood
Download Presentation

Agenda

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 #4 Function #1 Perform tasks call function #4 call function #5 commands Run (call) function #1 Function #5 commands Perform tasks Function #2 Run (call) function #2 Perform tasks commands Function #3 Function #6 Run (call) function #3 Call function #6 Perform tasks commands

  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 Categoriesof 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 No values (void) main function No value (void) values(int, double, char) main function No value (void) No values (void) main function value (int, double, char) values(int, double, char) main function value (int, double, char)

  6. Functions that do not acceptor return a value • #include <stdio.h > • 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 functioncalled 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 <stdio.h > • 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 functioncalled 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 acceptbut return a value • #include <stdio.h > • 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 functioncalled 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 <stdio.h > • 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 functioncalled sqr below main function: An integer is to be sent to function, and an integer is to be returned from function Call function 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. Return value back to main to be stored in variable result. 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

More Related