Basic C programming 6: Function¶

Yuto Omae @ Nihon University, Japan¶

  • We use online compiler and debugger for c/c++.
  • https://www.onlinegdb.com/online_c_compiler
  • By making user-defined function, we can implement various functions by simple expression.

Code 1: Void type function (cases of input non-array-type variable)¶

  • A user-defined function that returns no value is called void type function.
  • Here is one of typical void-type functions.
#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;
}

Key points¶

  • The role of view function is to view value of two variables.
  • In general, user-defined functions are defined to the location of upper side of main function.
  • It is important to match variable type regarding corresponded two variables, e.g., 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.

Works¶

  • Change function's name from view to other names.
  • Change the role from viewing int-type variables to viewing double-type variables. In this case, for viewing, use %lf instead of %d and use double instead of int.

Code 2: Void type function (cases of input array-type variable)¶

  • Here is the code for including a user-defined function of void type that takes input array type variable.
#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;
}

Key points¶

  • In the case of adopting array-type variable as input of user-defined function, need * just before variable name like view(int *func_a).
  • Compare with code 1 and code 2.
  • Of coruse, we can both of array and non-array variables as input of user-defined function lile the following code. See 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;
}

Works¶

  • Remove * and check error code.

Code 3: int type function (cases of input non-array-type)¶

  • The following is the code for returning sum total of three variables.
#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;
}

Key points¶

  • Because the total function has return v;, by coding total_val = total(x, y, z), the sum total is assigned to total_val.
  • By writing int total(...), type of variable for return value become int, that is, just before function name correspond to type of return value.
  • Because this function returns int-type value, there is return v in total function.
  • In contrast, void type functions do not have return statement. See Code 1 and Code 2.

Code 4: int type function (cases of input array-type variable)¶

  • The following is the code for calculating sum total of array-type variable.
#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;
}

Key points¶

  • Need * symbol because input to this user-defined function is array-type variable.
  • How to calculate total sum was explained in previous lecture material. If you cannot understand, see Basic C programming 5.

Works¶

  • Remove * and check error code.

Code 5: double type function¶

  • Of course, we can make double-type user-defined functions.
  • Here is the code for calcularing average of all elements of array.
#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;
}

Key points¶

  • Because average is real number, return value is also real number, that is, double-type function.
  • Need * symbol because input to this user-defined function is array-type variable.

Works¶

  • Compare between int-type function and double-type function, that is, compare between Code 4 and Code 5.

Assignments¶

  • (1) Make Function func1 to calculate the area of a triangle. Inputs to the function are width and height.
  • (2) Make Function func2 to calculate the area of a rectangle. Inputs to the function are width and height.
  • (3) Make Function 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.
  • (4) Create a function 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.
  • (5) Create a function 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.
  • (6) Roll the 6-sided dice 100 times and save the result in an int type array. Then, create a function count_1 that counts the number of occurrences of 1 and returns the result.
  • (7) Roll the 6-sided dice 100 times and save the result in an int type array. Then, create a function 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.