1. C Programming for Beginners: Writing Your First Hello World Program in C in 2023 2. Mastering Datatypes in C Programming: A Comprehensive Guide for Beginners in 2023 3. Complete Guide to % Format Specifiers in C for Beginners in 2023 4. A Comprehensive Guide to Operators in C Programming: Learn Everything About Operators in C in 2023 5. Master C Operator Precedence and Associativity in 2023: Definition, Examples, and Code 6. Learn Increment and Decrement Operators in C: A Beginners Guide with Examples - Codzify 7. Mastering Bitwise Operators in C: A Comprehensive Guide with Examples - Codzify 8. Ternary Operators in C Programming: A Beginners Guide (2023) 9. Switch Statements in C 2023: Complete Guide for Beginners 10. Learn Functions in C Programming - A Complete Tutorial for Beginners in 2023 11. Call by Value and Call by Reference in C Programming - A Complete Tutorial for Beginners in 2023 12. Storage Classes in C Programming: A Comprehensive Guide for 2023 13. Arrays in C Programming: A Comprehensive Guide for 2023 14. Basics of 2D Array in C Programming in 2023: Definition, Initialization, and Accessing Elements 15. Beginners Guide to Strings in C Programming with Codes and Examples 16. Strings Functions in C Language - strlen, strcpy,strcmp,strcat functions 17. A Comprehensive Guide to Structures in C Programming in 2023 18. Learn Unions in C Programming with Codes, Examples, and Explanations in 2023 19. Dynamic Memory Allocation in C: Understanding malloc, calloc, realloc, and free Functions 20. Introduction to C Programming 21. Hello world program in C 22. String functions in C - strncat, strcpy, strncpy, strchr, strrchr, strstr functions 23. Program for Fibonacci number in C - Codzify.com 24. What is palindrome number in C? 25. Write a C program to find a factorial of a given number 26. Write a C program to check whether a given number is prime or not 27. Write a C program to check whether a given number is an Armstrong number or not. 28. Write a C Program to transpose a matrix 29. C Programming Tutorial: Understanding Comments and Identifiers with Code Examples 30. Master String Handling Functions in C in 2023 - Codzify Topics

Learn Functions in C Programming - A Complete Tutorial for Beginners in 2023

Article by: Manish Methani

Last Updated: November 4, 2021 at 2:04pm IST
5 min 25 sec read

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.

What are Functions?

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.

How to Create a Function in C

To create a function in C, you need to follow these steps:

  1. 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.

  2. 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.

Function Parameters

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.

Function Return Values

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.

Explore Codzify Courses

PREMIUM

Learn to create the Dating App using No-Code Tool FlutterFlow

Level: Advanced

Course Fees: INR 1299/-

Apply Coupon Code: UNLOCK

4 days 100% Money Back Guarantee

Explore Curriculum
PREMIUM

Flutter Mobile App Development Course

Level: Intermediate

Course Fees: INR 1299/-

4 days 100% Money Back Guarantee

Explore Curriculum
PREMIUM

The Complete Angular Course

Level: Beginners

Course Fees: INR 1299/-

4 days 100% Money Back Guarantee

Explore Curriculum
PREMIUM

Dart Programming for Absolute Beginners

Level: Beginners

Course Fees: INR 1299/-

4 days 100% Money Back Guarantee

Coming Soon
FREE

Next.js course

Level: Beginners

Course Fees: FREE

Start Watching
FREE

Fundamentals of Computer Programming Languages

Level: Beginners

Course Fees: FREE

Start Watching
FREE

Learn HTML, CSS & Bootstrap

Level: Beginners

Course Fees: FREE

Start Watching

Latest Web Stories

1

Learn how to open WhatsApp using FlutterFlow | Step by Step Guide

Test your skills with these expert-led curated
Mock Tests.

C Programming Test

Test your C Programming skills with this comprehensive mock test on C Programming.

Take Test

Flutter Test

Solve most asked Interview Questions on Flutter and Test your foundational skills in flutter.

Take Test

GATE(CSE) Operating Systems

Solve most asked GATE Questions in Operating Systems and test your Gate Score.

Take Test

HTML,CSS Test

This is a mock test designed to help you assess your knowledge and skills in HTML and CSS.

Take Test

(GATE CSE) Data Structures & Algorithms Test

Solve most asked GATE Questions in Data Structures and Algorithms and test your Gate Score.

Take Test

Download the Codzify
Mobile App


Learn Anytime, Anywhere at your own pace. Scan the QR Code with your Mobile Camera to Download the Codzify Mobile App.

Codzify Mobile App Codzify Mobile App
Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer