Computer Programming I: Functions Year 2020-2021
Computer Programming I: Functions Year 2020-2021
Lecture 5
Functions
Year 2020-2021
Goals
• Declare and define functions in C
• Define the scope of a variable
– Declare global, local and static variables
• Distinguish parameter passing to functions by value from
parameter passing by reference
• Build C programs making use of our own functions
• Create C programs that receive command line
arguments:
– argc
– argv[]
problem
subproblem program
function
solution
subproblem function
[ Variables declaration ]
Function body
result = sumAbs(op1,op2);
printf ("%d\n", result);
} int sumAbs (int op1, int op2){
if (isPositive (op1,op2))
return (op1+op2);
else return (-1*(op2+op1));
}
int main () {
int counter = 0;
printf ("COUNTER VALUE INSIDE THE while:\n");
while (counter < 10) {
if (counter == 5) pause ();
printf ("%d\n", ++counter);
}
pause ();
printf ("COUNTER VALUE: %d\n", counter);
return 0;
}
finish (void) {
char c;
printf ("Enter a character: ");
scanf (" %c", &c);
return (c == 'n');
}
Computer Programming I Lecture 5 18
Contents
1. Description
2. Functions with no parameters
a) Inter function communication: local, global and static variables
3. Functions with parameters by value
4. Functions with parameters by reference
a) Command line arguments
5. Recursive Functions
6. Exercises
Computer Programming I Lecture 5 19
Functions with no parameters
• These functions include no declaration of either:
– input data ⇒ void parameters
– output data ⇒ void result
• So, they do not communicate each other
void main ( void ) {
…
}
int main () {
int counter = 0;
printf ("COUNTER VALUE INSIDE THE while:\n");
while (counter < 10) {
if (counter == 5 ) pause ();
printf ("%d\n", ++counter);
}
pause ();
printf ("COUNTER VALUE: %d\n", counter);
return 0;
}
Formal parameter
Actual parameter
i = 3;
j = 5;
printf ("opValue result: %d\n", opValue (i, j));
printf ("First operand: %d\nSecond operand: %d\n", i, j);
return 0;
}
a = a + a;
b = b + a;
return b;
}
Computer Programming I Lecture 5 48
Parameter passing by value
#include <stdio.h> i 3
#include <stdlib.h>
j 5
int opValue (int a, int b); // prototype
b = b + a;
return b;
}
Computer Programming I Lecture 5 49
Parameter passing by value
#include <stdio.h> i 3
#include <stdlib.h>
j 5
int opValue (int a, int b); // prototype
return b;
}
Computer Programming I Lecture 5 50
Parameter passing by value
#include <stdio.h> i 3
#include <stdlib.h>
j 5
int opValue (int a, int b); // prototype
i = 3;
j = 5;
printf ("opReference result: %d\n", opReference(&i,&j));
printf ("First operand: %d\nSecond operand %d\n",i, j);
return 0;
}
*c = *c + *c;
*d = *d + *c;
return *d;
}
Computer Programming I Lecture 5 61
Parameter passing by reference
#include <stdio.h> i 36
#include <stdlib.h>
j 5
int opReference (int *c, int *d); // prototype
*d = *d + *c;
return *d;
}
Computer Programming I Lecture 5 62
Parameter passing by reference
#include <stdio.h> i 6
#include <stdlib.h>
j 11
5
int opReference (int *c, int *d); // prototype
return *d;
}
Computer Programming I Lecture 5 63
Parameter passing by reference
#include <stdio.h> i 6
#include <stdlib.h>
j 11
int opReference (int *c, int *d); // prototype
}
Computer Programming I Lecture 5 64
Parameter passing by reference
#include <stdio.h> i 6
#include <stdlib.h>
j 11
int opReference (int *c, int *d); // prototype
int main() {
int i = 0;
fn (i);
printf ("PASSING BY VALUE: The value of i is %d\n", i);
return 0;
}
void fn (int j) {
printf ("The value of j is %d\n", j);
j = 10;
}
int main() {
int i = 0;
fn (&i);
printf ("PASSING BY REFERENCE: The value of i is %d\n", i);
return 0;
}
int main(){
int array[10];
int pos = 4;
fn (pos, array);
printf ("The position %d is %d\n", pos, array[pos]);
return 0;
}
int main () {
char sentence[] = "Hello World";
printChars (sentence);
return 0;
}
OUTPUT:
The number of arguments is: 2 8
The program name is: ./print_args 8
Argument number 1 is: one argument, several words 8
OUTPUT:
The number of arguments is: 5 8
The program name is: ./print_args 8
Argument number 1 is: several 8
Argument number 2 is: words, 8
Argument number 3 is: several 8
Argument number 4 is: arguments 8
Computer Programming I Lecture 5 76
Contents
1. Description
2. Functions with no parameters
a) Inter function communication: local, global and static variables
3. Functions with parameters by value
4. Functions with parameters by reference
a) Command line arguments
5. Recursive Functions
6. Exercises
Computer Programming I Lecture 5 77
Recursive Functions
• An object is recursive when it is defined as a function of
itself:
𝑁! = 𝑁 × 𝑁 − 1 ! ,𝑁 > 0
𝑎! = 𝑎 ×𝑎!"# ,𝑏 > 0
& &"#
- 𝑎$ = 𝑎& + - 𝑎$
$%# $%#
1 , 𝑖𝑓 𝑁 ≤ 1
𝑁! = /
𝑁× 𝑁−1 ! , 𝑖𝑓 𝑁 > 1