• TestingDocs
TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

C Tutorials

User-Defined Functions in C

Overview

User-Defined Functions are functions that are declared and defined by the programmer. User-Defined functions consist of the following components:

  • Function declaration/ Function Prototype
  • Function Definition
    • Function Header
    • Function Body
  • Function Invocation / Function Call

Example

Let’s look at a simple user-defined function example C program. The function Sum() computes the sum of two numbers.

/**
**********************************
* Program Description:
* C User-Defined Functions Demo
* Filename : userdefined.c
* C Tutorials - www.TestingDocs.com
*************************************
*/
#include<stdio.h>

/* Function Prototype */
int sum(int,int);

int main()
{
 /* Local variables declaration - main*/
 int a = 0, b = 0, c = 0;
 // Prompt user to enter a, b
 printf("Enter the number a = ");
 scanf("%d",&a);
 printf("Enter the number b = ");
 scanf("%d",&b);
 c = sum(a,b); // Function Call
 printf("The sum of %d , %d is = %d \n",a,b,c);
 return 0;
} // end main

/* Function Definition */
int sum(int x, int y) // Function Header
{
 /* Local variables declaration -sum*/
 int z;
 z = x + y;
 return z;
} // end sum

User-Defined Function Definition

C User-Defined Function Definition

Sample Output

Enter the number a = 7
Enter the number b = 9
The sum of 7 , 9 is = 16

 

C User defined function program output

 

The flowchart illustration of the flow control between the main() function and the user-defined function sum().
Function Flowchart Control TestingDocs

 

—

C Tutorials

C Tutorials on this website can be found at:

https://www.testingdocs.com/c-language-tutorial/

Related Posts

C Language Characteristics

C Tutorials /

C Language Characteristics

C Source Code .c File

C Tutorials /

C Program Files & Formats

GNU Compiler Collection GCC

C Tutorials /

C Compilers

C Pointers

C Tutorials /

C Pointers

C Tutorials /

C Two-Dimensional Arrays

‹ C Preprocessor Directives› C Arrays

Recent Posts

  • How to secure your SQL Database: Tips and Tricks
  • Shaping the Future of Development: Exploring Key Trends in Software Engineering
  • Improving Java Performance with Multithreading
  • Difference between PHP and JavaScript?
  • Bing Conversation Styles
  • ChatGPT Introduction
  • Open Source AI Frameworks
  • Artificial Intelligence Tools
  • Top AI Music Applications
  • Top AI Website Design Tools

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com