D Function
D Function
A - Theory - References
A function is a block of code that performs a specific task.
Suppose, you need to create a program to create a circle and color it. You can
create two functions to solve this problem:
Dividing a complex problem into smaller chunks makes our program easy to
understand and reuse.
1. Types of function
There are two types of function in C programming:
User-defined functions
b. User-defined function
You can also create functions as per your need. Such functions created by the
user are known as user-defined functions.
And, the compiler starts executing the codes inside functionName().
The control of the program jumps back to the main() function once
code inside the function definition is executed.
Note, function names are identifiers and should be unique.
Advantages of user-defined function
2. C User-defined functions
Here is an example to add two integers. To perform this task, we have created an
user-defined addNumbers().
#include <stdio.h>
int addNumbers(int a, int b); // function prototype
int main()
{
int n1,n2,sum;
printf("Enters two numbers: ");
scanf("%d %d",&n1,&n2);
sum = addNumbers(n1, n2); // function call
printf("sum = %d",sum);
return 0;
}
int addNumbers(int a, int b) // function definition
{
int result;
result = a+b;
return result; // return statement
}
-------------------------------------------------------------------------------------------------------------
-----
a. Function prototype
A function prototype is simply the declaration of a function that specifies
function's name, parameters and return type. It doesn't contain function body.
A function prototype gives information to the compiler that the function may
later be used in the program.
-------------------------------------------------------------------------------------------------------------
-----
e. Passing arguments to a function
In programming, argument refers to the variable passed to the function. In the
above example, two variables n1 and n2 are passed during the function call.
The parameters a and b accepts the passed arguments in the function definition.
These arguments are called formal parameters of the function.
The type of arguments passed to a function and the formal parameters must
match, otherwise, the compiler will throw an error.
-------------------------------------------------------------------------------------------------------------
-----
f. Return Statement
The return statement terminates the execution of a function and returns a value
to the calling function. The program control is transferred to the calling function
after the return statement.
g. Syntax of return statement
return (expression);
For example,
return a;
return (a+b);
The type of value returned from the function and the return type specified in the
function prototype and function definition must match.
The output of all these programs below is the same, and we have created a user-
defined function in each example. However, the approach we have taken in each
example is different.
Well, it depends on the problem you are trying to solve. In this case, passing
argument and returning a value from the function (example 4) is better.
#include <stdio.h>
#include <conio.h>
// Khai bao cac nguyen mau 'Prototype' cua cac ham trong CT
void Add();
void Sub();
void Multi();
void Div();
int main(){
int choice;
while(1){
system("cls");
printf("--- BASIC CALCULATOR ---\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiple\n");
printf("4. Division\n");
printf("5. Exit\n");
printf("Select options: ");
scanf("%d", &choice);
switch(choice){
case 1:
Add(); break;
case 2:
Sub(); break;
case 3:
Multi(); break;
case 4:
Div(); break;
case 5:
printf("Thank!");
exit(0);
break;
default:
printf("Invalid option. Please try again!");
break;
}
getch();
}
return 0;
}
// Tien hanh dinh nghia cac ham da duoc khai bao
void Add(){
int a, b, result;
printf("Enter a: "); scanf("%d", &a);
printf("Enter b: "); scanf("%d", &b);
result = a + b;
printf("Total a + b = %d\n", result);
}
void Sub(){
int a, b, result;
printf("Enter a: "); scanf("%d", &a);
printf("Enter b: "); scanf("%d", &b);
result = a - b;
printf("Sub a - b = %d\n", result);
}
void Multi(){
int a, b, result;
printf("Enter a: "); scanf("%d", &a);
printf("Enter b: "); scanf("%d", &b);
result = a * b;
printf("Multi a * b = %d\n", result);
}
void Div(){
int a, b;
double result;
printf("Enter a: "); scanf("%d", &a);
printf("Enter b: "); scanf("%d", &b);
result = (double)a / b;
printf("Div a / b = %.2lf\n", result);
}
C - PRACTICE
P1. Write a function that prints all numbers from 1 to 100 as below:
-----
Program:
P2. Write a function to print all the numbers from 1 to 100, which are divisible
by 3 and not divisible by 5.
-----
Program:
P3. Write a function to find all the prime numbers between the two
integers, checkPrimeNumber() is a function created. A prime number is a
positive integer that is divisible only by 1 and itself. For example: 2, 3, 5, 7, 11,
13, 17
Output:
(Note: If the user enters the larger number first, this program will not work as
intended. To solve this issue, you need to swap the numbers first)
-----
Program:
P4. Write a program in C to convert decimal number to binary number using the
function.
Output:
-----
Program:
D - EXERCISES
Ex1. Write a program in C to find the sum of the series 1!/1 + 2!/2 + 3!/3 + 4!/4
+ 5!/5 using the function.
Expected Output :
Ex2. Write a program in C to check a given number is even or odd using the
function.
Test Data :
Expected Output :
E - REFERENCE
Write a program that either calculates the value of an integer raised to the
power of an integer exponent or the arithmetic mean of a series of integers,
depending upon a choice made by the user.
list all of the tasks that the program should perform to solve this
problem
identify the modules for the problem structure
check that each module is high in cohesion
check that each module is low in coupling
----
Solution:
#include <stdio.h>
// Prototypes
int power(int base, int exponent);
int sum(int number);
int main(){
char choice, confirm;
int number, exponent;
do{
system("cls");
printf("-- Exercises 1 --\n");
printf("1. Power of an integer exponent\n");
printf("2. Sum of a series of integers\n");
printf("3. Exit\n");
printf("Select a option: ");
scanf(" %c", &choice);
switch(choice){
case '1':
printf("Enter base: ");
scanf("%d", &number);
printf("Enter exponent: ");
scanf("%d", &exponent);
printf("%d^%d = %d", number, exponent,
power(number,exponent));
break;
case '2':
printf("Enter a integer: ");
scanf("%d", &number);
printf("Sum: 1 + 2 + ... + %d = %d", number,
sum(number));
break;
case '3':
printf("Goodbye!");
exit(0);
break;
default:
printf("Invalid option. Try again!");
break;
}
printf("\nPress y or Y to continue: ");
scanf(" %c", &confirm);
} while(confirm=='y' || confirm=='Y');
return 0;
}
// Defination functions
int power(int base, int exponent){
int result = 1, i;
for(i=0; i< exponent; i++)
result *= base;
return result;
}
int sum(int number){
int result = 0, i;
for(i=0; i<=number; i++)
result += i;
return result;
}