Function Definition
This note is the function definition of C programming language. It includes the function prototype, parameter passed by value, parameter passing by reference and function that returns a value too.
Summary
This note is the function definition of C programming language. It includes the function prototype, parameter passed by value, parameter passing by reference and function that returns a value too.
Things to Remember
- The function prototype is the outline of a general function.
- A variable explicitly declared outside the function (outside main () also) is known as the global variable.
- Global variables are accessed by all the functions in the program.
- A function with a return value produces a value and returns it to the statement within the function that called it.
- C does not place any restriction on the type of return values except that the value cannot an array. Hence array cannot be the return type.
MCQs
No MCQs found.
Subjective Questions
No subjective questions found.
Videos
No videos found.

Function Definition
Function Definition
The body of the function begins just like in the same fashion as the main () function. The body of the function contains various statements, calculation modules, logic to determine different values and processing and return value. The return value is the type of value that should have been declared in the function declaration part. The general concept of a function definition is given below.
Parameter declaration
{ Body of function; Return expression; } |
Note:
- Generally, the function is defined before mains ().
- The function is generally called from the main ().
- The function defined before main follows a semicolon.
- The function definition contains function body and calculation section.
While defining a function, one has to remember the followings.
- Contains the actual codes for the program.
- The body of the function follows function declaration.
- Function declaration and a name of the function definition should be same with the same arguments and same return data type.
- Function declaration terminates with the semicolon (;).
- Function definition terminates without semicolon (;).
Example:
void main () { /* function definition*/ Program codes; } |
Function Prototype
The functionprototype is the outline of a general function. The prototype given below illustrates the different parts of a function.
#include <stdio.h> ......... .......... /* function declaration or prototype */ void function1 (); void function2 (); .......... ......... main () { ............. ........... /* calling function */ function1 (); function2 (); ............ ........... } /* function definition */ function (parameters) { ...................; body of the function; .....................; } |
# Write a program to find the sum of two numbers with a function sum ().
#include <stdio.h> #include <conio.h> void sum (); void main (); { printf(“/n Example of calling function “); sum (); /* function is called */ printf(“/n return from function”); } /* function defition */ void sum () { int a, b, t; printf(“\n print first number:”); scanf(“%d”, &a); printf(“\n print second number:”); scanf(“%d, &b); t=a+b; printf(“\n Sum of the two is %d”, t); } |
Functions handle the variables. The variables within the function itself are local to the function. A variable explicitly declared outside the function (outside main () also) is known as the global variable. Global variables are accessed by all the functions in the program. The use of global variable are with the help of scope resolution operator.(::) which is beyond this text.
Illustration of global variable the above program is changed to accept a global variable.
#include <stdio.h> #include <math.h> Float t; Void sum (); Void main () { Printf(“\n Example of calling function “); Sum; /* fucntion is called */ Printf(“\n Sum of the two is %4.2f”, t); Printf(“\n return from function”); } /* function defition */ Void sum () { Int a,b; Printf(“\n Print first number:”); Scanf(“%d”, &a); Printf(“\n Print second number:”); Scanf(“%d”, &b); t=a+b; } |
Function argument passed by value
#include <stdio.h> #include <conio.h> #include <math.h> sum (int,int); main () { printf(“\n Example of calling function:”); sum (15, 10); printf(“\n Return from the function.”); return 0; getch (); } sum (int a, int b) { int t; t=a+b; printf(“\n Value of A = %d”,a); printf(“\n Value of B =%d”, b); printf(“\n sum of %d and %d is %3.2f”, a, b, t); } |
Parameter passed by value
#include <stdio.h> #include <math.h>\ sum (int, int); main() { int tot; printf(“\n Example of calling function”); tot=sum (10, 15); printf(“\n Return from function\n”); printf(“\n The sum is %d”, tot); return0; } /*function definition */ sum (int a, int b) { int t; printf(“\n Value of a =%d\n”, a); printf(“\n value of b =%d\n”, b); t=a+b; return (t); } |
Function call with parameter passing by value.
#include <math.h> #include <conio.h> void change (int, int); void main () { int a = 15, b = 10; clrscr (); printf(“\n Before calling function”); printf(“\n Value of a = %d”, a); printf(“\n Value of b = %d\n\n”, b); printf(“\n After calling function”); change (a, b); printf(“\n After returning from function”); printf(“\n Value of A = %d”, a); printf(“\n Value of b = %d, b); } /* function definition */ void change (int a, int b) { int t; t=a; a=b; b=t; printf(“\n Value of a = %d”, a); printf(“\n Value of b = %d\n\n”, b); } |
Parameter passing by reference
#include <stdio.h> #include <math.h> #include <conio.h> void change (int &, int &); /* address of values */ main() { int a=15, b=10; clrscr (); printf(“\n Before calling funtion “); printf(“\n value of A = %d”, a); printf(“\n value of B = %d\n\n”, b); printf(“\n After function is called:”); change (a,b); /* function called by passing reference */ printf(\n After returning from function”); printf(\n Value of A = %d, a); printf(“\n Value of B = %d”, b); } /* function definition */ void change (int &a, int &b) { int t; t=a; a=b; b=t; printf(“\n Value of A = %d”,a); printf(“\n Value of b = %d\n”, b);\ } |
Interchanging the input values in the program using function. Here, the function is preceded by the main () function.
#include <stdio.h> /* interchanging the value */ void swap (int *a, int *b) { int temp; temp = *a; *a = *b; *b = temp; } void main () { int i, j; printf(“\n Enter first number:”); scanf(“%d”, &i); srintf(“\n Enter second number:”); scanf(“%d”, &j); printf(“\n Before swapping %d %d \n”, i, j ); swap (&i, &j); printf(&i, &j); printf(“\n After swapping %d %d”, i, j); } |
Function That Returns a Value
A function with a return value produces a value and returns it to the statement within the function that call it. Function with return values requires that a return statement is used so that the value is returned to the calling statement.
C does not place any restriction on the type of return values except that the value cannot form an array. Hence array cannot be the return type. A pointer to an array can be returned. But a function can return an array that is a part of a structure.
# Write a program to find the factorial of a number with the help of a function fact (unsigned int).
#include <stdio.h> #include <conio.h> long fact (unsigned int); /* function prototype */ void main (void) { unsigned int Num; long factorial; printf(“\n Enter a positive number:”); scanf(“%u”, &Num); factorial = fact (Num); printf(“\n The factorial of %u is %li”, Num, ffactorial); } long fact (unsigned int n) { long fact (unsigned int n) { long 1fact; int i; if (n==0) 1fact = 1; /* Factorial of 0 is 1 */ else { 1fact = 1; for (i=2; i<=n;i++) 1fact = 1fact *i; } return 1fact; } |
# Write a program to check whether a given word is palindrome by using function.
#include <stdio.h> #include <string.h> #include <ctype.h> enum Boolean{ false, true}; enum Boolean IsPalindrome (char string[]) { int left, right, len=strlen (string); enum boolean matched = true; if (len == 0) return true; left = 0; right = len-1; /* compare the first and last letter, second and last but one ..*/ while (left <right && matched) { if (string [left] != string[right]) matched =false; else { left++; right--; } } return matched; int main (void) { char string [40]; printf(“\n Program to test the given string is palindrome \n”); printf(“\n Enter a string:”); scanf(“%s”, string); if (ispalindrome (string) printf(“\n The given string %s is a palindrome \n”, string); else printf(“\n The given string %s is not a palindrome \n”, string); return 0; } |
References:
Khanal, R.C. Khanal, R.C. Computer Concept for XII. Pashupatigriha Marga, Thapathali, Kathmandu, Nepal: Ekta Books Distributors Pvt. Ltd., 2010. 197-205.
Adhikari, Deepak Kumar.,et.al., Computer Science XII,Asia Publication Pvt.Ltd
Lesson
Programming in C
Subject
Computer Science
Grade
Grade 12
Recent Notes
No recent notes.
Related Notes
No related notes.