HW1248
HW1248
Functions in C are reusable blocks of code that perform specific tasks. They help organize
code, improve readability, and facilitate debugging.
A function in C is a set of statements that when called perform some specific tasks. It is the
basic building block of a C program that provides modularity and code reusability. The
programming statements of a function are enclosed within { } braces, having certain
meanings and performing certain operations. They are also called subroutines or procedures
in other languages.
Syntax of Functions in C
The syntax of function can be divided into 3 aspects:
1. Function Declaration
2. Function Definition
3. Function Calls
Function Declarations
In a function declaration, we must provide the function name, its return type, and the number
and type of its parameters. A function declaration tells the compiler that there is a function
with the given name defined somewhere else in the program.
Syntax
return_type name_of_the_function (parameter_1, parameter_2);
The parameter name is not mandatory while declaring functions. We can also declare the
function without using the name of the data variables.
Example
int sum(int a, int b); // Function declaration with parameter names
int sum(int , int); // Function declaration without parameter names
Function Definition
The function definition consists of actual statements which are executed when the function
is called (i.e. when the program control comes to the function).
A C function is generally defined and declared in a single step because the function
definition always starts with the function declaration so we do not need to declare it
explicitly. The below example serves as both a function definition and a declaration.
Function Call
A function call is a statement that instructs the compiler to execute the function. We use the
function name and parameters in the function call.
In the below example, the first sum function is called and 10,30 are passed to the sum
function. After the function call sum of a and b is returned and control is also returned back
to the main function of the program.
// C program to show function call and definition
#include <stdio.h>
// Function that takes two parameters a and b as inputs and returns their sum
return a + b;
// Driver code
int main()
return 0;
Example:
int func(parameter_1,parameter_2);
Function Arguments
Function Arguments (also known as Function Parameters) are the data that is passed to a
function.
Example:
int function_name(int var1, int var2);
Conditions of Return Types and Arguments
In C programming language, functions can be called either with or without arguments and
might return values. They may or might not return values to the calling functions.
1. Function with no arguments and no return value
2. Function with no arguments and with return value
3. Function with argument and with no return value
4. Function with arguments and with return value
Types of Functions
There are two types of functions in C:
1. Library Functions
2. User Defined Functions
1. Library Function
A library function is also referred to as a “built-in function”. A compiler package already
exists that contains these functions, each of which has a specific meaning and is included in
the package. Built-in functions have the advantage of being directly usable without being
defined, whereas user-defined functions must be declared and defined before being used.
For Example:
pow(), sqrt(), strcmp(), strcpy() etc.
// C program to implement
#include <math.h>
#include <stdio.h>
// Driver code
int main()
double Number;
Number = 49;
// library function
Number, squareRoot);
return 0;
// C program to show
// user-defined functions
#include <stdio.h>
return a + b;
// Driver code
int main()
{
int a = 30, b = 40;
// function call
return 0;
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>
// Driver code
int main()
{
int var1 = 3, var2 = 2;
printf("Before swap Value of var1 and var2 is: %d, %d\n",
var1, var2);
swap(var1, var2);
printf("After swap Value of var1 and var2 is: %d, %d",
var1, var2);
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:
// call by Reference
#include <stdio.h>
*var1 = *var2;
*var2 = temp;
// Driver code
int main()
swap(&var1, &var2);
var1, var2);
return 0;
Advantages of Functions in C
Functions in C is a highly useful feature of C with many advantages as mentioned below:
1. The function can reduce the repetition of the same statements in the program.
2. The function makes code readable by providing modularity to our program.
3. There is no fixed number of calling functions it can be called as many times as you
want.
4. The function reduces the size of the program.
5. Once the function is declared you can just use it without thinking about the internal
working of the function.
Disadvantages of Functions in C
The following are the major disadvantages of functions in C:
1. Cannot return multiple values.
2. Memory and time overhead due to stack frame allocation and transfer of program
control.
1. Utility of Functions
Modularity: Functions break the program into smaller, manageable, and reusable
modules.
Code Reusability: Once written, a function can be reused multiple times.
Improved Debugging: Errors can be isolated to specific functions, making
debugging easier.
Code Readability: By breaking down the logic into functions, the overall structure of
the code becomes cleaner and easier to understand.
Example:
A function in C can return a value to the caller using the return keyword.
The return type is specified in the function declaration and definition.
Example:
int main() {
int result = multiply(3, 4);
printf("Result: %d\n", result); // Output: 12
return 0;
}
Functions can also return other data types like float, char, void (no return value),
and even pointers.
Function Definition:
4. Recursion
Example:
int factorial(int n) {
if (n == 0) // Base case
return 1;
else
return n * factorial(n - 1); // Recursive call
}
int main() {
int result = factorial(5); // 5! = 5 * 4 * 3 * 2 * 1
printf("Factorial: %d\n", result); // Output: 120
return 0;
}
Key Notes:
o Recursive functions can be memory-intensive due to multiple function calls on
the stack.
o Iterative solutions are often preferred for performance-critical applications.
Summary
The size is the sum of the The size is equal to the size of
sizes of all members, with the largest member, with
Size padding if necessary. possible padding.
Structures in C
1. Structure Definition
A structure is a user-defined data type that groups variables of different types under one name.
A structure in C is a collection of variables, possibly of different types, under a single name.
Each member of the structure is allocated its own memory space, and the size of the structure
is the sum of the sizes of all its members.
Syntax
struct name {
member1 definition;
member2 definition;
…
memberN definition;
};
Example:
#include <stdio.h>
struct Student {
char name[50];
int age;
float grade;
};
int main() {
return 0;
}
Example:
#include <stdio.h>
#include <string.h>
struct Student {
int id;
char name[50];
float marks;
};
int main() {
// Initialize s2
s2.id = 102;
strcpy(s2.name, "Bob");
s2.marks = 89.0;
// Print details
return 0;
Unions in C
1. Union Definition
A union is a user-defined data type where all members share the same memory location. This
means only one member can hold a value at any time.
A union in C is similar to a structure, but with a key difference: all members of a union share
the same memory location. This means only one member of the union can store a value at
any given time. The size of a union is determined by the size of its largest member.
Syntax:
union name {
member1 definition;
member2 definition;
…
memberN definition;
};
Example:
Example:
#include <stdio.h>
union Data {
int i;
double d;
char c;
};
int main() {
return 0;
}
Initializing Union Variables: You can initialize only one member of a union at a time.
Example:
Similar to structures, use the dot operator (.) to access union members.
Since all members share the same memory, modifying one member affects others.
Example:
d1.i = 10;
d1.f = 3.14;
#include <stdio.h>
#include <string.h>
union Data {
int i;
float f;
char str[20];
};
int main() {
d1.i = 10;
d1.f = 3.14;
strcpy(d1.str, "Hello");
return 0;
Structures:
o Used for creating data models like student records, employee details, or custom
data types.
Unions:
o Useful in situations where memory usage is critical, such as in embedded
systems.
o Example: Creating data structures for protocols (e.g., interpreting network
packets).
---------------------------------------------xxxx-------------------------------------------------