Function is a block of code that performs a specific task and can be called from different parts of a program.
Functions help to break down a program into smaller, more manageable pieces, making it easier to read and maintain.
In C programming, a function is defined using the following syntax:
return_type function_name(parameter_list) {
// function body
return value; // if return_type is not void
}
Where:
return_type is the data type of the value that the function will return. If the function does not return a value, the return type is void.function_name is the name of the function, which should be descriptive of its purpose.parameter_list is a comma-separated list of parameters that the function takes as input. Each parameter consists of a data type followed by a variable name.return statement is used to return a value from the function if the return type is not void. The data type of the return is the same as the return type.The most common function in C programming is the main() function.
The main() function is the entry point of a C program, and it is where the execution of the program begins.
Every C program must have a main() function, and it is typically defined as follows:
int main() {
// code to be executed
return 0;
}
In the above code, the main() function has a return type of int, which means it returns an integer value. The return 0; statement indicates that the program has executed successfully.
The main is the name of the function.
() indicates that the function takes no parameters.
The block of code is written between the curly braces {}.
Parameters are values that are passed to the function when it is called.
Parameters allow you to provide input to a function, which can then use that input to perform its task.
When a function is called, you can pass arguments to it, which are the actual values that correspond to the parameters defined in the function.
For example, if you have a function that takes two parameters, you can call it like this:
function_name(argument1, argument2);
Where argument1 and argument2 are the values that you want to pass to the function.
Functions can also return values, which can be used in other parts of the program. For example, if you have a function that returns an integer value, you can call it and store the returned value in a variable like this:
int result = function_name(argument1, argument2);
In this example, the function is called with two arguments, and the returned value is stored in the variable result.
Return value could also be used without storing it in a variable.
For example, if you have a function that returns an integer value, you can call it and use the returned value directly in an expression like this:
printf("The result is: %d\n", function_name(argument1, argument2));
In this example, the function is called with two arguments, and the returned value is used directly in the printf() function to display the result.
When a function does not need to return a value, its return type is specified as void.
For example, a function that only prints a message to the console might have a return type of void:
void welcome() {
printf("Welcome To C Programming!\n");
}
In this example, the welcome() function does not return any value, so its return type is void.
In C Programming, you must write a function prototype before the main() function if you want to call that function from within the main() function.
A function prototype is a declaration of a function that specifies its name, return type, and parameters (if any) without providing the actual implementation of the function.
The function prototype allows the compiler to understand the function's signature and how to call it before it encounters the actual function definition.
The syntax for a function prototype is as follows:
return_type function_name(parameter_list);
Where:
return_type is the data type of the value that the function returns (or void if it doesn't return a value)function_name is the name of the functionparameter_list is a list of the types and names of the parameters that the function accepts (or empty if it doesn't accept any parameters)For example, if you have a function that adds two integers and returns the result, you would write its prototype like this:
int add(int a, int b);
In this example, the function prototype declares a function named add that takes two integer parameters and returns an integer value.
By writing the function prototype before the main() function, you can call the add() function from within the main() function without any issues, as the compiler will already know about the function's signature.
The function definition follows the prototype and contains the actual implementation of the function.
Let's create mathematical functions to add, subtract, multiply, and divide two integers.
#include <stdio.h>
int add(int a, int b);
int subtract(int a, int b);
int multiply(int a, int b);
float divide(int a, int b);
int main() {
int num1, num2;
printf("Enter two integers separated by a space: ");
scanf("%d %d", &num1, &num2);
printf("Addition: %d + %d = %d\n", num1, num2, add(num1, num2));
printf("Subtraction: %d - %d = %d\n", num1, num2, subtract(num1, num2));
printf("Multiplication: %d * %d = %d\n", num1, num2, multiply(num1, num2));
if (num2 != 0) {
printf("Division: %d / %d = %.2f\n", num1, num2, divide(num1, num2));
} else {
printf("Division by zero is not allowed.\n");
}
return 0;
}
int add(int a, int b) {
return a + b;
}
int subtract(int a, int b) {
return a - b;
}
int multiply(int a, int b) {
return a * b;
}
float divide(int a, int b) {
return (float)a / b;
}
Let's write functions to calculate the circumference and area of a circle.
#include <stdio.h>
#define PI 3.14159
float circumference(float radius);
float area(float radius);
int main() {
float radius;
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
printf("Circumference: %.2f\n", circumference(radius));
printf("Area: %.2f\n", area(radius));
return 0;
}
float circumference(float radius) {
return 2 * PI * radius;
}
float area(float radius) {
return PI * radius * radius;
}
We can also write a function that converts degree Celsius to degree Fahrenheit.
float celsius_to_fahrenheit(float celsius) {
return (celsius * 9/5) + 32;
}
The complete code will be:
#include <stdio.h>
float celsius_to_fahrenheit(float celsius);
int main() {
float celsius;
printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);
printf("Temperature in Fahrenheit: %.2f\n", celsius_to_fahrenheit(celsius));
return 0;
}
float celsius_to_fahrenheit(float celsius) {
return (celsius * 9/5) + 32;
}
We can also write a function that converts degree Fahrenheit to degree Celsius.
float fahrenheit_to_celsius(float fahrenheit) {
return (fahrenheit - 32) * 5/9;
}
The complete code will be:
#include <stdio.h>
float fahrenheit_to_celsius(float fahrenheit);
int main() {
float fahrenheit;
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &fahrenheit);
printf("Temperature in Celsius: %.2f\n", fahrenheit_to_celsius(fahrenheit));
return 0;
}
float fahrenheit_to_celsius(float fahrenheit) {
return (fahrenheit - 32) * 5/9;
}
We can also write a function to calculate the pressure of a body given the force and area:
float calculate_pressure(float force, float area) {
return force / area;
}
The complete code will be:
#include <stdio.h>
float calculate_pressure(float force, float area);
int main() {
float force, area;
printf("Enter force in Newtons: ");
scanf("%f", &force);
printf("Enter area in square meters: ");
scanf("%f", &area);
if (area != 0) {
printf("Pressure: %.2f Pascals\n", calculate_pressure(force, area));
} else {
printf("Area cannot be zero.\n");
}
return 0;
}
float calculate_pressure(float force, float area) {
return force / area;
}
In the above code, we have defined a function called calculate_pressure that takes two parameters: force and area. The function calculates the pressure by dividing the force by the area and returns the result.
In the main() function, we prompt the user to enter the force and area, and then we call the calculate_pressure() function with the user input. We also check if the area is not zero to avoid division by zero errors.
A function can also call other functions. This is known as function nesting.
For example, we can write a function that calculates the area of a rectangle and then use that function to calculate the area of a square:
float area_of_rectangle(float length, float width) {
return length * width;
}
float area_of_square(float side) {
return area_of_rectangle(side, side);
}
In this example, the area_of_square() function calls the area_of_rectangle() function to calculate the area of a square by passing the same value for length and width.
Function nesting allows us to reuse code and create more complex functions by combining simpler ones.
In summary:
return_type function_name(parameter_list) { /* function body */ /* return value; // if return_type is not void */ }.main() function is the entry point of a C program, where execution begins.void.