0% found this document useful (0 votes)
3 views5 pages

Understanding Functions in C Programming

This document explains the concept of functions in C programming, highlighting their role in modular design, reusability, and simplicity. It covers standard library functions, user-defined functions, function declaration, definition, and calling, as well as parameter passing and return statements. Additionally, it introduces recursive functions with examples demonstrating their usage.

Uploaded by

sahilpawar8755
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Understanding Functions in C Programming

This document explains the concept of functions in C programming, highlighting their role in modular design, reusability, and simplicity. It covers standard library functions, user-defined functions, function declaration, definition, and calling, as well as parameter passing and return statements. Additionally, it introduces recursive functions with examples demonstrating their usage.

Uploaded by

sahilpawar8755
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Unit 4 : Functions

Concept of function :
● A function is a block of code that performs a specific task.
● It helps divide a large program into smaller, manageable parts (modules).
● Functions increase reusability — the same function can be used multiple times in a
program.
● Every C program must have at least one function: the main() function.
● Functions can be built-in (library) or user-defined.

Advantages of Modular Design :


● Reusability: Functions can be reused in multiple programs.
● Simplicity:A large program is divided into smaller functions (modules).
● Readability: Code becomes easier to read and understand
● Debugging: Error can be found and fixed easily.
● Teamwork: Multiple programmers can work on different functions at the same time.

Standard library functions:


● These are predefined functions provided by C libraries like <stdio.h>, <math.h>,
<string.h>.
● They help perform common tasks such as input/output, mathematical operations, and
string manipulation.
● You must include the header file where the function is defined.

Function Header File Purpose

printf() <stdio.h> Output text to screen

scanf() <stdio.h> Input from user

sqrt() <math.h> Calculate square root

strlen() <string.h> Find length of string

strcpy() <string.h> Copy one string to another

User defined functions :


● These are functions created by the programmer.
● Used to perform specific tasks not covered by library functions.
● Can have parameters (inputs) and return values (outputs).
● Must be declared, defined, and called properly.
Function Declaration (Prototype):
● Tells the compiler about function name, return type, and parameters.
● Placed before main() function.
● Syntax: return_type function_name(parameter_list);
● Example : int add(int, int);
Function Definition :
● Contains the actual code (body) of the function.
● Syntax :
int add(int a, int b) {
return a + b;
}
Function call:

● Used to execute the function in a program.


● Control jumps from main() to called function ,executes it and then comes back.
● Arguments are passed inside parentheses.
● The return value can be stored in variable .
● A function can be called multiple times to reuse code.
● Syntax:

function_name(arguments);
● Example : sum = add(10, 20);

Parameter passing (by value) :


● Parameters are a means of communication between calling function and called function.
● Any changes made inside the function do not affect the original variable.
● Example:
#include <stdio.h>
void change(int x) {
x = 100;
}
int main() {
int a = 10;
change(a);
printf("Value of a = %d\n", a); // remains unchanged
return 0;
}

Return statement :
● The return statement sends a value from a function to the calling function.
● It also terminates the function execution.
● The data type of returned value must match the function’s return type.
Syntax: return expression;

Program: Demonstration of All Function Concepts


#include <stdio.h> // Standard Input Output header

// ---------- Function Declaration (Prototype) ----------


int add(int x, int y);
int square(int n);

int main() {
int a, b, sum, result;

// ---------- Input from user ----------


printf("Enter two numbers: ");
scanf("%d %d", &a, &b);

// ---------- Function Call (by value) ----------


sum = add(a, b); // calling add() function
printf("Sum of %d and %d is: %d\n", a, b, sum);

// ---------- Passing return value to another function ----------


result = square(sum); // calling square() function
printf("Square of their sum is: %d\n", result);

return 0; // return statement for main


}

// ---------- Function Definition 1 ----------


int add(int x, int y) {
// parameter passing by value
int total = x + y;
return total; // returning result to main()
}

// ---------- Function Definition 2 ----------


int square(int n) {
return n * n; // returning the square of number
}

Output:
Enter two numbers: 5 3
Sum of 5 and 3 is: 8
Square of their sum is: 64
Recursive functions:

● A recursive function is a function that calls itself.


● It keeps calling itself until a stopping condition is reached.
● Base case : The condition to stop the function
● Recursive case : Function calls itself with smaller input
● Without Base case program will never stop (Infinite loop)
● Used in problems like factorial, Fibonacci, and GCD.

Syntax :

return_type function_name(parameters) {

if (base_condition){

return value;

}else{

function_name(arguments); // recursive call

Example :

#include <stdio.h>

int factorial(int n) {

if (n == 0)

return 1; // base case

else

return n * factorial(n - 1); // recursive call

int main() {

int num = 5;

printf("Factorial = %d\n", factorial(num));


return 0;

Output:

Factorial = 120

factorial(5) calls itself repeatedly:

5 * 4 * 3 * 2 * 1 = 120

You might also like