Summary - Functions
- C course
- Beginner
- Lesson 70/116
- 5 min
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 valueint f(int x): takes a parameter, returns a valuereturn: 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
#include <stdio.h>
/* define stars function */
int main(void) {
/* call stars twice */
return 0;
}Sample solution
#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
int result = add(3, ____); /* add(3, 5) call */
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 |