Lecture 8 - Functions
Lecture 8 - Functions
PROGRAMMING IN C
Functions
Masoud H. Mahundi
mmahundi97@gmail.com
+255 713832252
Introduction
The functions are meant for some levels of reusability
If there is a piece of code we want to write repeatedly
We write it once and only use it whenever we want it
If we write codes frequently
We become prone to errors, including typing errors
When we want to change/edit – difficult
it is also difficult to trace the source of errors when they happen
Input 1
Processing output
Input 1
return-type: this is the data type of the expected return value – the output
function-name: the name of the function which follows the same 6 identifier rules
2. Write a function that takes in year of birth and returns the age of a person
Function Definition
Array Arguments 1. #include<stdio.h>
2. float sum(float scores[5]){
3. float total = 0;
4. int i;
5. for(i=0;i<5;i++)
6. total = total + scores[i];
7. return total;
8. }
9. int main(){
10. float marks[] = {5.6, 7, 3, 6.5, 4};
11. printf("The Total is %0.2f", sum(marks));
12. Return 0;
13. }
Variable Scope
The scope of a variable or any other identifier is the portion of the program in which it can
be identified or referenced
There are two scopes recognised – local and global and the identifiers bear the same name
Local variables and Global variables
Local Variables
declared inside a function and they
known only to that function
Global Variable
declared outside all functions
known to all functions
Variable Scope
1. #include<stdio.h>
2. int A, B;//Global Variables
3. int add(){
4. return A+B;
5. }
6. main(){
7. int answer;//local variable
8. A = 11;
9. B = 17;
10. answer = add();
11. printf("The Answer is %d", answer);
12. }
Passing arguments
There are two common ways to pass arguments to functions
Passing by value and passing by reference
Passing by Values
The function process arguments without affecting the original values.
The function processes copies of the variables and then gives the intended results, without
altering the originals.
The values in the calling (NOT called) function, therefore, are not altered
The values in the calling (NOT called) function, therefore, are TRULY ALTERED
It will give a funny value – a certain figure – without complaining for the number of arguments
Prototype are used to help the compiler catch that error and send a message during compilation
Function Prototypes
Function Prototype;
Tells the return type of the data that the function will return.
Tells the order in which the arguments are passed to the function
Function Prototypes
1. #include<stdio.h>
1. #include<stdio.h>
2. main(){
2. int sum(int x, int y) {
3. int num1, num2;
3. int ans = 0;
4. printf("Enter the two integers \n");
4. ans = x + y;
5. scanf("%d\t",&num1);
5. return ans;
6. scanf("%d",&num2);
6. }
7. printf("The Sum is %d\n", sum(num1));
7. main(){
8. }
8. int num1, num2;
9. int sum(int x, int y) {
9. printf("Enter the two integers \n");
10. int ans = 0;
10. scanf("%d\t",&num1);
11. ans = x + y;
11. scanf("%d",&num2);
12. return ans;
12. printf("The Sum is %d\n", sum(num2));
13. }
13. }
1. #include <stdio.h>
2. int add (int,int);//the prototype
3. main(){
4. printf("%d\n",add(3));
5. }
6. int add(int i, int j)
7. {
8. return i+j;
9. }
1. float product(float num1, float num2){
2. float result;
3. result = num1*num2;
4. return result;
5. }
1.
2. //FUNCTION DEFINITION
3. int squares(int num){
4. int res;
5. res = num*num;
6. return res;
7. }
8. //FUNCTION CALLING
9. #include<stdio.h>
10. main(){
11. int var1, result;
12. printf("Enter Your Number");
13. scanf("%d", &var1);
14. result = squares(var1);
15. printf("The square is %d", result);
16. }
17.