PPA Unit-6
PPA Unit-6
C - Functions
int main()
{
... .. ...
... .. ...
functionName();
... .. ...
... .. ...
}
Subject Code 102 Subject PPA Faculty Name Naveen Kumar
The execution of a C program begins from the main() function.
When the compiler encounters functionName(); inside the main function, control of the program jumps to
void functionName()
And, the compiler starts executing the codes inside the user-defined function.
The control of the program jumps to statement next to functionName(); once all the codes inside the function definition are
executed.
Remember, function name is an identifier
and should be unique.
This is just an overview on user-defined
function. Visit these pages to learn more
on:
·
User-defined Function in C program
ming
· Types of user-defined Functions
Call by value
This method copies the actual value of an argument into the formal parameter of the
function. In this case, changes made to the parameter inside the function have no
effect on the argument.
Call by reference
This method copies the address of an argument into the formal parameter. Inside the
function, the address is used to access the actual argument used in the call. This
means that changes made to the parameter affect the argument.
Recursive functions are very useful to solve many mathematical problems, such as calculating the factorial of a
number, generating Fibonacci series, etc.
int factorial(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
Subject Code 102 Subject PPA Faculty Name Naveen Kumar
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;
return 0;
}
The static variable has the default value 0 which is provided by compiler.
1. #include <stdio.h>
2. void func() {
5. i++;
6. j++;
8. }
9. void main() {
10. func();
11. func();
12. func();
13. }
Output:
i= 1 and j= 1
i= 2 and j= 1
i= 3 and j= 1
C - Scope of variables
A scope in any programming is a region of the program where a defined variable can have its existence and
beyond that variable it cannot be accessed. There are three places where variables can be declared in C
programming language −
· Inside a function or a block which is called local variables.
· Outside of all functions which is called global variables.
· In the definition of function parameters which are called formal parameters.
Let us understand what are local and global variables, and formal parameters.
Subject Code 102 Subject PPA Faculty Name Naveen Kumar
Local Variables
Variables that are declared inside a function or block are called local variables. They can be used only by statements that are inside that function or block of code. Local variables are not known to
functions outside their own. The following example shows how local variables are used. Here all the variables a, b, and c are local to main() function.
#include <stdio.h>
int main () {
/* actual initialization */
a = 10;
b = 20;
c = a + b;
return 0;
}
A global variable can be accessed by any function. That is, a global variable is available for use throughout your entire program after its declaration. The following program show how
global variables are used in a program.
#include <stdio.h>
int main () {
/* actual initialization */
a = 10;
b = 20;
g = a + b;
return 0;
}
A program can have same name for local and global variables but the value of local variable inside a function will take preference. Here is an
example −
int main () {
return 0;
}
When the above code is compiled and executed, it produces the following result −
value of g = 10
int main () {
return 0;
}
return a + b;
}
When the above code is compiled and executed, it produces the following
result −
value of a in main() = 10
value of a in sum() = 10
value of b in sum() = 20
value of c in main() = 30