0% found this document useful (0 votes)
41 views8 pages

Itp Unit-5

Introduction to programming in c unit 5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views8 pages

Itp Unit-5

Introduction to programming in c unit 5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

SEM :- 1-1 (R23) introduction to programming UNIT-5

SYLLABUS

1) Introduction to Functions,

2) Function Declaration and Definition,

3) Function call Return Types and Arguments,

4) modifying parameters inside functions using

pointers, arrays as parameters.

5) Scope and Lifetime of Variables,

6.) Basics of File Handling

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:

int sum(int a, int b); // Function declaration

Note: A function in C must always be declared globally before calling it.

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:

int sum(int a, int b) {


return a + b; // Body of the function
}

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

6. Function Return Type:


o The return type specifies what type of value the function returns after
execution.
o If a function doesn’t return a value, we use the void data type.
o Example:
o int func(int a, int b); // This function returns an integer

Certainly! Here’s a simple example of a C function that prints a message without any
return statement:

#include <stdio.h>

// Function declaration or prototype


void display(int x);

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.

Certainly! Let’s explore each of these topics:

Modifying Parameters Inside Functions Using Pointers:

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>

void swap(int* a, int* b) {


int temp = *a;
*a = *b;
*b = temp;
}

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:

Before swap: x = 10, y = 20


After swap: x = 20, y = 10

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 sumArray(int arr[], int size) {


int sum = 0;
for (int i = 0; i < size; i++) {
sum += arr[i];
}
return sum;
}

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:

Sum of array elements: 15

Scope and Lifetime of Variables:


o The scope of a variable in C is the region where it is valid to refer to the
variable using its name.
o Variables can have local scope (within a block or function), global scope
(outside any block or function), or function scope (within a specific function).
o The lifetime of a variable is the time it remains in memory.
o Example:

#include <stdio.h>

int globalVar = 42; // Global variable

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
----------------------------------------------------------------------

Basics of File Handling:

o File handling in C involves creating, opening, reading, writing, and closing


files.
o Functions like fopen(), fwrite(), fread(), fseek(), and fclose() are
used for file operations.
o Types of files: text files (ASCII characters) and binary files (0s and 1s).
o Example: Writing to a text file:

#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;
}

Creates a file named “myFile.txt” with the content “Hello, World!”.

Remember, understanding these concepts is essential for effective C programming!

web-D Page 8

You might also like