0% found this document useful (0 votes)
10 views3 pages

Essential Function Programming Guide

The document outlines a series of programming exercises focused on writing and utilizing functions in C. Key topics include creating simple functions, using parameters and return types, implementing recursion, and organizing code across multiple files. Each exercise has specific objectives, such as calculating factorials, checking prime numbers, and creating a modular calculator.

Uploaded by

Nguyen Khanh Son
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Essential Function Programming Guide

The document outlines a series of programming exercises focused on writing and utilizing functions in C. Key topics include creating simple functions, using parameters and return types, implementing recursion, and organizing code across multiple files. Each exercise has specific objectives, such as calculating factorials, checking prime numbers, and creating a modular calculator.

Uploaded by

Nguyen Khanh Son
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Function Basics

Objective: Write and call a simple function.

 Write a function void info() that prints


 with basic information: full name, gender, age, hometown, phone number.

2. Function with Parameters

Objective: Use parameters in a function.

 Write a function void greet(char name[]) that takes a name as input and prints:

3. Function with Return Type

Objective: Write a function that returns a value.

 Write a function int square(int n) that calculates and returns the square of a number.
 Test the function by calling it from main().

4. Add Two Numbers

Objective: Combine parameters and return types.

 Write a function int add(int a, int b) that takes two integers as parameters and
returns their sum.
 Print the result in main().

5. Factorial Using a Function

Objective: Use a function to perform repetitive calculations.

 Write a function int factorial(int n) that calculates and returns the factorial of a
number.
 Call the function from main().

6. Swapping Two Numbers

Objective: Pass parameters by value.

 Write a function void swap(int a, int b) that swaps two numbers and prints their
values after swapping.
 Observe why the changes don’t persist in main() (pass by value).

7. Prime Number Check Using a Function


Objective: Use conditional logic in a function.

 Write a function int isPrime(int n) that returns 1 if a number is prime and 0


otherwise.
 Print the result in main().

8. Maximum of Three Numbers

Objective: Work with multiple parameters.

 Write a function int maxOfThree(int a, int b, int c) that returns the largest of
three numbers.

9. Temperature Conversion

Objective: Use functions for modular design.

 Write two functions:


o float celsiusToFahrenheit(float celsius)
o float fahrenheitToCelsius(float fahrenheit)
 Call these functions from main() to convert temperatures.

10. Organizing Functions Across Files

Objective: Divide a program into multiple files.

1. Create a header file math_utils.h with function prototypes for add, subtract,
multiply, and divide.
2. Implement these functions in math_utils.c.
3. Use them in a main.c program.

11. Recursive Functions

Objective: Use recursion in functions.

 Write a recursive function int fibonacci(int n) to calculate the nth Fibonacci


number.

12. Modular Calculator

Objective: Use multiple functions.

 Write a program with the following functions:


o int add(int a, int b)
o int subtract(int a, int b)
o int multiply(int a, int b)
o float divide(int a, int b)
 Call these functions based on user input (menu-driven program).

13. Sorting an Array with a Function

Objective: Pass arrays to functions.

 Write a function void sortArray(int arr[], int size) that sorts an array in
ascending order.
 Test it in main().

14. Passing Pointers to Functions

Objective: Use pointers for pass-by-reference.

 Write a function void swap(int *a, int *b) that swaps two numbers using pointers.
 Call the function from main() and verify the changes.

15. Organizing a Project

Objective: Structure a program with multiple files.

1. Create a header file string_utils.h for string-related functions (e.g., reverseString,


toUpperCase).
2. Implement the functions in string_utils.c.
3. Write a main.c file to test these functions.

You might also like