BCSC 1102 : Intro To Programming Week 5
Dr. Shem Mbandu Angolo, PhD
The Co-operatetive University of Kenya
September - December 2024
Contents
1 Introduction to Functions 2
2 Function Declaration, Definition, and Calling 2
3 Parameter Passing (by value and by reference) 3
4 Scope and Lifetime of Variables 4
1
1 Introduction to Functions
Definition: A function is a block of code that performs a specific task.
Advantages:
• Code Reusability
• Better Organization
• Easier Debugging
Example:
1 # include < stdio .h >
2
3 void sayHello () {
4 printf (" Hello , World !\ n ") ;
5 }
6
7 int main () {
8 sayHello () ; // Function call
9 return 0;
10 }
Listing 1: Simple Function Example
2 Function Declaration, Definition, and Calling
Declaration: A function declaration tells the compiler about a function’s name,
return type, and parameters.
Definition: A function definition provides the actual body of the function.
Calling: A function call executes the function.
Example:
1 # include < stdio .h >
2
3 // Function Declaration
4 void greet () ;
5
6 // Function Definition
7 void greet () {
8 printf (" Greetings !\ n ") ;
9 }
10
11 int main () {
12 greet () ; // Function Call
13 return 0;
2
14 }
Listing 2: Function Declaration, Definition, and Calling
3 Parameter Passing (by value and by refer-
ence)
Parameter Passing: In C programming, arguments can be passed to functions
in two ways: by value and by reference.
By Value: Passes a copy of the argument’s value to the function. Changes
to the parameter do not affect the original argument.
• When a function is called, the values of the actual arguments are copied
to the function’s formal parameters.
• Any changes made to the formal parameters within the function do not
affect the actual arguments.
• This is the default method of parameter passing in C.
Example: By Value
1 # include < stdio .h >
2
3 void increment ( int a ) {
4 a = a + 1;
5 printf (" Inside function : % d \ n " , a ) ; // Value is
modified only inside the function
6 }
7
8 int main () {
9 int x = 10;
10 increment ( x ) ;
11 printf (" Outside function : % d \ n " , x ) ; // Original
value remains unchanged
12 return 0;
13 }
Listing 3: Pass by Value
By Reference: Passes the address of the argument to the function. Changes
to the parameter affect the original argument.
• Instead of copying the value, the address of the actual arguments is passed
to the formal parameters.
• Changes made to the formal parameters directly affect the actual argu-
ments.
3
• This allows the function to modify the actual arguments.
Example: By Reference
1 # include < stdio .h >
2
3 void increment ( int * a ) {
4 * a = * a + 1;
5 printf (" Inside function : % d \ n " , * a ) ; // Value is
modified both inside and outside the function
6 }
7
8 int main () {
9 int x = 10;
10 increment (& x ) ;
11 printf (" Outside function : % d \ n " , x ) ; // Original
value is modified
12 return 0;
13 }
Listing 4: Pass by Reference
4 Scope and Lifetime of Variables
Scope: The region of the program where a variable is accessible.
• Local Scope: Variables declared inside a function or block are local to
that function or block and cannot be accessed outside.
• Global Scope: Variables declared outside all functions are global and
can be accessed from any function within the program.
Lifetime: The duration for which a variable exists in memory.
• Local Variables: The lifetime of local variables is limited to the execu-
tion time of the function or block in which they are declared.
• Global Variables: The lifetime of global variables is the entire runtime
of the program.
• Static Variables: These retain their value between function calls and
their lifetime is the entire runtime of the program.
Example: Local Scope
1 # include < stdio .h >
2
3 void display () {
4 int a = 5; // Local variable
4
5 printf (" Local a : % d \ n " , a ) ;
6 }
7
8 int main () {
9 display () ;
10 // printf ("% d " , a ) ; // Error : a is not accessible
here
11 return 0;
12 }
Listing 5: Local Scope
Example: Global Scope
1 # include < stdio .h >
2
3 int globalVar = 10; // Global variable
4
5 void display () {
6 printf (" Global variable : % d \ n " , globalVar ) ;
7 }
8
9 int main () {
10 display () ;
11 printf (" Global variable in main : % d \ n " , globalVar )
;
12 return 0;
13 }
Listing 6: Global Scope
Example: Static Variables (Lifetime)
1 # include < stdio .h >
2
3 void count () {
4 static int num = 0; // Static variable
5 num ++;
6 printf (" Count : % d \ n " , num ) ;
7 }
8
9 int main () {
10 count () ;
11 count () ;
12 count () ;
13 return 0;
14 }
Listing 7: Static Variables