C User-defined Functions are self-contained blocks of code that perform a specific task. They are the building blocks of modular programming, allowing you to break down a large program into smaller, manageable, and reusable pieces. Every C program has at least one function, `main()`, but creating your own functions is essential for writing clean and efficient code.
Chapter Information
Chapter 7: User-defined Functions (6 hours) – 6 marks
Course: Computer Programming (CT 101), I Year I Part
Description: This guide provides a complete overview of C User-defined Functions, covering declaration, definition, parameters, scope, recursion, and calling mechanisms, as per the IOE syllabus.
Credit: Sujan Karki
Table of Contents
Chapter 7: User-defined Functions
6 hours
6 marks
Detailed Chapter Notes
7.1 Introduction & Advantages of C User-defined Functions
A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing.
Advantages of Functions:
Code Reusability: Write a function once and call it multiple times.
Reduced Complexity: Breaks a large program into smaller, understandable pieces.
Easier Debugging: It’s easier to find and fix errors in a smaller, isolated function.
Faster Development: Work can be divided among programmers, each working on different functions.
Enables Recursion: A function can call itself to solve complex problems.
7.2 Elements of User-defined Function
To use a function, you must first define it. The three key elements are:
Function Declaration (Prototype): Tells the compiler about the function’s name, return type, and parameters. It’s like a blueprint.
Function Definition: Contains the actual statements that will be executed when the function is called.
Function Call: The statement that invokes the function to perform its task.
7.3 Storage Class
The storage class of a variable determines its scope, lifetime, and initial value.
Local Variables: Declared inside a function or block. They are only accessible within that block and are destroyed when the block is exited. Their initial value is garbage.
Global Variables: Declared outside all functions. They are accessible throughout the entire program and have a lifetime equal to the program’s runtime. They are initialized to 0 by default.
Static Variables: Declared with the `static` keyword inside a function. They have local scope but their lifetime is the entire program’s execution. They retain their value between function calls and are initialized to 0 by default.
7.4 Scope Rules
Scope defines the region of the code where a variable is accessible.
Local Scope: The scope of a local variable is limited to the function or block in which it is declared.
Global Scope: The scope of a global variable extends from the point of declaration to the end of the entire program file.
7.5 Category of Functions
Functions can be categorized based on their parameters and return values.
No arguments and no return value: Performs a task without taking input from or giving output to the calling function.
With arguments and no return value: Receives data from the calling function but does not return a value.
With arguments and a return value: Receives data and returns a computed value back to the calling function. This is the most common type.
No arguments but a return value: Does not take input but returns a value (e.g., a function that reads from a sensor and returns the value).
7.6 Recursive functions
A recursive function is a function that calls itself, either directly or indirectly. It must have two key components:
Base Case: A stopping condition that prevents the function from calling itself infinitely.
Recursive Step: The part where the function calls itself with a modified argument that moves it closer to the base case.
Example: Factorial of a Number using Recursion
#include <stdio.h>
long int factorial(int n) {
// Base Case: if n is 0 or 1, factorial is 1
if (n <= 1) {
return 1;
}
// Recursive Step: n * factorial of (n-1)
else {
return (n * factorial(n - 1));
}
}
int main() {
int num = 5;
printf("Factorial of %d is %ld\n", num, factorial(num));
return 0;
}
7.7 Function Call by Value and Reference
Call by Value
This is the default method of passing arguments in C. A copy of the actual argument's value is passed to the function. Any changes made to the parameter inside the function do not affect the original argument in the calling code.
Call by Reference
In this method, the memory address of the argument is passed to the function using pointers. This means the function can directly access and modify the original variable. Changes made inside the function are reflected in the calling code.
The educational materials provided on this website are intended as supplementary resources to support your learning journey. These study materials are sample documents designed to help students understand complex concepts in C Programming.
We have made every effort to ensure the accuracy of the content. However, we recommend students to refer to standard textbooks and consult with professors for authoritative explanations. These materials should be used as references only.