Article by: Manish Methani
Last Updated: November 4, 2021 at 2:04pm IST
If you're new to programming, you've likely heard of functions. Functions are an essential concept in programming languages, and they allow developers to create reusable code blocks that perform specific tasks. In this beginner's guide, we'll dive into functions in C programming and learn how they work, how to create them, and how to use them in your code.
A function in C programming is a block of code that performs a specific task. Functions can take inputs, called parameters, and return outputs, called return values. Functions are used to break down complex problems into smaller, more manageable tasks, making it easier to write, debug, and maintain code.
To create a function in C, you need to follow these steps:
Declare the function: You need to declare the function before using it in your code. The function declaration specifies the function name, return type, and parameters.
Define the function: The function definition contains the actual code that performs the task. It must include the function header, which matches the function declaration.
Here's an example of a simple function that takes two integer parameters and returns their sum:
#include <stdio.h> int sum(int num1, int num2) { int result = num1 + num2; return result; } int main() { int a = 5, b = 10, total; total = sum(a, b); printf("The sum of %d and %d is %d", a, b, total); return 0; }
In this example, we declare the sum
function with two integer parameters and an integer return value. Then, we define the function to take two integer inputs, add them together, and return the result. Finally, we call the function in our main
program, passing in two integer values and assigning the returned sum to a variable called total
.
Functions in C can take zero or more parameters. Parameters are the values that you pass to the function to perform the task. Parameters can be of any data type, such as integer, float, char, or even pointers.
Here's an example of a function that takes a string parameter and prints it to the console:
#include <stdio.h> void printString(char* str) { printf("The string is: %s", str); } int main() { char message[] = "Hello, World!"; printString(message); return 0; }
In this example, we declare the printString
function to take a string parameter and return nothing (void
). Then, we define the function to print the input string to the console. Finally, we call the function in our main
program, passing in a string value.
Functions in C can return zero or one value. The return value is the result of the function's task. You must declare the return type in the function declaration.
Here's an example of a function that calculates the factorial of a number and returns the result:
#include <stdio.h> int factorial(int num) { if (num == 0) { return 1; } else { return num * factorial(num - 1); } } int main() { int n = 5, result; result = factorial(n); printf("The factorial of %d is %d", n, result); return 0; }
This program is an example of a recursive function in C programming language that calculates the factorial of a number using the concept of recursion.
The function factorial()
takes an integer argument num
and returns the factorial of that number. Inside the function, there is an if-else
statement that checks if the value of num
is equal to zero. If it is, then the function returns 1 because the factorial of 0 is 1. If it is not zero, then the function calls itself recursively with the argument num - 1
and multiplies the result by num
.
In the main()
function, an integer variable n
is assigned the value of 5, which is the number for which we want to calculate the factorial. The factorial()
function is called with the argument n
and the returned value is stored in the variable result
. Finally, the result is printed on the console using the printf()
function.
When you run this program, the output will be "The factorial of 5 is 120" because 5! (factorial of 5) is equal to 120.
I hope this explanation helps you understand the program better.