#include <stdio.h>
// making function
void view(int func_a, int func_b){
printf("val0 = %d\n", func_a);
printf("val1 = %d\n", func_b);
}
int main() {
int a = 5;
int b = 3;
view(a, b); // using function
return 0;
}
view function is to view value of two variables.main function.int a and int b correspond to int func_a and int func_b, respectively. On the two variables, the variable types are match as int.view to other names.int-type variables to viewing double-type variables. In this case, for viewing, use %lf instead of %d and use double instead of int.#include <stdio.h>
//making function
void view(int *func_a){ // <- need [*] symbol, when input array type.
printf("val[0] = %d\n", func_a[0]);
printf("val[1] = %d\n", func_a[1]);
printf("val[2] = %d\n", func_a[2]);
}
int main() {
int a_arr[3] = {5, 4, 3};
view(a_arr); // <- using function
return 0;
}
* just before variable name like view(int *func_a).view(int *func_a, int a).#include <stdio.h>
//making function
void view(int *func_a, int a){ // <- func_a: array-type, a: non-array-type
printf("arr_val[0] = %d\n", func_a[0]);
printf("arr_val[1] = %d\n", func_a[1]);
printf("arr_val[2] = %d\n", func_a[2]);
printf("val = %d\n", a);
}
int main() {
int a_arr[3] = {5, 4, 3};
int a_val = 100;
view(a_arr, a_val); // <- using function
return 0;
}
* and check error code.#include <stdio.h>
// making function
int total(int a, int b, int c) { // <- int type function
int v = a + b + c;
return v; // <- return int variable
}
int main() {
int x, y, z;
int total_val;
x = 10;
y = 20;
z = 30;
total_val = total(x, y, z); // using function
printf("Total number = %d", total_val);
return 0;
}
total function has return v;, by coding total_val = total(x, y, z), the sum total is assigned to total_val.int total(...), type of variable for return value become int, that is, just before function name correspond to type of return value.int-type value, there is return v in total function.void type functions do not have return statement. See Code 1 and Code 2.#include <stdio.h>
#define MAX_SIZE 5
// making function
int total(int *a_func) { // <- int type function
int v = 0;
for(int i = 0; i < MAX_SIZE; i++) {
v = v + a_func[i];
}
return v; // <- return int variable
}
int main() {
int a_arr[MAX_SIZE] = {10, 20, 30, 40, 50};
int total_val = 0;
total_val = total(a_arr); // using function
printf("Total number = %d", total_val);
return 0;
}
* symbol because input to this user-defined function is array-type variable.* and check error code.#include <stdio.h>
#define MAX_SIZE 5
// making function
double ave(double *a_func) { // <- double type function
double v = 0;
for(int i = 0; i < MAX_SIZE; i++) {
v = v + a_func[i];
}
v = v / MAX_SIZE;
return v; // <- return double variable
}
int main() {
double a_arr[MAX_SIZE] = {5.9, 4.9, 6.2, 2.3, 1.5};
double ave_val = 0;
ave_val = ave(a_arr); // using function
printf("Average = %lf", ave_val);
return 0;
}
double-type function.* symbol because input to this user-defined function is array-type variable.int-type function and double-type function, that is, compare between Code 4 and Code 5.func1 to calculate the area of a triangle. Inputs to the function are width and height.func2 to calculate the area of a rectangle. Inputs to the function are width and height.func3 to calculate the area of triangle or rectangle. Inputs to the function are width, height, and user's select. When a user select 0, calculate triangle, when a user select 1, calculate rectangle.average that returns the average of an input array-type variable consisting of 100 randomly generated integer variables. The random range is from 0 to 100.minus_counter that returns the number of occurrences of negative values in the input array. The array type is int, and array size is 100. The elements of array is assigned a random number in the range of -100 to 100.count_1 that counts the number of occurrences of 1 and returns the result.count_x that counts the number of occurrences of x and returns the result. x is the value from 1 to 6, and this value is decided by user from scanf function.