- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C Functions
![]() Share with a Friend |
C Programming - C Functions
C Functions
A function in C is a block of code that performs a specific task. Functions allow for modular programming by breaking the program into smaller, manageable pieces. Once a function is defined, it can be called multiple times from different parts of the program, leading to more reusable and maintainable code.
Structure of a C Function
A typical C function consists of the following parts:
- Return Type: The type of value the function will return (e.g., int, float, char, void).
- Function Name: The identifier used to refer to the function.
- Parameters (optional): Input values passed into the function, enclosed in parentheses. If the function doesn't require input, the parameters list is empty.
- Body of the Function: The block of code enclosed in curly braces {} that defines what the function does.
Syntax of a Function
C
return_type function_name(parameter_list) {
// Body of the function
// Code to perform the task
}
- Return type: Specifies the data type of the value the function returns (e.g., int, float). If no value is returned, the return type is void.
- Function name: A unique identifier for the function (e.g., add, printMessage).
- Parameter list: A list of variables (parameters) that are passed to the function for processing. If no parameters are required, you can leave the parentheses empty or use void.
Example of a Function in C
C
#include <stdio.h>
// Function Declaration (Function Prototype)
void greet();
int main() {
// Function Call
greet();
return 0;
}
// Function Definition
void greet() {
printf("Hello, World!\n");
}
Explanation:
- The greet() function is a simple function that prints "Hello, World!" when called.
- The main() function calls greet() to execute its task.
Output:
Hello, World!
Types of Functions in C
- Standard Library Functions: These are predefined functions provided by the C standard library. Examples include printf(), scanf(), strcpy(), and malloc().
- User-Defined Functions: Functions that are created by the programmer to perform specific tasks. These are defined with the return_type function_name() syntax.
Function with Parameters
A function can accept parameters (also called arguments), which allow values to be passed to it for processing.
Syntax of Function with Parameters
C
return_type function_name(parameter1_type param1, parameter2_type param2) {
// Code using param1 and param2
}
Example of a Function with Parameters
C
#include <stdio.h>
// Function Declaration
int add(int a, int b);
int main() {
int result = add(5, 7); // Function Call with arguments
printf("Sum: %d\n", result);
return 0;
}
// Function Definition
int add(int a, int b) {
return a + b;
}
Explanation:
- The add() function takes two integers as parameters (a and b), adds them together, and returns the result.
- In the main() function, we call add(5, 7) and store the result in result.
Output:
Sum: 12
Function with Return Value
A function can return a value of a specific data type (e.g., int, float, char). This is done using the return keyword.
Syntax of Function with Return Value
C
return_type function_name(parameter_list) {
// Code to compute and return a value
return value;
}
Example of Function with Return Value
C
#include <stdio.h>
// Function Declaration
float multiply(float x, float y);
int main() {
float result = multiply(4.5, 2.0); // Function Call with return value
printf("Product: %.2f\n", result);
return 0;
}
// Function Definition
float multiply(float x, float y) {
return x * y;
}
Explanation:
- The multiply() function takes two float parameters, multiplies them, and returns the product.
- The main() function calls multiply(4.5, 2.0) and prints the result.
Output:
Product: 9.00
Return Types in C Functions
- Functions can return any data type, such as int, float, char, double, etc.
- If no value is returned, the return type is void.
Recursive Functions in C
A recursive function is one that calls itself in order to solve a problem. It is usually used for problems that can be broken down into smaller sub-problems, such as calculating factorials or Fibonacci numbers.
Example of a Recursive Function
C
#include <stdio.h>
// Function Declaration
int factorial(int n);
int main() {
int result = factorial(5); // Function Call
printf("Factorial of 5: %d\n", result);
return 0;
}
// Function Definition
int factorial(int n) {
if (n == 0) {
return 1; // Base case
}
return n * factorial(n - 1); // Recursive call
}
Explanation:
- The factorial() function calls itself with n - 1 until it reaches the base case n == 0, at which point it returns 1.
- This function computes the factorial of a given number.
Output:
Factorial of 5: 120
Inline Functions in C
An inline function is a function whose code is directly inserted into the place where the function is called. This can help reduce function call overhead.
Syntax of Inline Function
C
inline return_type function_name(parameters) {
// Function body
}
Example of Inline Function
C
#include <stdio.h>
// Inline Function Declaration
inline int square(int x) {
return x * x;
}
int main() {
int num = 4;
printf("Square of %d: %d\n", num, square(num));
return 0;
}
Explanation:
- The square() function is declared as inline, which means its code will be inserted directly where it is called.
- In the main() function, square(4) computes the square of 4.
Output:
Square of 4: 16
Advantages of Functions in C
- Modularity: Functions allow breaking a program into smaller, manageable pieces, making it easier to understand and maintain.
- Reusability: Functions can be called multiple times, eliminating the need for redundant code.
- Testing: Functions can be individually tested to ensure correctness before integrating them into the main program.
- Abstraction: Functions help abstract away the details of implementation, making the code more readable and easier to follow.
Summary of C Functions
- Functions are blocks of code that perform specific tasks.
- Functions can take parameters and return values.
- Functions can be recursive, allowing a function to call itself.
- Functions can be declared inline for performance optimization.
- Functions help with modularity, reusability, and maintainability of code.
