Semester-1 B.Tech.
-ESC 103 Introduction
to UNIX and C Programming
Dr. Prakash Kumar
SARALA BIRLA UNIVERSITY
Semester-1 B.Tech.-ESC 103 Introduction to UNIX and C Programming
Module 3
C Functions
In C, a function is a self-contained block of code that performs a specific task or a set of tasks. Functions
are a fundamental concept in structured programming, and they help in organizing code, making it more
modular and easier to manage. Here's the basic structure of a function in C:
return_type function_name(parameter1_type parameter1, parameter2_type parameter2, ...)
{
// Function body
// Statements to perform a task
// Return a value if the function has a return type
}
Types of function
There are two types of function in C programming:
Standard library functions
User-defined functions
Standard library functions
C provides a standard library, often referred to as the C Standard Library or the Standard Template Library
(STL), which contains a set of built-in functions and header files to perform various operations and tasks.
These functions are part of the C standard and are available for use in any C program. Here are some of
the common categories of standard library functions in C:
Input and Output (I/O):
printf(): Used for formatted output to the console.
scanf(): Used for formatted input from the console.
getchar(), putchar(): Used for character-based I/O.
fgets(), fputs(): Used for reading and writing strings.
fread(), fwrite(): Used for binary I/O.
Math Functions:
sqrt(), pow(): For square root and exponentiation.
sin(), cos(), tan(): Trigonometric functions.
abs(), labs(), llabs(): For absolute values.
rand(), srand(): For random number generation.
Header files in C:
Header files in C are files that contain declarations, function prototypes, macro definitions, and other
information that can be shared across multiple source code files. They serve to provide a consistent and
organized way to manage declarations and interface information for your C programs. Standard C header
files are usually enclosed in angle brackets (<>), while user-defined header files are enclosed in double
quotes ("").
Semester-1 B.Tech.-ESC 103 Introduction to UNIX and C Programming
User-defined function
You can also create functions as per your need. Such functions created by the user are known as user-
defined functions.
#include <stdio.h>
void functionName()
{
... .. ...
... .. ...
}
int main()
{
... .. ...
... .. ...
functionName();
... .. ...
... .. ...
}
Types of User-defined Functions
Example 1: No Argument Passed and No Return Value
#include <stdio.h>
// Function prototype (declaration)
void greet(); // Function takes no arguments and returns no value
int main() {
// Call the greet function
greet();
return 0;
}
// Function definition
void greet() {
printf("Hello, World! This is a function with no arguments and no return value.\n");
}
Semester-1 B.Tech.-ESC 103 Introduction to UNIX and C Programming
Example 2: No Arguments Passed But Returns a Value
#include <stdio.h>
// Function prototype (declaration)
int getNumber(); // Function takes no arguments but returns an integer value
int main()
{
int Number;
Number = getNumber();
printf("Number: %d\n", Number);
return 0;
}
// Function definition
int getNumber()
{
return (100); // This function returns a value 100
}
Example 3: Argument Passed But No Return Value
#include <stdio.h>
// Function prototype (declaration)
void printVal(int x1); // Function takes an argument but returns no value
int main()
{
int x=100;
printVal(x);
return 0;
}
// Function definition
void printVal(int x1)
{
printf("Value from the function: %d\n", x1);
}
Semester-1 B.Tech.-ESC 103 Introduction to UNIX and C Programming
Example 4: Argument Passed and Returns a Value
#include <stdio.h>
// Function prototype (declaration)
int addNumbers(int a, int b); // Function takes two integers as arguments and returns an integer value
int main() {
int num1, num2, sum;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
// Call the addNumbers function, passing num1 and num2 as arguments, and store the result in 'sum'
sum = addNumbers(num1, num2);
printf("Sum: %d\n", sum);
return 0;
}
// Function definition
int addNumbers(int a, int b) {
return a + b; // This function returns the sum of the two numbers
}
Passing Parameters to Functions
The data passed when the function is being invoked is known as the Actual parameters. In the below
program, 10 and 30 are known as actual parameters. Formal Parameters are the variable and the data
type as mentioned in the function declaration. In the below program, a and b are known as formal
parameters.
We can pass arguments to the C function in two ways:
1. Pass by Value
2. Pass by Reference
1. Pass by Value
Parameter passing in this method copies values from actual parameters into formal function
parameters. As a result, any changes made inside the functions do not reflect in the caller’s
parameters.
Example:
// C program to show use
// of call by value
#include <stdio.h>
void swap(int a, int b)
{
int temp = a;
Semester-1 B.Tech.-ESC 103 Introduction to UNIX and C Programming
a = b;
b = temp;
}
// Driver code
int main()
{
int a = 3, b = 2;
printf("Before swap Value of a and b is: %d, %d\n",a, b);
swap(a, b);
printf("After swap Value of a and b is: %d, %d",a, b);
return 0;
}
2. Pass by Reference
The caller’s actual parameters and the function’s actual parameters refer to the same locations, so any
changes made inside the function are reflected in the caller’s actual parameters.
Example:
// C program to show use of
// call by Reference
#include <stdio.h>
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
// Driver code
int main()
{
int a = 3, b = 2;
printf("Before swap Value of a and var2 is: %d, %d\n",a, b);
swap(&a, &b);
printf("After swap Value of a and b is: %d, %d",a, b);
return 0;
}
C Recursion
A function that calls itself is known as a recursive function. And, this technique is known as recursion.
#include <stdio.h>
// Function to calculate the factorial using recursion
int factorial(int n)
{
Semester-1 B.Tech.-ESC 103 Introduction to UNIX and C Programming
if (n == 0)
{
// Base case: 0! is 1
return 1;
}
else
{
// Recursive case: n! = n * (n-1)!
return n * factorial(n - 1);
}
}
int main()
{
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
if (num < 0)
{
printf("Factorial is not defined for negative numbers.\n");
}
else
{
int result = factorial(num);
printf("Factorial of %d is %d\n", num, result);
}
return 0;
}
Passing an Array as a Pointer:
In this method, you pass the base address (or pointer) of the array to the function. The function receives
it as a pointer and can manipulate the original array.
#include <stdio.h>
// Function that takes an array and its size as arguments
void printArray(int arr[], int size)
{
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
}
Semester-1 B.Tech.-ESC 103 Introduction to UNIX and C Programming
int main() {
int numbers[] = {1, 2, 3, 4, 5};
int size = 5;
printArray(numbers, size);
return 0;
}
Macro in C:
In C, a macro is a fragment of code that is given a name. When the name is encountered in your code, it is
replaced by the code fragment. A macro function (also known as a macro) is defined using the #define
preprocessor directive and is a simple way to create inline code substitutions. Macro functions are not
true functions; they are text substitutions performed by the preprocessor before the actual compilation
of the code.
1.
#include <stdio.h>
// Define a simple macro function
#define SQUARE(x) ((x) * (x))
int main() {
int num = 5;
int result;
result = SQUARE(num); // Expanded as: ((num) * (num))
printf("The square of %d is %d\n", num, result);
return 0;
}
2.
#include<stdio.h>
#define SWAP(x, y, temp) temp = x; x = y; y = temp;
int main()
{
int a, b, temp;
printf("Enter 2 integer numbers\n");
scanf("%d%d", &a, &b);
printf("Before swapping: a = %d and b = %d\n", a, b);
SWAP(a, b, temp);
printf("After swapping: a = %d and b = %d\n", a, b);
return 0;
}
Semester-1 B.Tech.-ESC 103 Introduction to UNIX and C Programming
Macro Function
Macros are Pre-processed Functions are Compiled
No Type Checking is done in Macro Type Checking is Done in Function
Using Function keeps the code length
Using Macro increases the code length
unaffected
Functions do not lead to any side effect in any
Use of macro can lead to side effect at later stages
case
Speed of Execution using Macro is Faster Speed of Execution using Function is Slower
Before Compilation, macro name is replaced by During function call, transfer of control takes
macro value place
Macros are useful when small code is repeated many Functions are useful when large code is to be
times written
Macro does not check any Compile-Time Errors Function checks Compile-Time Errors
______eof_Module3______