| #include <stdio.h>
float AddInterest(float, float);
void main(void) { float newValue, Balanced, Interest;
printf("Enter your balanced and interest rate: \n"); // scanf("%f%f", &Balanced, &Interest); scanf_s("%f%f", &Balanced, &Interest, sizeof(float), sizeof(float)); newValue = AddInterest(Balanced, Interest); printf("Your new balanced is %.2f\n", newValue); }
float AddInterest(float BalancedHold, float InterestRateHold) { float value;
value = BalancedHold + (InterestRateHold*BalancedHold); return value; }
|
| #include <stdio.h>
int ArrayLen(float [ ]);
void main(void) { // the f modifier used to force to float float myArr[7] = {4.1f,7.2f,1.1f,8.5f,10.0f,2.2f,0.0f}; int value;
value = ArrayLen(myArr); printf("The number of slot minus the last is %d\n", value); }
int ArrayLen(float num[ ]) { int count = 0, i;
for(i=0;num[i] != 0;i++) count = count+1; return count; }
|
| #include <stdio.h>
int AddArray(int [ ]);
void main(void) { int myArr[7] = {4,2,1,8,5,2,0}; int value;
value = AddArray(myArr); printf("The sum of array element is %d\n", value); }
int AddArray(int num[ ]) { int sum = 0, i;
for(i=0;num[i] != 0;i++) sum = sum + num[i]; return sum; }
|
| #include <stdio.h>
int CountArray(int [ ]);
void main(void) { int myArr[7] = {4,2,10,8,5,2,-6}; int value;
value = CountArray(myArr); printf("The number of array element that\n" "greater than %d is %d\n", myArr[0], value); }
int CountArray(int num[ ]) { int count = 0, i;
for(i=0;num[i] >= 0;i++) if(num[i] > num[0]) count = count + 1; return count; }
|
| #include <stdio.h>
int StringLen(char [ ]);
void main(void) { // initialize to dummy value char myArr[20] = "abc"; int value;
printf("Enter a string: "); scanf(" %s", &myArr); scanf_s(" %s", &myArr, sizeof(myArr)); value = StringLen(myArr); printf("The string length is %d\n", value); }
int StringLen(char holdStr[ ]) { int i;
for(i=0;holdStr[i] != '\0';i++) // this is needed to complete the for loop definition ; return i; }
|
More Practice on Functions
#include<stdio.h>
void GetScore(char [ ]);
void main(void) { GetScore("Mrs. Tom Hank"); }
void GetScore(char person[ ]) { int Score;
printf("What is the score for %s?", person); scanf_s("%d", &Score, 1); }
|
---------------------------------------------------
No. Because the stored score's value is not returned to main(). |
| #include <stdio.h>
int GetScore(char [ ]);
void main(void) { int x; x = GetScore("Mrs. Tom Hank"); }
int GetScore(char person[ ]) { int Score;
printf("What is the score for %s?\n", person); // scanf("%d", &Score); scanf_s("%d", &Score, 1); return Score; } |
| #include <stdio.h>
int GetScore(char [ ]);
void main(void) { int x; x = GetScore("Mrs. Tom Hank"); printf("The score is %d\n", x); x = GetScore("Mrs. Nicole Kidman"); printf("The score is %d\n", x); }
int GetScore(char person[ ]) { int Score;
printf("What is the score for %s?\n", person); // scanf("%d", &Score); scanf_s("%d", &Score, 1); return Score; }
|
| #include <stdio.h>
int GetScore(char [ ]);
void main(void) { printf("Mr. Tom Hank score is %d\n", GetScore("Mr. Tom Hank")); printf("Mrs. Nicole Kidman score is %d\n", GetScore("Mrs. Nicole Kidman")); }
int GetScore(char person[ ]) { int Score;
printf("What is the score for %s?\n", person); // scanf("%d", &Score); scanf_s("%d", &Score, 1); return Score; }
|
| No. We don't need variable x. The disadvantage of not having x is that, once the score is printed, it is not available in main(). If we need that score later for some computation, then we would need to call get_score() again. In that case, it would have been to have saved the score in x at the beginning. |
| #include <stdio.h>
int GetScore(char [ ]);
void main(void) { int x;
x = GetScore("Mr. Tom Hank"); x = x + GetScore("Mrs. Nicole Kidman"); printf("The total score is: %d\n",x); }
int GetScore(char person[ ]) { int Score;
printf("What is the score for %s?\n", person); // scanf("%d", &Score); scanf_s("%d", &Score, 1); return Score; }
|
| #include <stdio.h>
int GetScore(char [ ]);
void main(void) { int x;
x = GetScore("Mr. Tom Hank"); printf("The total score is: %d\n",x + GetScore("Mrs. Nicole Kidman")); }
int GetScore(char person[ ]) { int Score;
printf("What is the score for %s?\n", person); // scanf("%d", &Score); scanf_s("%d", &Score, 1); return Score; }
|
|
|
| void PrintScore(char person[ ], int score) { printf("%s's score is %d\n", person, score); } |
| |
| void main(void) { char person[20] = "Mr. Krakatua"; int x;
x = GetScore(person); PrintScore(person); } |
| void main(void) { char person[20] = "Mr. Krakatua";
PrintScore(person, GetScore(person)); } |
Area = π * radius2 | #define PI 3.1416
float Area(float radius) { return (PI * radius * radius); } |
| float Volume((float radius, float height) { return (Area(radius) * height); } |
| void main(void) { printf("%d\n", Volume(3.0, 5.5)); } |
int StringLen(char s[ ]) { int i; ... ... ... return ... } | int StringLen(char s[ ]) { int i; for(i=0;s[i] != '\0';i++) ; return i; } |
void RemoveStr(char s[ ], int start, int count) { int i, length;
length = StringLen(s); for(i = start, j = start + _________; j <= __________; ++i, ++j) s[i] = ____________; } | void RemoveStr(char s[ ], int start, int count) { int i, length;
length = StringLen(s); for(i = start, j = start + count; j <= length; ++i, ++j) s[i] = s[j]; } |
|
|
For commonly used routines, there are already functions for them such as printf()/printf_s() and scanf()/scanf_s(). These are pre-defined functions defined in the related header files. For the standard C or C++, the header files provided with the compiler that you use. A collection of the header files that perform certain categories of tasks normally supplied in a compiled form and the package called libraries, for example, you may have a C graphic library. For the non-standard or implementation specific, the header files normally supplied by the company that provide the compiler or from third party. So, before you create your own functions, check your compiler’s documentation for the related tasks need to be completed by functions. Finally don’t forget to learn about the mystery ofmain() function.