- 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 User Defined Functions
![]() Share with a Friend |
C Programming - C User Defined Functions
C User-Defined Functions
A user-defined function in C is a function that is defined by the user, as opposed to built-in functions that are part of the C standard library. User-defined functions allow programmers to break down a program into smaller, manageable, and reusable blocks of code.
Functions in C can be broadly categorized into two types:
- Functions without return values (void functions)
- Functions with return values
Syntax of a User-Defined Function
A function definition in C generally consists of the following components:
- Return Type: The type of value the function will return (e.g., int, float, char, void).
- Function Name: The identifier that will be used to call the function.
- Parameters (Optional): The input data for the function, defined inside parentheses.
- Function Body: The block of code that contains the statements that are executed when the function is called.
General Syntax:
C
return_type function_name(parameter_list) {
// Function body
}
- return_type: The data type of the value that will be returned by the function. Use void if the function does not return any value.
- function_name: The name of the function (e.g., add, multiply).
- parameter_list: A comma-separated list of parameters that are passed to the function.
Types of User-Defined Functions in C
- Functions with Return Values
- These functions return a value after performing some operations.
- The return type is something other than void.
- Functions without Return Values (void functions)
- These functions do not return any value.
- The return type is void.
Example 1: Function with Return Value
This is an example of a simple function that adds two integers and returns the result.
C
#include <stdio.h>
// Function declaration
int add(int, int);
int main() {
int num1 = 10, num2 = 20;
int result = add(num1, num2); // Calling the user-defined function
printf("Sum: %d\n", result); // Printing the result
return 0;
}
// Function definition
int add(int a, int b) {
return a + b; // Returns the sum of a and b
}
Explanation:
- Function Declaration: int add(int, int); tells the compiler that there will be a function add which takes two integers as arguments and returns an integer.
- Function Definition: int add(int a, int b) { return a + b; } is where the actual logic of adding two numbers is implemented.
- Function Call: Inside main(), the add() function is called with two arguments, and the result is stored in the variable result.
Example 2: Function without Return Value (Void Function)
This example shows a function that prints the sum of two numbers but does not return any value.
C
#include <stdio.h>
// Function declaration
void printSum(int, int);
int main() {
int num1 = 10, num2 = 20;
printSum(num1, num2); // Calling the function that does not return anything
return 0;
}
// Function definition
void printSum(int a, int b) {
printf("Sum: %d\n", a + b); // Printing the sum
}
Explanation:
- Function Declaration: void printSum(int, int); specifies that the function printSum will take two integers and return no value.
- Function Definition: void printSum(int a, int b) contains the logic to print the sum of a and b.
- Function Call: Inside main(), the printSum() function is called with two arguments to print the sum.
Function Call Mechanism
When a function is called in C, the following steps occur:
- Function Call: The function is invoked with the arguments passed (if any).
- Function Execution: The function executes its code.
- Return Value: If the function has a return type, it returns a value to the caller. If it does not return a value, control is returned to the calling function once the function body completes.
Passing Arguments to Functions
In C, functions can be passed arguments in two ways:
- Call by Value: The function gets a copy of the actual argument value. Any changes made to the parameter inside the function do not affect the original value.
- Call by Reference: The function gets the address of the actual argument. Any changes made to the parameter will affect the original value.
Example 3: Call by Value
C
#include <stdio.h>
void square(int num) {
num = num * num; // Squaring the number inside the function
printf("Squared: %d\n", num);
}
int main() {
int value = 5;
square(value); // Passing the value by copy (call by value)
printf("Original value: %d\n", value); // The original value remains unchanged
return 0;
}
Explanation:
- The square function receives a copy of the value variable. Modifying num inside the function does not change the original value.
Example 4: Call by Reference
C
#include <stdio.h>
void square(int *num) {
*num = *num * *num; // Modifying the original value by dereferencing the pointer
}
int main() {
int value = 5;
square(&value); // Passing the address of the value (call by reference)
printf("Squared value: %d\n", value); // The original value is changed
return 0;
}
Explanation:
- In this case, we pass the address of value to the square function. The function modifies the original value by dereferencing the pointer.
Recursive Functions
A recursive function is a function that calls itself in its own definition. Recursive functions are useful for solving problems that can be broken down into smaller sub-problems.
Example of a Recursive Function (Factorial)
C
#include <stdio.h>
// Function declaration
int factorial(int n);
int main() {
int num = 5;
int result = factorial(num); // Calling the recursive function
printf("Factorial of %d is: %d\n", num, result);
return 0;
}
// Function definition (recursive)
int factorial(int n) {
if (n == 0) { // Base case
return 1;
} else {
return n * factorial(n - 1); // Recursive call
}
}
Explanation:
- The factorial function calls itself to calculate the factorial of n recursively.
- Base Case: The recursion stops when n equals 0, returning 1.
Advantages of User-Defined Functions
- Code Reusability: Functions allow code to be written once and used multiple times.
- Modularity: Functions break down a program into smaller, manageable parts.
- Improved Readability: Functions help make code more readable and maintainable.
- Abstraction: Functions allow you to hide complex logic behind simple function calls.
Summary
- User-Defined Functions in C are functions that allow you to perform specific tasks and operations.
- They can return values or perform actions without returning anything.
- Call by Value and Call by Reference are the two ways to pass arguments to a function.
- Recursive Functions are useful for problems that require repetitive actions and self-referential logic.
