0% found this document useful (0 votes)
4 views19 pages

Functions

The document provides an overview of functions in C programming, detailing their types, parts, and advantages. It explains library and user-defined functions, along with their declarations, definitions, and calls. Additionally, it covers different function types based on arguments and return values, and includes examples and exercises related to function usage with arrays.

Uploaded by

Raghav Gupta
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)
4 views19 pages

Functions

The document provides an overview of functions in C programming, detailing their types, parts, and advantages. It explains library and user-defined functions, along with their declarations, definitions, and calls. Additionally, it covers different function types based on arguments and return values, and includes examples and exercises related to function usage with arrays.

Uploaded by

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

Functions

Function
A function is a block of code that performs a specific task.
• It helps in:
• Code reusability
• Better readability
• Easy debugging
•A function is executed when it is called.
Types of Functions in C
1. Library Functions
• Predefined functions
• Example: printf(), scanf()

2. User-Defined Functions
• Created by programmer
• Example: add()
Parts of a Function
A function in C has three main parts:
• Function Declaration
• Function Definition
• Function Call
Function Declaration
Tells the compiler about:
• Function name
• Return type
• Parameters

Syntax
return_type function_name(parameter_list);

int add(int a, int b);


Function Definition
Contains the actual logic of the function.
Syntax
return_type function_name(parameter_list)
{
// statements
}
Example
int add(int a, int b)
{
return a + b;
}
Function Call
• Used to execute the function.
• Called from main().
Example:
sum = add(x, y);
Advantages of Using Functions
• Avoids repetition of code
• Improves program structure
• Makes program easy to maintain
Function Types in C (Based on Arguments & Return Type)

In C, user-defined functions are classified into four types:


1. No arguments & No return value
2. Arguments & No return value
3. No arguments & Return value
4. Arguments & Return value
1. No Arguments & No Return Value Function
• This function does not take any input from the #include <stdio.h>
calling function.
• It does not return any value. void add(); // function declaration
• All input/output operations are done inside
the function. int main()
{
Syntax add(); // function call
return 0;
void function_name() }
{
// statements void add() // function definition
{
} int a, b, sum;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
sum = a + b;
printf("Sum = %d", sum);
}
2. Arguments & No Return Value Function
• Takes input values as parameters. #include <stdio.h>
• Does not return any value.
• Result is printed inside the function. void add(int x, int y);

int main()
{
Syntax int a, b;
printf("Enter two numbers: ");
void function_name(type arg1, type arg2) scanf("%d %d", &a, &b);
{
// statements add(a, b); // function call
return 0;
} }

void add(int x, int y)


{
int sum = x + y;
printf("Sum = %d", sum);
}
3. No Arguments & Return Value Function
• Does not take input from the calling function. #include <stdio.h>
• Returns a value to main().
• Input is taken inside the function. int add();

int main()
{
Syntax int sum;
sum = add(); // function call
return_type function_name() printf("Sum = %d", sum);
{ return 0;
return value; }
} int add()
{
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
return a + b;
}
4. Arguments & Return Value Function
• Takes input through arguments. #include <stdio.h>
• Returns the computed result.
• Most flexible and commonly used type. int add(int x, int y);

int main()
{
int a, b, sum;
Syntax printf("Enter two numbers: ");
return_type function_name(type arg1, type arg2) scanf("%d %d", &a, &b);
{
return value; sum = add(a, b); // function call
} printf("Sum = %d", sum);
return 0;
}

int add(int x, int y)


{
return x + y;
}
4. Arguments & Return Value Function

Q.1 WAP to input two numbers from the keyboard and print their sum.
Q.2 WAP to input two numbers from the keyboard and print their average.
Q.3 WAP to calculate the area
Q.4 WAP to calculate circumference of circle.(circumference=2 *pi* r)
Q.5 WAP to print the total seconds in a given time (hrs,min,sec)
Passing Individual Array Element to a Function
#include <stdio.h>
// Function to display a single value
• User enters array elements.
void display(int x)
• Only one element of the array is passed to the {
function. printf("Value received in function: %d\n", x);
• Function receives a copy of the value, not the }
whole array. int main()
{
int arr[5];
int i, pos;
// Taking array elements from user
printf("Enter 5 array elements:\n");
for(i = 0; i < 5; i++)
{
scanf("%d", &arr[i]);
}
// Taking position from user
printf("Enter position of element to pass (0 to 4): ");
scanf("%d", &pos);
// Passing individual array element
display(arr[pos]);

return 0;
}
Passing Entire 1-D Array to a Function
#include <stdio.h>
// Function to display entire array
When an array is passed to a function: void displayArray(int arr[], int n)
{
• The base address of the array is passed int i;
• Function can access all elements printf("Array elements are:\n");
for(i = 0; i < n; i++)
• Changes inside the function affect the original {
printf("%d ", arr[i]);
array }
}
int main()
Syntax {
int arr[5];
return_type function_name(data_type array_name[], int i;
int size); // Taking user-defined values
OR printf("Enter 5 array elements:\n");
for(i = 0; i < 5; i++)
return_type function_name(data_type *array_name, {
int size); scanf("%d", &arr[i]);
}
// Passing whole array to function
displayArray(arr, 5);
return 0;
}
• Definition → main → call
• Declaration → main → call → definition
• main → call → definition
#include <stdio.h>
main()
{ int main()
Function Call {
} show(); // error
Function Definition return 0;
}

void show()
{
printf("Hello");
}
Questions
1. Write a C program to pass the third element of an array to a function and print it.
2. Write a function that receives a whole array and finds the sum of its elements.
3. Write a C program to modify array elements using a function.
4. Write a C program to display array elements using a user-defined function.

You might also like