0% found this document useful (0 votes)
26 views3 pages

Functions in C Complete Guide v2

The document provides a comprehensive guide to functions in C programming, explaining their structure, types, and usage. It covers built-in library functions, user-defined functions, recursive functions, and the differences between call by value and call by reference. Key examples illustrate how to define, call, and utilize functions effectively.

Uploaded by

nagas2820
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)
26 views3 pages

Functions in C Complete Guide v2

The document provides a comprehensive guide to functions in C programming, explaining their structure, types, and usage. It covers built-in library functions, user-defined functions, recursive functions, and the differences between call by value and call by reference. Key examples illustrate how to define, call, and utilize functions effectively.

Uploaded by

nagas2820
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/ 3

Functions in C Programming - Complete Guide

1. Introduction to Functions

A function is a block of code that performs a specific task. It allows code reuse and modular programming.

In C, functions can be built-in (library functions) or user-defined.

2. Structure of a Function

A function in C has the following parts:

- Function Declaration (Prototype): Informs the compiler about the function.

- Function Definition: Contains the actual statements to execute.

- Function Call: Executes the function.

#include <stdio.h>

void greet() { // Function definition


printf("Hello, Sandeep!\n");
}

int main() {
greet(); // Function call
return 0;
}

3. Types of Functions

1. Library Functions: Built-in functions like printf(), scanf().

2. User-defined Functions: Created by the programmer to perform specific tasks.

3. Recursive Functions: A function that calls itself.

4. Function with Arguments and Return Value

A function can take parameters (arguments) and return a value to the caller.

#include <stdio.h>

int add(int a, int b) {


return a + b;
}

int main() {
int sum = add(5, 3);
printf("Sum: %d\n", sum);
return 0;
}

5. Call by Value vs Call by Reference

1. Call by Value: A copy of the variable is passed. Changes made do not affect the original value.

2. Call by Reference: The address of the variable is passed. Changes made affect the original value.

// Call by Value Example


#include <stdio.h>

void changeValue(int a) {
a = 10;
}

int main() {
int x = 5;
changeValue(x);
printf("%d\n", x); // Outputs 5 (unchanged)
return 0;
}

// Call by Reference Example


#include <stdio.h>

void changeValue(int *a) {


*a = 10;
}

int main() {
int x = 5;
changeValue(&x);
printf("%d\n", x); // Outputs 10 (changed)
return 0;
}

6. Recursive Functions

A recursive function is a function that calls itself.

#include <stdio.h>

int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}

int main() {
int num = 5;
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
}

7. Summary

- Functions help in modular programming and code reuse.

- Types include Library, User-defined, and Recursive functions.

- Call by Value passes a copy; Call by Reference passes the address.

You might also like