L9_ - 9. FUNCTIONS Example: Printing a Message /*...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
9. FUNCTIONS
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Example: Printing a Message /* Illustrates functions with no arguments */ #include <stdio.h> void print_pun(void) { printf("To C, or not to C: "); printf("that is the question.\n"); } int main(void) { print_pun(); return 0; }
Background image of page 2
Example: Counting Down /* Illustrates functions with arguments */ #include <stdio.h> void print_count(int n) { printf("T minus %d and counting\n", n); } int main(void) { int i; for (i = 10; i > 0; --i) print_count(i); return 0; }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Example: Finding the Larger of Two Numbers /* Illustrates functions that return a value */ #include <stdio.h> int max(int a, int b) { if (a > b) return a; else return b; } int main(void) { int i, j; printf("Enter two numbers: "); scanf("%d%d", &i, &j); printf("The larger number is %d\n", max(i, j)); return 0; }
Background image of page 4
Defining a Function • A function definition has the following appearance: result-type function-name ( parameters ) { declarations statements } • If a function does not return a value, its result type should be specified to be void. • If the result type is omitted, the function is assumed to return a value of type int. • If a function has no parameters, the word void should appear between the parentheses.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Calling a Function • To call a function, give the function name followed by a list of arguments (in parentheses). • A call of a void function must be a statement: print_pun(); print_count(i); • A call of a non-void function returns a value that can be stored in a variable, tested, printed, or used in some other way: k = max(i, j); if (max(i, j) > 10) . .. printf("The larger number is %d\n", max(i, j));
Background image of page 6
Calling a Function • The value returned by a non-void function can always be discarded if desired: max(i, j); /* return value is discarded */ To make it clear that the return value is deliberately being discarded, it is possible to put (void) before the call:
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 07/08/2011 for the course CGS 3460 taught by Professor Staff during the Summer '08 term at University of Florida.

Page1 / 26

L9_ - 9. FUNCTIONS Example: Printing a Message /*...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online