Itp Unit-5
Itp Unit-5
SYLLABUS
1) Introduction to Functions,
web-D Page 1
SEM :- 1-1 (R23) introduction to programming UNIT-5
1.) Functions
Certainly! Let’s dive into the fundamental concepts of functions, including function
declaration and function definition, in C programming.
1. Functions in C:
o A function in C is a set of statements that, when called, performs a specific
task.
o Functions serve as the basic building blocks of a C program, providing
modularity and code reusability.
o The statements within a function are enclosed within curly braces { }.
o Functions are also known as subroutines or procedures in other programming
languages.
2. Syntax of Functions in C:
o The syntax of a function can be divided into three aspects:
web-D Page 2
SEM :- 1-1 (R23) introduction to programming UNIT-5
Function Declaration: Specifies the function name, its return type, and
the number and type of its parameters.
Function Definition: Contains the actual statements executed when the
function is called.
Function Calls: Instructs the compiler to execute the function.
o Let’s explore each aspect:
3. Function Declaration:
o In a function declaration, we provide:
The function name.
Its return type.
The number and type of its parameters (if any).
o A function declaration informs the compiler that a function with the given
name is defined elsewhere in the program.
o Example:
4. Function Definition:
o The function definition consists of the actual statements executed when the
function is called.
o Typically, a C function is defined and declared in a single step.
o Example:
Here, the function sum takes two integer parameters and returns their sum.
web-D Page 3
SEM :- 1-1 (R23) introduction to programming UNIT-5
5. Function Call:
o A function call instructs the compiler to execute the function.
o We use the function name and provide the necessary arguments.
o Example:
o int main() {
o int result = sum(10, 30); // Calling the sum function
o printf("Sum is: %d", result);
o return 0;
o}
Output:
Sum is: 40
Certainly! Here’s a simple example of a C function that prints a message without any
return statement:
#include <stdio.h>
int main() {
int a = 1;
display(a); // Calling the display function
return 0;
web-D Page 4
SEM :- 1-1 (R23) introduction to programming UNIT-5
// Function definition
void display(int x) {
printf("x = %d\n", x);
}
In this program:
We declare a function named display with the void return type (since it doesn’t
return any value).
Inside main(), we call the display function and pass the value of a.
The display function prints the value of x.
1. When you pass a pointer to a function, you can modify the value at the memory
location pointed to by that pointer. This is known as call by reference.
o Example: Swapping two integers using pointers:
#include <stdio.h>
int main() {
int x = 10, y = 20;
printf("Before swap: x = %d, y = %d\n", x, y);
web-D Page 5
SEM :- 1-1 (R23) introduction to programming UNIT-5
swap(&x, &y);
printf("After swap: x = %d, y = %d\n", x, y);
return 0;
}
Output:
Arrays as Parameters:
o You can pass arrays to functions in C.
o When you pass an array to a function, you’re actually passing a pointer to the
first element of the array.
o Example: Sum of array elements using a function:
#include <stdio.h>
int main() {
int myArray[] = {1, 2, 3, 4, 5};
int arraySize = sizeof(myArray) / sizeof(myArray[0]);
printf("Sum of array elements: %d\n", sumArray(myArray,
arraySize));
return 0;
web-D Page 6
SEM :- 1-1 (R23) introduction to programming UNIT-5
Output:
#include <stdio.h>
void myFunction() {
int localVar = 10; // Local variable
printf("Local variable: %d\n", localVar);
}
int main() {
printf("Global variable: %d\n", globalVar);
myFunction();
return 0;
}
Output:
web-D Page 7
SEM :- 1-1 (R23) introduction to programming UNIT-5
Global variable: 42
Local variable: 10
----------------------------------------------------------------------
#include <stdio.h>
int main() {
FILE* filePointer;
filePointer = fopen("myFile.txt", "w"); // Open for
writing
if (filePointer != NULL) {
fprintf(filePointer, "Hello, World!\n");
fclose(filePointer);
printf("Data written to file.\n");
} else {
printf("Error opening file.\n");
}
return 0;
}
web-D Page 8