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

Calling The C Function

The document discusses C language functions. It covers: 1. The parts of a function definition including return type, function name, parameters, and function body. Parameters can be formal or actual. 2. The two ways functions can be called: call by value where the value is passed, and call by reference where the address is passed. 3. An example of each - call by value does not modify the original variables, while call by reference can since it passes the addresses.

Uploaded by

Saif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Calling The C Function

The document discusses C language functions. It covers: 1. The parts of a function definition including return type, function name, parameters, and function body. Parameters can be formal or actual. 2. The two ways functions can be called: call by value where the value is passed, and call by reference where the address is passed. 3. An example of each - call by value does not modify the original variables, while call by reference can since it passes the addresses.

Uploaded by

Saif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Programming Fundamentals [C Language] by: SHAH NAWAZ,UoG,Lahore

Calling the C function

Topics Covered: Function that return a value, Parameters and use of arguments to pass data to a
function, Formal/ Actual parameters, Passing values between functions, Formal/ Actual
parameters

Function definition
A function definition in C programming consists of following parts–

syntax

return_type function_name( parameter list );

 Return Type − A function may return a value. The return_type is the data


type of the value the function returns. Some functions perform the desired
operations without returning a value. In this case, the return_type is the
keyword void.
 Function Name − This is the actual name of the function. The function
name and the parameter list together constitute the function signature.
 Parameters − parameters are variables defined in the function definition
which receive values passed from function call. When a function is
called/invoked, you also pass a value or set of values to the parameter. A
function may contain one, many or no parameters. The parameter list refers
to the type, order, and number of the parameters of a function. Parameters
are optional. Following are two types of parametres:
o Actual parameters are parameters which appear in function calls.

o Formal parameters are parameters which appear in function


declarations.
 Function Body − The function body contains a collection of statements that
define what the function does.

Function Name

Return Type int myfunction ( int a, int b ) Formal


{ Parameters
----
----
Function body ----
}
Programming Fundamentals [C Language] by: SHAH NAWAZ,UoG,Lahore

Call by Reference and Call by Value

There are two ways that a C function can be called from a program. They are
1. Call by value
2. Call by reference

1. Call by value:
 In call by value method, the value of the variable is passed to the
function as parameter.
 The value of the actual parameter can not be modified by formal
parameter.
 Different Memory is allocated for both actual and formal parameters.
Because, value of actual parameter is copied to formal parameter.

Example
#include<stdio.h>
void swap(int a, int b);
int main()
{
int m = 22, n = 44;
printf(" values before swap m = %d \nand n = %d", m, n);
swap(m, n);
}

void swap(int a, int b)


{
int tmp;
tmp = a;
a = b;
b = tmp;
printf(" \nvalues after swap m = %d\n and n = %d", a, b);
}

Output:
values before swap m = 22
and n = 44
values after swap m = 44
and n = 22
Programming Fundamentals [C Language] by: SHAH NAWAZ,UoG,Lahore

2. Call by reference:
 In call by reference method, 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.

Example
#include<stdio.h>
void swap(int *a, int *b);

int main()
{
int m = 22, n = 44;
printf("values before swap m = %d \n and n = %d",m,n);
swap(&m, &n);
}

void swap(int *a, int *b)


{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
printf("\n values after swap a = %d \nand b = %d", *a, *b);
}

Output:
values before swap m = 22 
and n = 44
values after swap a = 44
and b = 22
Programming Fundamentals [C Language] by: SHAH NAWAZ,UoG,Lahore
Programming Fundamentals [C Language] by: SHAH NAWAZ,UoG,Lahore

Example
#include <stdio.h>
#include <conio.h>

int myfunction(int, int)


void main()
{
clrscr();
int val1 = 100;
int val2 = 200;
int answer;
ret= myfunction(a,b);
printf(“Answer of calculation is =%d”, &answer);

}
int myfunction(int a, int b)
{
int c;
c=a+b;
return c;
}

Output:

Answer of calculation is = 300

You might also like