Nothing Special   »   [go: up one dir, main page]

Cis 1101 - Programming 1: Function in C

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

24 Mar 2020

CIS 1101 – PROGRAMMING 1

FUNCTION IN C

Part 2

C FUNCTION: C FUNCTION ASPECTS

C Function Aspects Syntax

Function definition: Return_type function_name (parameters list)


 Function Header {
 Function Body Body of the function;
}

Function call Function_name (arguments list);

Function declaration Return_type function_name (parameters list);


(or prototype)

2
24 Mar 2020

C FUNCTION: STEPS IN FUNCTION CREATION

1. Write the Function Header.


 int calcSum(int x, int y)

2. Write a sample Function Call.


 Declare and initialize (if needed) the variables
used in the call and place them before the call.

C FUNCTION: FUNCTION CALL

Sample Function Call

int a, b;
int total;
a = 75;
b = 86;

total = calcSum(a,b);

4
24 Mar 2020

C FUNCTION: STEPS IN FUNCTION CREATION

3. Assume that the function call is in main().


 Draw the execution stack.
 Label the variables with names, values, and addresses.

C FUNCTION: EXECUTION STACK

Local variable Parameters

sum x y  Activation Record of calcSum()


• Pass-By-Copy (Call-By-Value )
? ? ?

B700 B500 B600

a b total  Activation Record of main()


• total = calcSum (a, b);
? ? ?

A100 A200 A300

6
24 Mar 2020

C FUNCTION: STEPS IN FUNCTION CREATION

4. Write the code of the function.


 If the return type is not void,
• declare the local variable of the return type first
• then write the return statement.

CALLING A C FUNCTION: CALL-BY-VALUE/PASS-BY-VALUE

 The value of the variable


is passed to the function
as parameter.

 The value of the actual parameter


variable is passed (or copied) into
the formal parameter variable.

 It is also known as Pass-By-Copy.

8
24 Mar 2020

CALLING A C FUNCTION: CALL-BY-VALUE/PASS-BY-VALUE

 The value of the actual parameter


cannot be modified by formal
parameter.

 Different memory is allocated for both


actual and formal parameters because
value of the actual parameter is copied to
formal parameter.

ACTUAL PARAMETER(S) VERSUS FORMAL PARAMETER(S)

 Actual parameter:
• The actual value that is passed into the function
by a caller, and it is often called as argument.

 Formal parameter:
• The identifier used in a function to stand for the
value that is passed into the function by a caller.

10
24 Mar 2020

CALLING A C FUNCTION: CALL-BY-REFERENCE

 The address of the variable is passed


to the function as parameter.

 The value of the actual parameter can be modified by


formal parameter.

 Same memory is used for both actual and formal


parameters since only address is used by both parameters.

11

CALLING A C FUNCTION: CALL-BY-REFERENCE

 The location (reference or address) of the actual


parameter variable is passed (or copied) into
the formal parameter variable.

 It is also known as Pass-By-Reference.

12
24 Mar 2020

CALLING A FUNCTION: SAMPLE


Given:
A program uses two variables d and e and it assigns d with a value of 30 and
e with 50. Then it will swap those values.
Tasks:
a) Create a function that will swap the values assigned to the given two variables d and e using
Pass-By-Value.

b) Write the code of the program described above using the created function and verify its output.

c) Create a function using pointers that will swap the values assigned to the given two variables d
and e using Pass-By-Reference.

d) Write the code of the program described above using the created function and verify its output.

13

CALLING A FUNCTION: SAMPLE


#include<stdio.h>
/* function prototype, also called function declaration */
void swap(int, int);
int main() C FUNCTION PROGRAM
{ (USING PASS-BY-VALUE):
int d = 30, e = 50;
/* Calling swap function by value */ In this program, the values
printf(“\nThe values before swapping: \nd = %d \ne = %d\n", d, e);
of the variables “d” and
swap(d,e);
printf("\nThe values after swapping: \nd= %d \ne = %d\n", d, e); “e” are passed to the
return 0; function “swap”.
}
/* Swap function definition */ These values are copied to
void swap(int a, int b) formal parameters “a”
{ and “b” in swap function
int tmp; and used.
tmp = a;
a = b;
b = tmp;
}

14
24 Mar 2020

CALLING A FUNCTION: SAMPLE


#include<stdio.h>
/* Function prototype, also called function declaration */
void swap(int*, int*); C FUNCTION PROGRAM
(USING PASS- BY-REFERENCE):
int main()
{ In this program, the address of the
int d = 30, e = 50; variables “d” and “e” are passed
/* Calling swap function by reference */ to the function “swap”.
printf(“The values before swapping: \nd = %d \ne = %d\n", d, e);
swap(&d, &e); These values are not copied to
printf(“\nThe values after swapping: \nd = %d \ne = %d\n", d, e); formal parameters “a” and “b”
return 0; in swap function because they
} are just holding the address of
those variables.
void swap(int *a, int *b)
{ This address is used to access
int tmp; and change the values of the
tmp = *a; variables.
*a = *b;
*b = tmp;
}

15

You might also like