A function is itself a block of code that can solve simple or complex tasks/calculations. Functions that perform calculations on the data provided to it is called “parameter” or “argument”. A function always returns a single value result.

Types of function:

1. Built in functions(Library functions)

a.) Inputting Functions.

b.) Outputting functions.

2. User defined functions.

a.) fact();

b.) sum();

Parts of a function:

1. Function declaration/Prototype/Syntax.

2. Function Calling.

3. Function Definition.

1.)Function Declaration:

Syntax: <return type><function name>(<type of argument>)

The declaration of the function name, its argument, and return type is called function declaration.

2.) Function Calling: The process of calling a function for processing is called function calling.

Syntax: =<var name>=<function name>(<list of argumnent>).

3.) Function definition:

The process of writing a code for performing any specific task is called function definition.

Syntax:<return type><function name> (<types of arguments>)

{ <statement-1>

<statement-2>

return(<value>)

}

Example: program based upon function: WAP to compute cube of a no. using function.

#include<stdio.h>
#include<conio.h> 
void main()
{
int c,n;
int cube(int); 
printf("Enter a no."); 
scanf("%d",&n); 
c=cube(n);
printf("cube of a no. is=%d",c);
}
int cube(int n)
{
c=n*n*n; 
return(c);
}

Passing parameters to a function:

Firstly, what are the parameters?

parameters are the values that are passed to a function for processing.

There are 2 types of parameters:

a.) Actual Parameters.

b.) Formal Parameters.

a.) Actual Parameters: These are the parameters which are used in main() function for function calling. Syntax: = Example: f=fact(n);

b.) Formal Parameters: These are the parameters that are used in function definition for processing.

Methods of parameters passing:

1.) Call by reference.

2.) Call by value.

1.) Call by reference: In this method of parameter passing, original values of variables are passed from calling program to function. Thus, any change made in the function can be reflected back to the calling program.

2.) Call by value. In this method of parameter passing, duplicate values of parameters are passed from calling program to function definition. Thus, Any change made in function would not be reflected back to the calling program.

# include<stdio.h>
# include<conio.h> 
void main()
{
   int a,b; 
   a=10; 
   b=20;
   void swap(int,int)
   printf("The value of a before swapping=%d",a); 
   printf("The value of b before swapping=%d",b); 
   void swap(a,b);
   printf("The value of a after swapping=%d",a); 
   printf("The value of b after swapping=%d",b);
}
void swap(int x, int y)
{
   int t; 
   t=x; 
   x=y; 
   y=t;
}
Also Read: Decision Statements

One comment on “Functions in c”

Write a comment