KEMBAR78
Unit 5 Functions | PDF | Parameter (Computer Programming) | Pointer (Computer Programming)
0% found this document useful (0 votes)
32 views5 pages

Unit 5 Functions

The document provides an overview of functions in C, explaining their declaration, definition, and usage, as well as the importance of function prototypes and built-in libraries. It details parameter passing methods, including 'Call by Value' and 'Call by Reference', and illustrates these concepts with examples. Additionally, it demonstrates how to use the <stdio.h> library for input and output operations in C programs.

Uploaded by

njnim55
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)
32 views5 pages

Unit 5 Functions

The document provides an overview of functions in C, explaining their declaration, definition, and usage, as well as the importance of function prototypes and built-in libraries. It details parameter passing methods, including 'Call by Value' and 'Call by Reference', and illustrates these concepts with examples. Additionally, it demonstrates how to use the <stdio.h> library for input and output operations in C programs.

Uploaded by

njnim55
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/ 5

Functions in C:

Functions in C are blocks of code that perform a specific task. They are an essential part of the
C programming language and are used to break down a program into smaller, manageable units.
Functions make code more modular, reusable, and easier to understand. Here's how to work with
functions in C:

1. Function Declaration:

A function in C is declared with a specific name, return type, and parameter list. The
declaration tells the compiler what to expect when the function is called.

return_type function_name(parameter_list);

return_type : Specifies the type of value that the function will return. If the
function doesn't return a value, use void .

function_name : The name of the function. It must be a valid identifier.

parameter_list : A list of parameters (input values) that the function accepts. If


the function doesn't take any parameters, use void .

Example of a function declaration:

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

Function Prototypes
In C, it's a good practice to declare a function prototype before using a function. A
function prototype provides the compiler with information about the function's return
type and parameters. For example:

int add(int a, int b);

This way, the compiler knows what to expect when it encounters the function call later in
the code.

2. Function Definition:

The function definition provides the actual code that the function will execute. It
includes the function's name, return type, parameter list, and function body.

Example of a function definition:

int add(int a, int b) { // Function definition


int sum = a + b;
return sum;
}

In this example:

int is the return type, indicating that the function will return an integer.

add is the function name.


(int a, int b) is the parameter list, which specifies that the function accepts
two integer parameters a and b .

The function calculates the sum of a and b and returns the result using the
return statement.
3. Function Call:

To use a function, you call it by its name and provide the required arguments (if any).
The function executes its code and may return a value.

Example of a function call:

int result = add(5, 3); // Function call

This code calls the add function with the arguments 5 and 3 and stores the result in
the result variable.

4. Built-in Libraries:

C provides a standard library, which includes pre-defined functions for a wide range of
tasks. These libraries are included using the #include directive.

For example, you can include the <stdio.h> library to access functions for input and
output operations (e.g., printf , scanf ).

Example of including a library:

#include <stdio.h>

Function Header and Body


A function is divided into two parts: the function header (declaration) and the function body
(definition). The function header specifies the function's name, return type, and parameter
list. The function body contains the actual code to perform the desired task.

Here's an example of a complete function:

int add(int a, int b); // Function prototype

int main() {
int result = add(5, 3); // Function call
printf("The result is %d\n", result);
return 0;
}

int add(int a, int b) { // Function definition


int sum = a + b;
return sum;
}

This program defines a function add , declares it before main , and then defines it later in the
code.
Example of Using Built-in Libraries and Functions:
Here's an example of a simple C program that uses the <stdio.h> library to read user input and
display output:

#include <stdio.h>

int main() {
int num1, num2, sum;

printf("Enter two numbers: ");


scanf("%d %d", &num1, &num2);

sum = num1 + num2;

printf("The sum of %d and %d is %d\n", num1, num2, sum);

return 0;
}

In this program:

We include the <stdio.h> library to access functions like printf and scanf .

We use the printf function to display a message and read two numbers from the user
using scanf .

We perform a simple addition operation and use printf again to display the result.

By using functions and built-in libraries, you can write more organized and efficient C programs
for a wide range of tasks, from basic input/output to complex calculations and data processing.
Parameter passing in functions,
In C, parameters can be passed to functions in two primary ways: "Call by Value" and "Call by
Reference" (achieved using pointers). Understanding the difference between these two methods is
crucial for effective parameter passing in functions.

1. Call by Value:

In the "Call by Value" parameter passing mechanism, the function receives a copy of the
actual argument's value.

Any changes made to the parameter within the function do not affect the original
argument outside the function.

It is the default parameter passing mechanism in C for basic data types like int, float,
char, etc.

Example:

void modifyValue(int x) {
x = x + 10;
}

int main() {
int num = 5;
modifyValue(num);
// 'num' remains 5, as 'modifyValue' operates on a copy of 'num'
return 0;
}

2. Call by Reference (Using Pointers):

In "Call by Reference," you pass a reference to the actual argument by using pointers as
parameters. This means that the function operates on the original data located at the
memory address pointed to by the parameter.

Any changes made to the parameter within the function affect the original argument
outside the function.

Example:

void modifyValueByReference(int *x) {


*x = *x + 10;
}

int main() {
int num = 5;
modifyValueByReference(&num);
// 'num' becomes 15, as 'modifyValueByReference' operates on 'num'
directly
return 0;
}

3. Arrays as Parameters:

When you pass an array to a function, you are effectively passing a pointer to the first
element of the array.
Changes made to the elements of the array within the function affect the original array
outside the function (similar to "Call by Reference").

Example:

void modifyArray(int arr[], int size) {


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

int main() {
int numbers[] = {5, 10, 15};
modifyArray(numbers, 3);
// 'numbers' becomes {15, 20, 25} due to modifications made by
'modifyArray'
return 0;
}

By understanding these two parameter passing mechanisms, you can choose the appropriate one
for your needs when defining and using functions in C. "Call by Value" is suitable when you want
to work with a copy of the data, while "Call by Reference" (using pointers) allows you to modify the
original data directly.

You might also like