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

Functions in C

Uploaded by

dark.sci.magic
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)
1 views5 pages

Functions in C

Uploaded by

dark.sci.magic
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

Detailed Notes on Functions in C

In C, functions are blocks of code designed to perform specific tasks. They allow for modular

programming,

reusability, and better code organization. Below is a detailed breakdown of functions in C:

1. Anatomy of a Function:

A typical function in C has the following structure:

return_type function_name(parameter_list) {

// Function body

return value; // Optional, depends on return_type

Components:

- Return Type: Specifies the type of value the function returns (e.g., int, float, char, void).

- Function Name: Identifier used to call the function.

- Parameter List: Input values passed to the function, enclosed in parentheses.

- Function Body: Contains the code to perform the function's task.

- Return Statement: Ends the function and optionally returns a value.

Example:

int add(int a, int b) { return a + b; }

2. Function Declaration (Prototype):

Functions must be declared before they are used. A function declaration tells the compiler about the
function's name, return type, and parameters.

Syntax:

return_type function_name(parameter_list);

Example:

int add(int a, int b);

3. Function Definition:

This is where the actual implementation of the function is provided. It matches the declaration.

Example:

int add(int a, int b) { return a + b; }

4. Function Call:

To execute a function, use its name followed by parentheses containing arguments (if any).

Example:

int result = add(5, 10);

5. Types of Functions:

a. Based on Return Type:

- Returning a Value:

Example:

int square(int x) { return x * x; }

- Void Functions:

Example:
void printMessage() { printf("Hello, World!"); }

b. Based on Arguments:

- With Arguments and Return Value:

int multiply(int a, int b) { return a * b; }

- Without Arguments but with Return Value:

int getNumber() { return 42; }

- With Arguments but No Return Value:

void greet(char name[]) { printf("Hello, %s!", name); }

- Without Arguments and Return Value:

void sayHello() { printf("Hello, World!"); }

6. Storage Classes and Scope:

- Global Functions: Declared outside any function. Accessible throughout the program.

- Static Functions: Declared with static. Accessible only within the file.

Example:

static void internalFunction() { printf("This is static!"); }

7. Recursion:

A function that calls itself.

Example:

int factorial(int n) {

if (n == 0) return 1;

return n * factorial(n - 1);

}
8. Advantages of Functions:

- Code Reusability: Avoid rewriting the same code multiple times.

- Modularization: Makes code easier to debug and understand.

- Maintainability: Updates can be done in one place.

- Scalability: Functions can be added or modified without affecting the rest of the program.

9. Example Program:

#include <stdio.h>

// Function declaration

int add(int a, int b);

void printMessage();

int main() {

int num1 = 5, num2 = 10;

// Function call

int sum = add(num1, num2);

printf("Sum: %d\n", sum);

printMessage();

return 0;

// Function definition

int add(int a, int b) {


return a + b;

void printMessage() {

printf("This is a function example.\n");

Key Notes:

- Always declare functions before calling them or use prototypes.

- Parameters are passed by value by default in C. To modify original data, use pointers.

- Use meaningful names for functions and parameters for better readability.

You might also like