0% found this document useful (0 votes)
25 views

Lecture 8 - Functions

The document discusses functions in C programming. It defines what functions are, how they are defined and declared with prototypes. Functions allow breaking programs into reusable modules to reduce errors and improve readability. Parameters pass input values into functions, which are called arguments. Functions can return values. Functions scope determines where variables are accessible. Arguments can be passed by value or reference, affecting whether the original variable is modified.

Uploaded by

frankkinunghi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Lecture 8 - Functions

The document discusses functions in C programming. It defines what functions are, how they are defined and declared with prototypes. Functions allow breaking programs into reusable modules to reduce errors and improve readability. Parameters pass input values into functions, which are called arguments. Functions can return values. Functions scope determines where variables are accessible. Arguments can be passed by value or reference, affecting whether the original variable is modified.

Uploaded by

frankkinunghi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

UNIVERSITY OF DAR ES SALAAM

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

 Functions are meant for that


 They are defined – written once
 Are used frequently - calling
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

 The inputs in a function are called parameters in the definition

 Arguments in function calling

 Processing is where processing is done – function body

 Output in a function is called the return value


Function Definition
return-type function-name(parameters){
statement 1;
statement 2;
return result;
}

 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

 parameters: these are expected inputs of a function

 result: the output value itself

 The following is an example of a function definition


Function Definition 1. int fact(int i) {
2. int j,k; j=1;
3. for (k=2; k<=i; k++)
4. j=j*k;
5. return j;
6. }
7.
 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
 Exampleint
2: sum(int
the function
x, int y) takes in one parameter of type int, processes it and returns j
{
int ans = 0;
ans = x + y;
return ans
}
Function Definition

1. int iseven(int num){


2. if (num%2 == 0)
3. return 1;
4. else
5. return 0;
6. }
Wake Up
1. Write a function that takes one number and return its square

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

 Arguments are mere variables


Passing arguments
 Passing by Reference
 The function process arguments and it actually affects the original values.

 The function the original values using their memory addresses

 The values in the calling (NOT called) function, therefore, are TRULY ALTERED

 Arguments are memory addresses


Passing arguments
PASSING BY VALUE PASSING BY REFERENCE
1. void swap(int x, int y) 1. void swap(int *px, int *py)
2. { 2. {
3. int temp; 3. int temp;
4. temp = x; 4. temp = *px;
5. x = y; 5. *px = *py;
6. y = temp; 6. *py = temp;
7. } 7. }
8. // called as swap (a, b) – values 8. // called as swap (&a, &b)- address
1. #include<stdio.h>
2. void swap(int a, int b)// function definition
3. {
4. int tmp;
5. tmp = a;
6. a = b;
7. b = tmp;
8. }
9. main(){
10. int m = 22, n = 44;
11. // calling swap function by value
12. printf("Before swap m = %d and n = %d\n", m, n);
13. swap(m, n); //function calling
14. printf("After swap m = %d and n = %d\n", m, n);
15. }
1. #include<stdio.h>
2. void swap(int *a, int *b)// function definition
3. {
4. int tmp;
5. tmp = *a;
6. *a = *b;
7. *b = tmp;
8. }
9. main(){
10. int m = 22, n = 44;
11. // calling swap function by value
12. printf("Before swap m = %d and n = %d\n", m, n);
13. swap(&m, &n); //function calling
14. printf("After swap m = %d and n = %d\n", m, n);
15. }
Array Arguments
1. #include <stdio.h>
2. void convert(int *inArray, int n){
3. int i;
4. for(i=0;i<n;i++){
5. *(inArray + i) = 3**(inArray+i);
6. }
7. }
8. main()
9. {
10. int k;
11. int values[] = {4, 6, 7, 12, 7};
12. convert(values, 5);
13. for( k = 0; k < 5; k++)
14. printf("%d ", values[k] );
15. }
Function Positioning
1. In the same file as the main function
 Appropriate when the program is short
 Can be before or after the main function

2. Each function in a separate file


 Appropriate when the functions are big and complex

3. All functions to be in one file – special for functions


 when there many functions to be used

 For option number 2 and 3


 If the name of the file with functions is called functfile.c then there has to be in the pre-
processor directive. #include “functfile.c”
Function Prototypes
 Mostly appear when the function
 Is defined after the main function or in different files

 Meant to solve the number of arguments mismatch


 Defining a function with more than one parameter

 Calling it with fewer arguments

 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 number of arguments passed to the function.

 Tells the data types of the each of the passed arguments.

 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.

You might also like