0% found this document useful (0 votes)
18 views4 pages

CProg 1 Chapter 5

It's all about computer

Uploaded by

salcedosheila17
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)
18 views4 pages

CProg 1 Chapter 5

It's all about computer

Uploaded by

salcedosheila17
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
You are on page 1/ 4

CHAPTER 5

METHODS
Lesson 1: Basic on Methods
Video Reference: Function / Methods In C Programming Language:
https://youtu.be/X7auVd3QpTM

Lesson 2: Procedures Parameter Handling


There are different ways in which parameter data can be passed into and out
of methods and functions. Let us assume that a function B() is called from
another function A(). In this case A is called the “caller function” and B
is called the “called function or callee function”. Also, the arguments
which A sends to B are called actual arguments and the parameters of B are
called formal arguments.

Terminology
Formal Parameter : A variable and its type as they appear in the prototype
of the function or method.
Actual Parameter : The variable or expression corresponding to a formal
parameter that appears in the function or method call in the calling
environment.
1. Pass By Value : This method uses in-mode semantics. Changes made to
formal parameter do not get transmitted back to the caller. Any
modifications to the formal parameter variable inside the called function or
method affect only the separate storage location and will not be reflected
in the actual parameter in the calling environment. This method is also
called as call by value.

Example:
#include <stdio.h>

37 | P a g e
void func(int a, int b)
{ a += b;
printf("In func, a = %d b = %d\n", a, b);
}
int main(void)
{ int x = 5, y = 7;

// Passing parameters func(x, y); printf("In main, x = %d y =


%d\n", x, y); return 0;
}

Output:

In func, a = 12 b = 7 In main, x = 5 y = 7

2. Pass by reference(aliasing) : This technique uses in/out-mode semantics.


Changes made to formal parameter do get transmitted back to the caller
through parameter passing. Any changes to the formal parameter are reflected
in the actual parameter in the calling environment as formal parameter
receives a reference (or pointer) to the actual data. This method is also
called as call by reference. This method is efficient in both time and
space.

// C program to illustrate
// call by reference #include <stdio.h> void swapnum(int* i, int* j)
{ int temp = *i; *i = *j;
*j = temp;
}

int main(void)
{
int a = 10, b = 20;

// passing parameters
swapnum(&a, &b);

printf("a is %d and b is %d\n", a, b); return 0;


38 | P a g e
}

Output:

a is 20 and b is 10

Lesson 3: Recursion

Recursion is the process of repeating items in a self-similar way. In


programming languages, if a program allows you to call a function inside the
same function, then it is called a recursive call of the function.
void recursion() {
recursion(); /* function calls itself */
}

int main() { recursion();


}

The C programming language supports recursion, i.e., a function to call


itself. But while using recursion, programmers need to be careful to define
an exit condition from the function, otherwise it will go into an infinite
loop.

Recursive functions are very useful to solve many mathematical problems,


such as calculating the factorial of a number, generating Fibonacci series,
etc.

Number Factorial

The following example calculates the factorial of a given number using a


recursive function −

#include <stdio.h>

unsigned long long int factorial(unsigned int i) {

if(i <= 1) { return 1;


} return i * factorial(i - 1);
}

int main() { int i = 12;


printf("Factorial of %d is %d\n", i, factorial(i)); return 0;
}

When the above code is compiled and executed, it produces the following
result −

Factorial of 12 is 479001600

39 | P a g e
Fibonacci Series

The following example generates the Fibonacci series for a given number
using a recursive function −

#include <stdio.h>

int fibonacci(int i) {

if(i == 0) { return 0;
} if(i == 1) { return 1;
}
return fibonacci(i-1) + fibonacci(i-2);
}

int main() {
int i;
for (i = 0; i < 10; i++) { printf("%d\t\n", fibonacci(i));
}
return 0;
}

When the above code is compiled and executed, it produces the following
result −

0
1
1
2
3
5
8
13
21
34

40 | P a g e

You might also like