CS110_03a_FcnExamples.c

CS110_03a_FcnExamples.c - } void incr () { x=x+10; }...

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

View Full Document Right Arrow Icon
Function Examples #include<stdio.h> int absolute_value (int); main () { int num; printf(“Enter a number: ”); scanf(“%d”, &num); printf(“The absolute value of %d is %d\n”, \ num, absolute_value(num)); } int absolute_value (int x) { if (x >= 0) return x; else return -x; } 8
Background image of page 1

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

View Full DocumentRight Arrow Icon
Function Examples #include<stdio.h> void table(int); main () { int num; printf(“Enter a number: ”); scanf(“%d”, &num); table(num); } void table (int x) { int i; for (i=1; i<=10; i++) printf(“%d * %d = %d\n”, i, x, i*x); } 9
Background image of page 2
Function Examples #include<stdio.h> void table(int); main () { int i; printf(“Enter a number: ”); scanf(“%d”, &i); table(i); } void table (int x) { int i; for (i=1; i<=10; i++) printf(“%d * %d = %d\n”, i, x, i*x); } 11
Background image of page 3

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

View Full DocumentRight Arrow Icon
Global variables #include<stdio.h> int x; void incr (void); main () { printf(“Enter a number: ”); scanf(“%d”, &x); printf(“x is %d\n”, x); incr(); printf(“Now, x is %d\n”, x);
Background image of page 4
Background image of page 5

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

View Full DocumentRight Arrow Icon
Background image of page 6
Background image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: } void incr () { x=x+10; } Output: Please enter a number: 12 x is 12 Now, x is 22 13 Example #include&lt;stdio.h&gt; float add (int, int); int subtract (int, int); void divide (int, int); main () { int a,b; /* printf and scanf prompt the user and read values for a and b */ divide(a,b); } void divide(int x,int y) { int temp=subtract(x,y); if (temp==0) printf(Division by zero\n); else printf(The answer is %f\n), add(x,y)/temp); } 17 Example (contd) float add(int x, int y) { return x+y; } int subtract(int x, int y) { return x-y; } 18 Another example #include&lt;stdio.h&gt; int factorial (int); main () { int n; printf(&quot;Enter a number: &quot;); scanf(&quot;%d&quot;, &amp;n); printf(&quot;The factorial of %d is %d\n&quot;, n, factorial(n)); } int factorial (int n) { if (n==0) return 1; else return n*factorial(n-1); } 19...
View Full Document

This note was uploaded on 10/05/2011 for the course COMPUTER S 110-1 taught by Professor Tumblin during the Spring '11 term at Northwestern.

Page1 / 7

CS110_03a_FcnExamples.c - } void incr () { x=x+10; }...

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

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