More Practice
1. Study the following function construct.
#include<stdio.h>
// prototype for Testing(), needed by C++ standard // take note about the semicolon at the end of the statement... void Testing(void);
// definition for main(). The first void means main() returns nothing // the second void means main() receive nothing void main(void) { // main() is calling Testing() and not passing any arguments // jump to Testing()... Testing(); }
// definition for Testing(), a user defined function // the second void means Testing() receive nothing // all work done or completed in the function body void Testing(void) { // Testing() is calling printf() and passing one argument, a string // this is a built in function translated to the defined task through // the stdio.h header file... printf("Function that just displaying a string!\n"); // back to main()... } |
|
In the program the main() calling a function called Testing(). Testing() in turn, calls printf(). When main() calls Testing(), no arguments or parameters are passed, which is noted by the empty parentheses.
When Testing() calls printf(), the argument "Function that just displaying a string!\n" is passed. When writing a function other than main(), you should provide a prototype at the top of the program (or before the function is defined), declaring what the function receives and what it returns. Think of the prototype as a declaration for a variable, such as , int i; where the type of the variable is specified. The prototype is also declaring the function type, void in this case. For the following statement, number the events in order starting from 1.
_________ - Testing() calls printf(). _________ - We/system call main() _________ - main() begins to execute by calling Testing() _________ - End of the program. _________ - Execution returns to main() from Testing(). | Ans:
3 - Testing() calls printf(). 1 - We/system call main() 2 - main() begins to execute by calling Testing() 5 - End of the program. 4 - Execution returns to main() from Testing().
|
________ - Calling a function. ________ - Heading for a function definition. ________ - Prototype for a function.
In the header for a function definitions, which void, the first one or the one in the parentheses, means that the function isn’t receiving any arguments? ___________ Which one means that the function isn’t returning any values? __________ | Yes - Calling a function. No - Heading for a function definition. Yes - Prototype for a function.
|
| #include <stdio.h>
void Testing(void);
void main(void) { int i; for(i=1;i <=2;i++) Testing(); }
void Testing(void) { printf("Function that just displaying a string!\n"); }
|
| #include <stdio.h>
void Testing(void);
void main(void) { Testing(); }
void Testing(void) { int i; for(i=1;i <=2;i++) printf("Function that just displaying a string!\n"); }
|
--------Output--------- nice! nice! nice! nice! | #include <stdio.h>
void Testing(void);
void main(void) { int i; for(i=1;i <=4;++i) Testing(); }
void Testing(void) { printf("nice!\n"); }
|
| #include <stdio.h>
void Testing(void);
void main(void) { Testing(); }
void Testing(void) { int i; for(i=1;i <=4;++i) printf("nice!\n"); }
|
| In solution 5, Testing() was called 4 times and while in Testing(), printf() was called once. Total number of times that printf() was called in each solution is 4. In solution 6, Testing was called once and while in Testing(), printf() was called 4 times. |
#include<stdio.h>
void Testing(char []);
... ... ...
void Testing(char str[]) { int i; for(i = 1; i <= 4; ++i) printf("str = %s ", str); printf("\n"); } | #include <stdio.h>
void Testing(char [ ]);
void main(void) { Testing("nice!"); }
void Testing(char str[ ]) { int i; for(i = 1; i <= 4; ++i) printf("str = %s ", str); printf("\n"); }
|
| #include <stdio.h>
void Testing(char [ ]);
void main(void) { Testing("nice!"); Testing("sweet!"); }
void Testing(char str[ ]) { int i; for(i = 1; i <= 4; ++i) printf("str = %s ", str); printf("\n"); }
|
| #include <stdio.h>
void Testing(char [ ]);
void main(void) { char str[10];
printf("Enter a string: "); // scanf("%s", &str); scanf_s("%s", &str, sizeof(str)); Testing(str); }
void Testing(char str[ ]) { int i; for(i = 1; i <= 4; ++i) printf("%s ",str); printf("\n"); }
|
---------Output--------- Enter a small string: kind kind kind kind kind Enter a small string: good good good good good Enter a small string: cute cute cute cute cute | #include <stdio.h>
void Testing(char []);
void main(void) { char str[10], str1[10], str2[10];
printf("Enter a small string: "); // scanf("%s", &str); scanf_s("%s", &str, sizeof(str)); Testing(str); printf("Enter a small string: "); // scanf("%s", &str1); scanf_s("%s", &str1, sizeof(str1)); Testing(str1); printf("Enter a small string: "); // scanf("%s", &str2); scanf_s("%s", &str2, sizeof(str2)); Testing(str2); }
void Testing(char str[]) { int i; for(i = 1; i <= 4; ++i) printf("%s ",str); printf("\n"); }
|
| void Testing(char [ ], int); |
| void Testing(char str[ ], int num) { int i; for(i = 1; i <= num; ++i) printf("%s ",str); printf("\n"); } |
| void main(void) { Testing(“forgiving”, 3); Testing(“patience”, 2); }
The following is a complete code:
#include <stdio.h>
void Testing(char [ ], int);
void main(void) { Testing("forgiving", 3); Testing("patience", 2); }
void Testing(char str[ ], int num) { int i; for(i = 1; i <= num; ++i) printf("%s ",str); printf("\n"); }
|
------Output----- The largest is 30 The largest is 50 The largest is 60 | #include <stdio.h>
void Largest(int [ ], int [ ]);
void main(void) { int A[3] = {40, 70, 20}, B[3]={45, 35, 75};
// pass array's element pointers or addresses Largest(&A[0], &B[0]); Largest(&A[1], &B[1]); Largest(&A[2], &B[2]); }
void Largest(int hold_arr1[ ], int hold_arr2[ ]) { int i =0, largest;
if(hold_arr1[i] > hold_arr2[i]) largest = hold_arr1[i]; else largest = hold_arr2[i]; printf("The largest is %d\n", largest); }
|
| We still need to edit some part of the Largest().
#include <stdio.h>
void Largest(int [ ], int [ ]);
void main(void) { int A[3] = {40, 70, 20}, B[3]={45, 35, 75}; int i;
// pass array's element pointers or addresses for(i=0;i<=2;i++) Largest(&A[i], &B[i]); }
void Largest(int hold_arr1[ ], int hold_arr2[ ]) { int i =0, largest;
if(hold_arr1[i] > hold_arr2[i]) largest = hold_arr1[i]; else largest = hold_arr2[i]; printf("The largest is %d\n", largest); }
|
| #include <stdio.h>
void Largest(int [ ], int [ ]);
void main(void) { int A[3] = {40, 70, 20}, B[3]={45, 35, 75};
// pass array's first element pointers or addresses Largest(A, B); }
void Largest(int hold_arr1[ ], int hold_arr2[ ]) { int i, largest;
for(i=0;i<=2;i++) { if(hold_arr1[i] > hold_arr2[i]) largest = hold_arr1[i]; else largest = hold_arr2[i]; printf("The largest is %d\n", largest); } }
|
| #include <stdio.h>
void Largest(int [ ], int [ ]);
void main(void) { int A[3] = {40, 70, 20}, B[3]={45, 35, 75};
// pass array's first element pointers or addresses Largest(A, B); }
void Largest(int hold_arr1[ ], int hold_arr2[ ]) { int i, x[3], largest;
for(i=0;i<=2;i++) { if(hold_arr1[i] > hold_arr2[i]) largest = hold_arr1[i]; else largest = hold_arr2[i];
x[i] = largest; printf("The largest is %d\n", x[i]); } }
|
| #include <stdio.h>
void Largest(int [ ]);
void main(void) { int i, A[3] = {40, 70, 20}, B[3]={45, 35, 75};
// pass array's first element pointers or addresses Largest(A); printf("\nI'm in main().\n"); for(i=0;i<=2;i++) printf("A[%d] = %d\n", i, A[i]); }
void Largest(int x[ ]) { int i;
printf("I'm in Largest().\n"); for(i=0;i<=2;i++) { x[i] = x[i] + 5; printf("x[%d] = %d\n", i, x[i]); } }
|
|
In main(), will the first or the second string that is passed be altered? Ans: The function alters the first string so that the first string passed in main() will be altered. The second string stays the same.
In question 20, which loop, the first or the second, looks for the end of the first string? Which loop looks for the end of the second string? Which loop copies characters from one string to the end of the other? Ans: The first loop looks for the end of the first string by searching for the null character '\0'. The second loop looks for the end of the second string. The second loop copies characters from the second string to the end of the first one.
| void FindLen(char x[ ]) { int i; for(i=0;x[i]!='\0';i++) ; printf("Length of %s is %d\n", x, i); } |
|
|
#include<stdio.h>
void main(void) { int i = 2, A[3] = {5, 9, 4}; DrillIt(i, A); printf("i = %d, A[1] = %d\n", i, A[1]); }
void DrillIt(int j, int B[ ]) { j = 0; B[1] = 0; }
| #include <stdio.h>
void DrillIt(int, int [ ]);
void main(void) { int i = 2, A[3] = {5, 9, 4}; DrillIt(i, A); printf("i = %d, A[1] = %d\n", i, A[1]); }
void DrillIt(int j, int B[ ]) { j = 0; B[1] = 0; }
|
Well, we have already come to the end of this worksheet. If you already noticed, all the programs used functions that don’t have return value. In next worksheet we will learn functions that having return values.