CodeBlanks Logo Icon CodeBlanks

Summary - Functions

Combine prototypes, parameters and recursion to write a factorial calculator.

Summary: Functions

In this group you saw how to define functions, call them, return values, take parameters and use recursion.

  • void f(void) { ... }: a function that returns no value
  • int f(int x): takes a parameter, returns a value
  • return: sends the value back to the caller
  • Prototype: a signature declaration before the definition
  • Recursion: the function calls itself; a base case is required

Now let's combine these in one task.

Practice Call a function twice

Define void stars(void) with body printf("***\n");. Call it twice from main. Output should be two lines of ***.

Starter code

main.c
#include <stdio.h>

/* define stars function */

int main(void) {
    /* call stars twice */
    return 0;
}

Sample solution

main.c
#include <stdio.h>

void stars(void) {
    printf("***\n");
}

int main(void) {
    stars();
    stars();
    return 0;
}

Expected output

***
***

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What should the return type be if a function returns no value?

  • char
  • int
  • void Correct answer
  • double

Explanation: void means the function returns no value.

2 Call expression

Question
int result = add(3, ____);  /* add(3, 5) call */
Answer
int result = add(3, 5);  /* add(3, 5) call */

Explanation: The second argument is 5.

3 Match function concepts

Term Meaning
return returns value
void no return value
prototype signature declaration
recursion calls itself

All lessons in Beginner