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

Lecture 8 - Functions

Notes about c language programming

Uploaded by

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

Lecture 8 - Functions

Notes about c language programming

Uploaded by

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

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


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

int sum(int x, int y) int fact(int i)


{ {
int ans = 0; int j,k; j=1;
ans = x + y; for (k=2; k<=i; k++)
return ans j=j*k;
} return j;
}

 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

 known only to that function

 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

 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. 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. #include<stdio.h> 8. #include<stdio.h>
9. int main(){ 9. int main(){
10. int num1=11; 10. int num1=11;
11. int num2=13; 11. int num2=13;
12. swap(num1,num2); 12. swap(&num1, &num2);
13. printf("Num1 %d, Num2 %d\ 13. printf("Num1 %d, Num2
n", num1, num2); %d\n", num1, num2);
14. } 14. }
Array Arguments
1. #include <stdio.h>
2. int* convert(int *inArray, int n){
3. int i;
4. for(i=0;i<n;i++){
5. *(inArray + i) = 3**(inArray);
6. }
7. return inArray;
8. }
9. main()
10. {
11. int k;
12. int* addr;
13. int values[] = {4, 6, 7, 12, 7};
14. addr = convert(values, 5);
15. for( k = 0; k < 5; k++)
16. printf("%d ", values[k] );
17. }
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-
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. }

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy