DevAcademia
C++C#CPythonJava
  • C Basics

  • Introduction to C
  • Getting Started with C
  • C Syntax
  • C Output
  • C Comments
  • C Variables
  • C Data Types
  • C Constants
  • C Operators
  • C Booleans
  • C If...Else Statements
  • C Switch Statement
  • C While Loops
  • C For Loops
  • C Break and Continue
  • C Strings
  • C User Input
  • C Memory Address
  • C Pointers
  • C Files
  • C Functions

  • C Functions
  • C Function Parameters
  • C Scope
  • C Function Declaration
  • C Recursion
  • C Math Functions
  • C Structures

  • C Structures
  • C Structs & Pointers
  • C Unions
  • C Enums

  • C Enums
  • C Memory

  • C Allocate Memory
  • C Access Memory
  • C Reallocate Memory
  • C Deallocate Memory
  • C Structs and Memory
  • C Memory Example
  • C Quiz

  • C Quiz
  • C Basics

  • Introduction to C
  • Getting Started with C
  • C Syntax
  • C Output
  • C Comments
  • C Variables
  • C Data Types
  • C Constants
  • C Operators
  • C Booleans
  • C If...Else Statements
  • C Switch Statement
  • C While Loops
  • C For Loops
  • C Break and Continue
  • C Strings
  • C User Input
  • C Memory Address
  • C Pointers
  • C Files
  • C Functions

  • C Functions
  • C Function Parameters
  • C Scope
  • C Function Declaration
  • C Recursion
  • C Math Functions
  • C Structures

  • C Structures
  • C Structs & Pointers
  • C Unions
  • C Enums

  • C Enums
  • C Memory

  • C Allocate Memory
  • C Access Memory
  • C Reallocate Memory
  • C Deallocate Memory
  • C Structs and Memory
  • C Memory Example
  • C Quiz

  • C Quiz

Loading C tutorial…

Loading content
C FunctionsTopic 48 of 64
←PreviousPrevNextNext→

C Functions

Introduction to Functions

Functions are fundamental building blocks in C programming that allow you to organize code into reusable, modular units. A function is a self-contained block of code that performs a specific task.

Key benefits of using functions:

- Code reusability: Write once, use multiple times

- Modularity: Break complex problems into smaller, manageable pieces

- Maintainability: Easier to debug and update code

- Readability: Makes programs more organized and understandable

Function Syntax

Example
return_type function_name(parameter_list) {
    // function body - statements that perform the task
    return value; // optional depending on return type
}
ℹ️ Note: The return type specifies what type of value the function returns. Use 'void' if the function doesn't return any value. Use 'void' in the parameter list to indicate no parameters (e.g., void f(void)). Declare (prototype) functions before use—typically in a header file for multi-file programs.

Function Components

ComponentDescriptionExample
Return TypeData type of value returned by functionint, float, void, char, etc.
Function NameIdentifier used to call the functionadd, calculate, printResult
ParametersVariables that receive values passed to function(int a, int b)
Function BodyStatements that define what the function does{ return a + b; }
Return StatementReturns a value to the caller (optional for void)return result;

Basic Function Example

Example
#include <stdio.h>

// Function declaration (prototype)
int multiply(int a, int b);

int main(void) {
    int x = 5, y = 3;
    
    // Function call
    int result = multiply(x, y);
    printf("%d * %d = %d\n", x, y, result);
    
    return 0;
}

// Function definition
int multiply(int a, int b) {
    return a * b;
}
Output
5 * 3 = 15
ℹ️ Note: Functions must be declared before use. The declaration can be separate from the definition (commonly placed in a header file).

Types of Functions

C supports different types of functions based on their return values and parameters:

  • Built-in functions (library functions): Predefined in C standard library (printf, scanf, etc.)
  • User-defined functions: Created by programmers to perform specific tasks
  • Functions with return values: Return data to the caller
  • Void functions: Don't return any value
  • Functions with parameters: Accept input values
  • Functions with no parameters: Use a 'void' parameter list (e.g., void f(void))

Function Call Mechanism

When a function is called:

1. Program control transfers to the function

2. Parameters are passed (if any)

3. Function executes its statements

4. Return value is sent back (if any)

5. Control returns to the point after the function call

Example
#include <stdio.h>

void greet(void) {
    printf("Function: Hello from inside the function!\n");
}

int main(void) {
    printf("Main: Before function call\n");
    greet(); // Function call
    printf("Main: After function call\n");
    return 0;
}
Output
Main: Before function call
Function: Hello from inside the function!
Main: After function call
Test your knowledge: C Functions
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
C FunctionsTopic 48 of 64
←PreviousPrevNextNext→