0% found this document useful (0 votes)
27 views

Chapter 4 Functions

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Chapter 4 Functions

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Chapter 4: Function and Passing Arguments to Function

4.1 Definition of Function

A function is a block of code designed to perform a specific task. It can be reused in the program to avoid r

Example:

#include <stdio.h>

void greet() {

printf("Hello, World!\n");

int main() {

greet();

return 0;

4.2 Declaration of Function

Function declaration specifies the name, return type, and parameters of a function, enabling the compiler to

Example:

int add(int, int); // Declaration

int main() {

printf("%d", add(5, 3));

return 0;

int add(int a, int b) {

return a + b;
}

4.3 Passing Value of a Function by Value

When passing by value, a copy of the argument's value is passed to the function. Changes made to the pa

Example:

#include <stdio.h>

void modify(int x) {

x = x + 10;

int main() {

int a = 5;

modify(a);

printf("%d", a); // Output: 5

return 0;

4.4 Passing Value of a Function by Reference

When passing by reference, the address of the argument is passed, allowing the function to modify the orig

Example:

#include <stdio.h>

void modify(int *x) {

*x = *x + 10;

int main() {

int a = 5;
modify(&a);

printf("%d", a); // Output: 15

return 0;

You might also like