ASSIGNMENT TOPIC: FUNCTIONS
GROUP 2 MEMBERS
NAME ADM NUMBER SIGNATURE
Daisy Cherotich Kirui 2025CS163462
Maina Daniel 2025CS163597
Sirengo Anold 2025CS164221
Cheruiyot Enock 2025CS164192
Ann Wanjiku 2025CS163376
Simon Mbugua 2025CS164066
Philip Juma Simiyu 2025CS163881
Rahab Njoki 2025CS164271
Dickson Opiyo 2025CS163672
Weldon Cheruiyot 2025CS163456
FUNCTION
A function is a block of code that performs a specific task in a program.
Every C program has at least one function, which is main (), and all the most trivial programs
can define additional functions.
Why Functions are important in programming
1. Enables code reusability ie a function can be used multiple times in different parts of the
program without rewriting the same logics.
2. It improves readability. Functions help organize code logically making it easier to read
and modify.
3. Modularity. Breaks a large program into smaller, manageable sections.
Types of Functions
a. Library Functions
Library functions are the in-built function in C programming system.
For example:
printf() - is used for displaying output.
scanf() - is used for taking input.
Pow() – calculates the power of a number.
Sqrt() – calculates the square root of a number.
strcmp() – used to compare two strings lexicographically (character by character) and returns an
integer indicating their relationship.
strcpy() - copies a string from a source location to a destination location.
b. User Defined Functions
Defined by programmers according to their requirements.
Syntax of user-defined function:
return_type function_name(parameter_list) { // Function body }.
This includes a return_type (e.g., int, void), a function_name, a parameter_list (optional), and
a function_body enclosed in curly braces.
Function Declaration
A function declaration tells the compiler about a function name and how to call the function. The
actual body of the function can be defined separately.
A function declaration has the following parts:
return_type function_name( parameter list );
Function Definition
A function definition provides the actual body of the function.
The general form of a function definition in C programming language is as follows:
return_type function_name( parameter list ) { body of the function }
A function definition in C programming language consists of a function header and a function
body. Here are all the parts of a function:
Return Type: A function may return a value. The return_type is the data type of the value the
function returns. Some functions perform the desired operations without returning a value. In this
case, the return_type is the keyword void.
Function Name: This is the actual name of the function. The function name and the parameter
list together constitute the function signature.
Parameters: A parameter is like a placeholder. When a function is invoked, you pass a value to
the parameter. This value is referred to as actual parameter or argument. The parameter list refers
to the type, order, and number of the parameters of a function. Parameters are optional; that is, a
function may contain no parameters.
Function Body: The function body contains a collection of statements that define what the
function does.
Calling a Function
While creating a C function, you give a definition of what the function has to do. To use a
function, you will have to call that function to perform the defined task.
A called function performs defined task, and when its return statement is executed or when its
function-ending closing brace is reached, it returns program control back to the main program.
To call a function, you simply need to pass the required parameters along with function name,
and if function returns a value, then you can store returned value.
Function Arguments/Parameters
If a function is to use arguments, it must declare variables that accept the values of the
arguments. These variables are called the formal parameters of the function.
The formal parameters behave like other local variables inside the function and are created upon
entry into the function and destroyed upon exit.
While calling a function, there are two ways that arguments can be passed to a function:
a. Function call by value
The call by value method of passing arguments to a function copies the actual value of an
argument into the formal parameter of the function. In this case, changes made to the parameter
inside the function have no effect on the argument.
By default, C programming language uses call by value method to pass arguments. In general,
this means that code within a function cannot alter the arguments used to call the function.
Consider the function calculateArea() definition as follows.
#include <stdio.h>
int calculateArea(int length, int width) {
return length * width;
}
int main() {
int length = 10, width = 5, area;
// Call the function by value to get the area
area = calculateArea(length, width);
printf("Length: %d, Width: %d\n", length, width);
printf("Area of the rectangle: %d\n", area);
return 0;
}
Output of the above code
Length: 10, Width: 5
Area of the rectangle: 50
b. Function call by reference
The call by reference method of passing arguments to a function copies the address of an
argument into the formal parameter. Inside the function, the address is used to access the actual
argument used in the call. This means that changes made to the parameter affect the passed
argument.
To pass the value by reference, argument pointers are passed to the functions just like any other
value. So accordingly you need to declare the function parameters as pointer types as in the
following function calculateArea().
#include <stdio.h>
// Function to calculate the area of a rectangle
void calculateArea(int length, int width, int *area) {
*area = length * width;
}
int main() {
int length = 10, width = 5, area;
// Call the function by reference to get the area
calculateArea(length, width, &area);
printf("Length: %d, Width: %d\n", length, width);
printf("Area of the rectangle: %d\n", area);
return 0;
}
Output
Length: 10, Width: 5
Area of the rectangle: 50