// A simple demo of how functions work

// Marc Chee, March 2020

#include <stdio.h>

// For our main to know what a function is, it should be
// declared before the main function starts
int add(int a, int b);
void print_add(int a, int b);

int main(void) {
    int first = 3;
    int second = 7;
    
    // use the add function to find a value that
    // we will then store in total
    int total = add(first, second);
    
    // use the print_add function to write some
    // information to the terminal
    print_add(first, second);
    
    return 0;
}

// a simple function that adds two ints together and
// returns the sum
int add(int a, int b) {
    return a + b;
}

// a simple function that adds two ints together and
// prints the result to the terminal
void print_add(int a, int b) {
    int result = a + b;
    printf("The result of the addition is: %d\n", result);
}

Resource created Wednesday 27 January 2021, 01:16:15 PM, last modified Monday 01 March 2021, 02:45:17 PM.

file: functionDemo.c


Back to top

COMP1511 21T1 (Programming Fundamentals) is powered by WebCMS3
CRICOS Provider No. 00098G