C/Function/Function
Содержание
- 1 A program with two functions
- 2 A simple program that demonstrates sum()
- 3 Assign function return value to a variable
- 4 Check out the int value change before and after function call
- 5 Define function and return value
- 6 Define function and use it
- 7 Define function and use it: square
- 8 Define function to calculate the ounces and cups
- 9 Define function to calculate the volumn
- 10 Define function to multiply two int
- 11 Define function to output char
- 12 Define function to sum
- 13 Define function with parameter and return value
- 14 Define function with void return
- 15 Define two functions and call them in main
- 16 Define two functions and make function call
- 17 Definition of the function to calculate an average
- 18 Demonstraction of function call
- 19 Function call
- 20 Function call each other
- 21 Function declaration
- 22 Function: Print a string in uppercase
- 23 Function prototype
- 24 Function prototype: declare getnum() prior to its first use
- 25 Function to output square
- 26 Function which returns int value
- 27 Global and local variable inside function
- 28 Return statement
- 29 Serve as a prototype within this program
- 30 This program has three functions
- 31 Uses a function prototype to enforce strong type checking
A program with two functions
<source lang="cpp">
- include <stdio.h>
void f(void); /* prototype for f() */ int main(void) {
printf("I "); f(); printf("java2s.com."); return 0;
} void f(void) {
printf("like ");
}
</source>
A simple program that demonstrates sum()
<source lang="cpp">
- include <stdio.h>
void sum(int x, int y); int main(void) {
sum(12, 230); sum(94, 63); sum(851, 39); return 0;
} void sum(int x, int y) {
printf("%d ", x + y);
}
</source>
Assign function return value to a variable
<source lang="cpp">
- include <stdio.h>
int f(void); int main(void) {
double answer; answer = f(); printf("%f", answer); return 0;
} int f(void) {
return 100;
}
</source>
Check out the int value change before and after function call
<source lang="cpp">
- include <stdio.h>
void f(int *k) {
*k = *k + 10;
} main () {
int i; i = 0; printf (" i before call %d \n", i); f(&i); printf ("i after call %d \n", i);
}
</source>
Define function and return value
<source lang="cpp">
- include <stdio.h>
int f(void); int main(void) {
char ch; ch = f(); printf("%c", ch); return 0;
} int f(void) {
return "a";
}
</source>
Define function and use it
<source lang="cpp">
- include <stdio.h>
int f(void); /* prototype */ int main(void) {
int num; num = f( ); printf("%d", num); return 0;
} int f(void) {
return 10;
}
</source>
Define function and use it: square
<source lang="cpp">
- include <stdio.h>
int sqr(int x); int main(void) {
int t=10; printf("%d %d", sqr(t), t); return 0;
} int sqr(int x) {
x = x * x; return( x );
}
</source>
Define function to calculate the ounces and cups
<source lang="cpp">
- include <stdio.h>
int ounces_to_cups(int o); int main(void) {
int ounces; int cups; printf("Enter ounces: "); scanf("%d", &ounces); cups = ounces_to_cups(ounces); printf("%d cups", cups); return 0;
} int ounces_to_cups(int o) {
return o / 8;
}
</source>
Define function to calculate the volumn
<source lang="cpp">
- include <stdio.h>
double volume(double s1, double s2, double s3); int main(void) {
double vol; vol = volume(12.2, 5.67, 9.03); printf("Volume: %f", vol); return 0;
} double volume(double s1, double s2, double s3) {
return s1 * s2 * s3;
}
</source>
Define function to multiply two int
<source lang="cpp">
- include <stdio.h>
int mul(int a, int b); int main(void) {
int x, y, z; x = 10; y = 20; z = mul(x, y); /* 1 */ printf("%d", mul(x,y)); /* 2 */ mul(x, y); /* 3 */ return 0;
} int mul(int a, int b) {
return a * b;
}
</source>
Define function to output char
<source lang="cpp">
- include <stdio.h>
void printchar(char ch); int main(void) {
printchar("A"); printchar("B"); printchar("C"); return 0;
} void printchar(char ch) {
printf("%c", ch);
}
</source>
Define function to sum
<source lang="cpp">
- include <stdio.h>
int sum(int i1, int i2,int i3) {
return (i1 + i2 + i3);
} int main() {
printf("Sum is %d\n", sum(1, 2, 3)); return (0);
}
</source>
Define function with parameter and return value
<source lang="cpp">
- include <stdio.h>
double f(); int main(void) {
printf("%f", f(10.2)); return 0;
} double f(double num) {
return num / 2.0;
}
</source>
Define function with void return
<source lang="cpp">
- include <stdio.h>
void outputnum(int num); int main(void) {
outputnum(10); return 0;
} void outputnum(int num) {
printf("%d", num);
}
</source>
Define two functions and call them in main
<source lang="cpp">
- include <stdio.h>
void f1(void); void f2(void); int main(void) {
f1(); f2(); return 0;
} void f1(void) {
printf("f1, ");
} void f2(void) {
printf("f2.");
}
</source>
Define two functions and make function call
<source lang="cpp">
- include <stdio.h>
void f1(void), f2(void); int main(void) {
f1(); return 0;
} void f1(void) {
int i; for(i = 0; i < 10; i++) f2();
} void f2(void) {
int i; for(i = 0; i < 10; i++) printf("%d ", i);
}
</source>
Definition of the function to calculate an average
<source lang="cpp">
- include <stdio.h>
float average(float x, float y) {
return (x + y)/2.0f;
} /* main program - execution always starts here */ void main() {
float average(float x, float y); /* Function prototype */ float value1 = 1.0F; float value2 = 2.0F; float r = 0.0F;
r = average(value1, value2); printf("\nThe average is: %f", r);
}
</source>
Demonstraction of function call
<source lang="cpp"> void f2(void){
printf ("2 \n");
} void f1 (void) {
printf ("1 \n"); printf ("1 \n"); f2 ( ); printf ("1 \n"); printf ("1 \n");
}
int main ( ) {
printf ("\n"); printf ("\n"); printf ("\n"); printf ("\n"); printf ("\n"); f1(); printf ("\n"); printf ("\n"); printf ("\n"); printf ("\n"); return 0;
}
</source>
Function call
<source lang="cpp">
void f(int k) {
k = k + 10;
} int main ( ) {
int i = 0; printf ("i before call %d \n", i); f(i); printf (" i after call %d \n", i);
}
</source>
Function call each other
<source lang="cpp">
- include <stdio.h>
void f2(int b); void f1(int a); int main(void) {
f1(30); return 0;
} void f1(int a) {
if( a ) f2( a - 1 ); printf("%d ", a);
} void f2(int b) {
printf(" . "); if(b) f1( b - 1 );
}
</source>
Function declaration
<source lang="cpp">
- include <stdio.h>
int area(int l, int w); int main(void) {
printf("area is %d", area(100, 130)); return 0;
} int area(l, w) int l, w; {
return l * w;
}
</source>
Function: Print a string in uppercase
<source lang="cpp">
- include <stdio.h>
- include <ctype.h>
void print_upper(char *string); int main(void) {
char s[80]; printf("Enter a string: "); gets(s); print_upper(s); printf("\ns is now uppercase: %s", s); return 0;
} void print_upper(char *string) {
register int t; for(t=0; string[t]; ++t) { string[t] = toupper(string[t]); putchar(string[t]); }
}
</source>
Function prototype
<source lang="cpp">
- include <stdio.h>
void f(void); int main(void) {
f(); f(); return 0;
} void f(void) {
int i = 10; printf("%d ", i);
}
</source>
Function prototype: declare getnum() prior to its first use
<source lang="cpp">
- include <stdio.h>
/* declare getnum() prior to its first use */ float getnum(void) {
float x; printf("Enter a number: "); scanf("%f", &x); return x;
} int main(void) {
float i; i = getnum(); printf("%f", i); return 0;
}
</source>
Function to output square
<source lang="cpp">
- include <stdio.h>
void sqrF(int num); int main(void) {
sqrF(10.0); return 0;
} void sqrF(int num) {
printf("%d", num * num);
}
</source>
Function which returns int value
<source lang="cpp">
- include <stdio.h>
int convert(void); int main(void) {
printf("%d", convert()); return 0;
} int convert(void) {
int dollars; printf("Enter number of dollars: "); scanf("%d", &dollars); return dollars / 8;
}
</source>
Global and local variable inside function
<source lang="cpp">
- include <stdio.h>
int count; /* global count */
void f(void) {
int count; /* local count */ count = 100; printf("count in f() : %d\n", count);
} int main(void) {
count = 10; f(); printf("count in main(): %d\n", count); return 0;
}
</source>
Return statement
<source lang="cpp">
- include <stdio.h>
void f(void); int main(void) {
f(); return 0;
} void f(void) {
printf("Here."); return; /* return with no value */ printf("Never.");
}
</source>
Serve as a prototype within this program
<source lang="cpp">
- include <stdio.h>
void f(int a, int b) {
printf("%d ", a % b);
} int main(void) {
f(10,3); return 0;
}
</source>
This program has three functions
<source lang="cpp">
- include <stdio.h>
void f1(void); /* prototypes */ void f2(void); int main(void) {
f2( ); printf("3"); return 0;
} void f2(void) {
f1(); printf("2 ");
} void f1(void) {
printf("1 ");
}
</source>
Uses a function prototype to enforce strong type checking
<source lang="cpp"> void sqr_it(int *i); /* prototype */ int main(void) {
int x; x = 10; sqr_it(x); /* type mismatch */ return 0;
} void sqr_it(int *i) {
*i = *i * *i;
}
</source>