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
Introduction
With Functions
breaking down of a big program into small manageable units, sometimes called
modules – divide and conquer
Re-using the codes
Writing the repeating codes only once and so adding to the convenience of
programming
Encapsulating the implementation and share
Reducing the difficulties of making changes. As long as the fragment of code has been
written once, then editing it is also done once
Enhancing clarity in the codes
Function Definition
Input 1
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
Example 1: the function takes in two parameters, x and y, of type int. it processes them and
returns ans – the sum of the two numbers
Example 2: the function takes in one parameter of type int, processes it and returns j
Function Definition
1. #include<stdio.h>
2. int fact(int i){
3. int j,k; j=1;
4. for (k=2; k<=i; k++)
5. j=j*k;
6. return j;
7. }
8. main(){
9. int number, value;
10. printf("Please enter the number ");
11. scanf("%d",&number);
12. value = fact(number);// function calling
13. printf("\n");
14. printf("The factorial is %d",value);
15. printf("\n");
16. }
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
Global Variable
declared outside 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. }