.

C Programming Functions Assignment Help

Functions

A function is a sub program which has set of statements that together to perform a job. Every C program has at least one function, which is main(), the program starts executing from the main function. 

Advantages of function

  • It avoids code redundancy.
  • It shrinks length of program
  • Easy to maintain. Updates can be made in only one place.
  • Easy to understand the concept of program due to this modularity.
  • It leads to abstraction. Wherever we need the usage call the function by name and enough parameter, it returns the value.  
functions image 1

Syntax:

return_type function_name (argument list)

Set of statements

return_type: It may be any data type such as int, double, char, void, short etc. It indicates the datatype of the value returned by the function as output.

function_name: It should be valid identifier. However it is advised to give a meaningful name for the functions so that it would be easy to understand the purpose of function.

argument list: Argument list contains set of variables names associated with their data types. These are passed to the function as inputs.

Example:

int addition(int num1, int num2);

In the above example int is return data type, addition is function name, int num1 and int num2 are argument list.

Calling function:

The program control is transferred to the called function when a program encounters calling statement. A called function consists set of statements to perform a specified task and if it has return statement, it returns the output to the calling location and program control transferred back to the main program.


C Programming Functions Assignment Help By Online Tutoring and Guided Sessions from assignmenthippo.com


Syntax:

function_name(arguments);

Example:

res=addition (a,b);

In the above example addition is the function name, a,b are arguments(inputs) for the function. The returned value will be assigned to the variable ‘res’.

Example:

#include <stdio.h>

 /* function declaration */

int addition(int n1, int n2);

 int main ()

{'/* local variable declaration */'}

int a,b,sum ;

printf(“Enter the first value:”);

scanf(“%d”,&a);

printf(“Enter the second value:”);

scanf(“%d”,&b);

{'/* calling a function to add two values */'}

sum = addition(a,b);

printf( "The addition of two value is : %d", sum );

return 0;

 /* function returning the sum of two values */

int addition(int n1, int n2)

{'/* local variable declaration */'}

int s;

s=n1+n2;

return s;

Output:

Enter the first value:10

Enter the second value:20

The addition of two value is:30

Function call types

  • call by value
  • call by reference

call by value

It is the default method to call function by passing arguments. 

Example:

#include <stdio.h>

 /* function declaration */

int mul(int n1, int n2);

 int main ()

{'/* local variable declaration */'}

int a,b,m ;

printf(“Enter the first value:”);

scanf(“%d”,&a);

printf(“Enter the second value:”);

scanf(“%d”,&b);

{'/* calling a function to add two values */'}

m = mul(a,b);

printf( "The multiplication of two value is : %d", mul );

return 0;

 /* function returning the sum of two values */

int mul(int n1, int n2)

{'/* local variable declaration */'}

int s;

s=n1*n2;

return s;

Explanation:

In the above example, the variables declared in function declaration is called as formal arguments or formal parameters. The variable declared in function calling is called as actual arguments. The actual arguments hold the values.

Output:

Enter the first value:5

Enter the second value

The multiplication of two value is:10

Swapping numbers using Function Call by Value

#include <stdio.h>

void swap( int n1, int n2 )

int temp ;

{'/*Copying n1 value into temporary variable */'}

temp = n1 ;

{'/* Copying n2 value into n1*/'}

n1 = n2 ;

{'/*Copying temporary variable value into n2 */'}

n2 = temp ;

int main( )

int n1 = 10, n2 = 20 ;

printf("Before swapping: %d, %d", n1, n2);

{'/*calling swap function*/'}

swap(num1, num2);

printf("\nAfter swapping: %d, %d", n1, n2);

Output:

Before swapping: 10, 20

After swapping: 10, 20

Call by reference

Call by reference pass the addresses of actual parameters to the called function as input. The addresses are assigned into the formal parameters. The changes are made in the formal parameters affect the actual parameters.

Example:

Swapping numbers using Function Call by Reference

#include <stdio.h>

void swap( int *n1, int *n2 )

int temp ;

{'/*Copying n1 value into temporary variable */'}

temp = *n1 ;

{'/* Copying n2 value into n1*/'}

*n1 = *n2 ;

{'/*Copying temporary variable value into n2 */'}

*n2 = temp ;

int main( )

int n1 = 10, n2 = 20 ;

printf("Before swapping: %d, %d", n1, n2);

{'/*calling swap function*/'}

swap(&num1, &num2);

printf("\nAfter swapping: %d, %d", n1, n2);

Output:

Before swapping: 10, 20

After swapping: 20, 10

.