A function is a named block of code designed to perform a specific task. Functions help divide a large program into smaller, organized, reusable parts.
A function is a block of statements that performs a particular task.
void greet()
{
printf("Hello");
}
Here, greet() is a function that displays a message.
Functions provide several benefits:
return_type function_name()
{
// statements
}
For example:
void display()
{
printf("Welcome");
}
The function has a return type, a name, parentheses, and a body.
The function name identifies the function and is used when calling it.
void showMessage()
{
printf("Hello");
}
Here, showMessage is the function name.
Function names should follow C identifier rules and should clearly describe the task when possible.
The return type specifies the type of value a function returns.
int add()
{
return 10 + 20;
}
Here, the function returns an int value.
A function that does not return a value can use void.
A void function does not return a value to its caller.
void greet()
{
printf("Hello!");
}
The function can be called to perform an action without producing a return value.
Defining a function does not automatically execute it. The function must be called.
#include <stdio.h>
void greet()
{
printf("Hello!");
}
int main()
{
greet();
return 0;
}
The statement greet(); calls the function.
A function definition contains the actual statements that perform the task.
void welcome()
{
printf("Welcome to C programming!");
}
The statements between the braces form the function body.
A function declaration tells the compiler about a function before the function is used.
void greet();
int main()
{
greet();
return 0;
}
void greet()
{
printf("Hello!");
}
The declaration void greet(); appears before
main().
A function prototype is a declaration that specifies the function's return type, name, and parameter types.
int add(int, int);
It tells the compiler that an add function exists and
accepts two int arguments.
A function can be written without parameters.
void display()
{
printf("Hello World");
}
int main()
{
display();
return 0;
}
The function gets all the information it needs from its own body or other accessible program data.
int getNumber()
{
return 100;
}
int main()
{
int value = getNumber();
printf("%d", value);
return 0;
}
The function returns the integer value 100.
float getPrice()
{
return 99.50f;
}
int main()
{
float price = getPrice();
printf("%.2f", price);
return 0;
}
The function returns a floating-point value.
void greet()
{
printf("Hello\n");
}
int main()
{
greet();
greet();
greet();
return 0;
}
The same function can be called multiple times.
int add()
{
int a = 10;
int b = 20;
return a + b;
}
int main()
{
int result = add();
printf("Sum = %d", result);
return 0;
}
The function calculates and returns the sum.
int square(int number)
{
return number * number;
}
int main()
{
int result = square(5);
printf("Square = %d", result);
return 0;
}
This example uses a parameter to receive the number. Parameters will be covered in more detail in the next lesson.
void showMessage()
{
printf("Welcome to Soopro Pathshala!\n");
}
int main()
{
showMessage();
return 0;
}
A function can be used to keep display-related code separate from
main().
The main() function is the entry point of a hosted C program.
Program execution begins from main().
int main()
{
printf("Program starts here.");
return 0;
}
Other functions can be called from main().
A variable declared inside a function normally has local scope.
void display()
{
int number = 10;
printf("%d", number);
}
The variable number is local to the function block and cannot
normally be accessed directly from another function.
int multiply()
{
int a = 6;
int b = 7;
int result = a * b;
return result;
}
int main()
{
printf("Result = %d", multiply());
return 0;
}
The calculated value is returned to the calling code.
A large program can be divided into functions according to different tasks.
void inputData()
{
// input code
}
void calculate()
{
// calculation code
}
void displayResult()
{
// output code
}
This approach makes the program easier to organize and maintain.
A function can be reused whenever the same task is required.
int cube(int number)
{
return number * number * number;
}
int main()
{
printf("%d\n", cube(2));
printf("%d\n", cube(3));
printf("%d\n", cube(4));
return 0;
}
The same function is called with different values.
Defining a function does not execute it automatically.
void greet()
{
printf("Hello");
}
int main()
{
return 0;
}
Here, greet() is defined but never called, so its body is not
executed.
Correct:
int main()
{
greet();
return 0;
}
The return type should match the kind of value the function is intended to return.
int getValue()
{
return 50;
}
Here, int is appropriate because the function returns an
integer value.
A function that returns no value should use void.
A non-void function should return a value as required by its definition and control flow.
int getNumber()
{
return 100;
}
The return statement supplies the value to the caller.
#include <stdio.h>
void welcome()
{
printf("Welcome to C programming!\n");
}
int main()
{
welcome();
return 0;
}
This program defines a simple function and calls it from
main().
#include <stdio.h>
int add(int a, int b)
{
return a + b;
}
int main()
{
int result;
result = add(10, 20);
printf("Sum = %d", result);
return 0;
}
The function receives two values and returns their sum.
#include <stdio.h>
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;
}
int main()
{
printf("Addition = %d\n", add(10, 5));
printf("Subtraction = %d\n", subtract(10, 5));
printf("Multiplication = %d\n", multiply(10, 5));
return 0;
}
Different functions can handle different operations of the same program.
#include <stdio.h>
float calculateAverage(int a, int b, int c)
{
return (a + b + c) / 3.0f;
}
int main()
{
int math = 80;
int english = 75;
int computer = 90;
float average;
average = calculateAverage(
math,
english,
computer
);
printf("Average = %.2f", average);
return 0;
}
The calculation is placed in a separate function, keeping
main() easier to understand.
Practice the following programs:
Practice creating, declaring, calling, and returning values from functions. Parameters and arguments will be studied in the next lesson.
void is used when a function does not return a value.int or float.main() is the entry point of a hosted C program.Question: What is the main purpose of a function in C?