Module 3: Functions and Recursion - Algoritma-dan-Pemrograman-ITS/DasarPemrograman GitHub Wiki

Table of Contents


Functions

Introduction to Functions

A function is a set of statements that execute a specific task, with or without input, to return a correct output.

In general, functions are categorized into two, standard library functions and user-defined functions. Standard library functions are built-in functions from standard libraries. For instance, printf() and scanf() are defined in the library stdio.h. User-defined functions are functions that are made custom by the user to fulfill certain needs in the program.

The Objectives of Functions

Functions are made so that programs can be more modular. They are usually used when we want to execute a group of commands for several times, sometimes with different input values, without having to write the code pieces repeatedly. Aside from that, functions are also made to enable easier debugging.

Defining A Function

Before a function can be executed through a function call, we need to define it first. Function definition is needed to explain what the function will do if it is called for execution.

Below is the syntax for function definition in C:

<return_type> <function_name>(<parameter1>, <parameter2>, ...)
{
    statement;
    statement;
    ...
    ...
    ...
}

Below is an example of a function that prints the string "I am a function.":

void print()
{
    printf("I am a function\n");
}
 
int main()
{
    print();
    return 0;
}

Function Prototypes

Aside from defining a function since the start like we did in the previous example, we can also begin by making a function prototype. A function prototype declares a function without its definition. This declaration only contains the return type, name, and parameters of the function.

Below is the syntax for function prototypes:

// Declaration
<return_type> <function_name>(<parameter1>, <parameter2>, ...);

Code example of using a function prototype:

void print();        // Function prototype
 
int main()
{
    print();
    return 0;
}
 
void print()         // Function definition
{
    printf("I am a function\n");
}

Function Parameters

Parameters in a function can be seen as input to that function. There can be as many parameters passed to a function as needed. Function parameters are defined in a similar way to a variable. Each parameter is separated by the comma (,) operator.

<data_type_1> <parameter_1> , <data_type_2> <parameter_2>, ...

Example:

void print(char str[])
{
    printf("%s\n", str);
}
 
void sum(int a, int b)
{
    int total = a + b;
    printf("%d\n", total);
}

Function Calls

To call a function, function name is written, followed by a pair of brackets () containing parameters passed to that function. If the function requires parameters passed to it, then we take the right values/variables/objects to be passed as arguments separated by the operator ,. Arguments passed need to have the same sequence and data types as the function parameters.

Example of a function call:

int main()
{
    print();
    sum(2,5);
    print("Hello, world");
}

Function Return Values

If we want a function to return a value or simply outputs something, add the keyword return to the function and define the return type of this function (same data type as the return value). Functions that have a return type other than void must return a value.

When the statement return is found on a function, then the program will halt the function execution on the point of that return statement, then returns to the part of the code where the function was called.

Suppose we want to obtain the result of a simple addition of a and b through the function sum().

#include <stdio.h>
 
int sum(int a, int b);
 
int main()
{
    int x = 2, y = 3, result;
    result = sum(x, y);
    printf("%d\n", result);
    return 0;
}
 
int sum(int a, int b)
{
    int result = a;
    result += b;
    return result;
}

Recursive Functions

Introduction to Recursion

Recursion refers to a procedure that is repeated and is defined in terms of itself.

image

The above figure is a representation of recursion. In programming, the term recursion is used to describe a function that is recursive.

A recursive function is a function that may call upon itself while executing. See the below code example:

#include <stdio.h>
 
void recursion(int n)
{
    printf("%d\n", n);
    recursion(n+1);       // calls itself
}
 
int main()
{
    recursion(1);
    return 0;
}

The function recursion() is a recursive function because inside it there exists a call to itself.

Recursive Case and Base Case

The previous code example will keep on printing the value of n becaue the function doesn't know when to stop calling itself. Because of that, we need to define a base case in recursive functions.

A base case is a terminating condition that we provide the recursive function with so that it is able to stop calling itself.

Meanwhile, a recursive case is a condition where a recursive function has to call itself, or in other words where a recursive function has not reached its base case.

Let's take an example of exponentiating a number using a recursive function. Firstly, let's define the power of an integer a to an integer m as power(a, m), therefore it can be written as below:

Or, recursively:

With the base case:

The base case of the function power(a, m) is when power(a, 0) is executed, which returns 1. After the base case is reached, there's no need for further calls.

#include <stdio.h>
 
int power(int a, int m)
{
    if (m == 0) return 1;       // base case
    return (a * power(a, m-1)); // recursive case
}
 
int main()
{
    printf("%d\n", power(2,3));
    return 0;
}

Exercises

Exercise 1

Create a program that implements recursion to return the value of N! (Factorial of N).

Sample Input

5

Sample Output

120

Exercise 2

Given a sequence 1, 5, 14, 30, ...

Create a program that implements recursion to return the value of n-th number based on the above sequence.

Sample Input

2

Sample Output

5

Exercise 3

Create a program that implements recursion to determine the maximum and minimum values from an array A with N-numbers (all integers).

Sample Input

5
1 2 3 4 5

Sample Output

max: 5
min: 1
⚠️ **GitHub.com Fallback** ⚠️