C Function Practice Questions
C Programming Functions
Use these exercises to practice functions progressively. Start with no-argument/no-return functions, then add parameters, return values, scope, static variables and recursion.
Level 1: Basic Function Calls Top ↑
Create a function that prints Welcome and returns no value.
Create a function that displays the sum of two integers using printf() inside the function.
Create a function that displays the square of 5 without returning a value.
Pass one number to a function and display the value after adding 10.
Create a function that returns the square of 5 to main().
Pass 5 as a parameter, return its square and print the returned value in main().
Level 2: User Input and Return Values Top ↑
Read one number and pass it to a function that displays its square.
Read one number, pass it to a function, add 10 and return the result to main().
Read two numbers and display their sum without using a user-defined function.
Read two numbers and display their sum inside a function without returning a value.
Pass two numbers to a function and display the larger number inside the function.
Pass two numbers to a function and return the larger number.
Return the larger of two numbers and print it in main().
Return the sum of two numbers and print twice that sum in main().
Pass three numbers to a function, return their sum and add 10 to the result in main().
Level 3: Logic and Scope Top ↑
Read a car-number integer and a day number 1–7. Allow even-numbered cars on even days and odd-numbered cars on odd days.
Declare num1 = 5 inside main(). Try to access it from another function without passing it. Explain why this fails.
Declare a separate local num1 = 100 inside another function and compare its scope with the variable in main().
Add 100 to the function's local variable and print it.
Call that function twice and observe how a normal local variable behaves.
Change the local variable to static and compare the output across calls.
Find the lowest common multiple (LCM) of two input numbers using a function.
Find the greatest common divisor (GCD/HCF) of two input numbers using a function.
Level 4: Recursion Top ↑
Recursively find the sum n + (n-1) + ... + 1 for a positive integer.
Recursively find the factorial of a positive integer.
Recursively find the GCD/HCF of two positive integers.
Starter Pattern Top ↑
#include <stdio.h>
int square (int value) {
return value * value;
}
int main (void ) {
int number = 5 ;
printf ("%d\n" , square (number));
return 0 ;
}
For each exercise, decide first: What parameters are needed? and Should the function return a value?
← Functions in C
Author & Instructor at
plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery,
and web development at plus2net. The tutorials focus on clear explanations, working
examples, and code that readers can test and adapt while learning.
This article is written by plus2net.com team.
plus2net.com