ProDeveloperTutorial.com

Tutorials and Programming Solutions
Menu
  • Shell Scripting
  • System Design
  • Linux System Programming
  • 4g LTE
  • Coding questions
  • C
  • C++
  • DSA
  • GIT
  • 450 DSA Cracker
  • 5G NR
  • O-RAN

Chapter 6: C language Functions

prodevelopertutorial July 3, 2018

xIn this chapter we study about:

  1. C function Declaration or function prototype.
  2. C function Definition
  3. Calling a function
  4. Types of functions in C
  5. Types of  function call
  6. More function examples
  7. Recursion Function

Functions are the core part of C language. In fact every C program has a function i.e main().

Function helps a program to become efficient. If a program has a set of statements to be executed in multiple parts, then we can include them in a function, hence reducing program size and complexity.

A function is just a set of statements included in a bock and given a name to that block.

 

C function declaration or function prototype

Function declaration allows the compiler to know about the function name, return type, and the input parameters.

Sometimes function declaration is also called as a function prototype.

This will help the compiler to know that there is a function with that name and will be used further in the program.

Example:

int sum (int a, int b); 

Here notice the semicolon after the function. This is how we declare a function. The parameter names are not important but the parameters are. Hence we can also define as below:

int sum(int , int);

 

C function definition

In the above section, we saw how to declare a function. Here we will see how to define a function.

Function syntax:

<returnType> <functionName> (parameters list)
{

     function body;

}

From the above syntax we can see that function has below parts:

  1. Function name: Function name is mandatory for a function. A function name should be short and meaningful.
  2. Function return type: This is a mandatory parameter. The return type gives the data type of the value that is going to be returned from the function. If the function is not returning anything, then it should be declared as void.
  3. Function parameters: This is an optional parameter. A function can accept an input parameter or it might not accept a parameter.
  4. Function body: In the function body, we write a set of statements that are needed to perform to get the desired output from the function.

Example:

int sum(int a , int b)
{

  return(a + b);

}

In the above function, “sum” is the function name, it returns “int” data type and it takes 2 int type parameters

In C language, all the function definition should be defined above main(). So that the compiler will get to know before that function is being executed.

If you are defining function after main(), then you have to declare the function before main() and define after main(). Else you will get the below warning:

warning: implicit declaration of function ‘sum’ [-Wimplicit-function-declaration]

 

Calling a function:

  1. The function should be called by the respective function name.
  2. You need to pass the exact function parameters and the type.
  3. If the function is returning a value, then the return value should be stored in a variable of same type.

 

Example:

result = sum(10, 20);

The full example of a function in C:

#include<stdio.h>

int sum(int a, int b); //Function declaration

int main()
{
int result = 0;

result = sum(10, 20); //Calling a function

return 0;
}


int sum(int a, int b) //Function definition
{

  return (a + b );

}

Types of functions in C

There are 4 types of user-defined functions in C. Below are the types of functions. We have given an example and also have explained the same.

  1. Function with no arguments (parameter) and no return value
  2. Function with arguments (parameter) and no return value
  3. Function with no arguments (parameter) and with a return value
  4. Function with arguments (parameter) and with a return value

 

  1. Function with no arguments (parameter) and no return value example:
#include<stdio.h>

void sum();

int main()
{

sum();

return 0;
}


void sum()
{

  printf("Called sum function\n");

}

  1. Function with arguments (parameter) and no return value
#include<stdio.h>

void sum(int a, int b);

int main()
{

sum(10, 20);

return 0;
}


void sum(int a, int b)
{
  printf("The sum is = %d\n",(a+b));

}
  1. Function with no arguments (parameter) and with a return value
#include<stdio.h>

int sum();

int main()
{
  int result = 0;
  result = sum(10, 20);
  printf("The result is = %d\n", result);
return 0;
}


int sum()
{
  return(10 + 20);

}

  1. Function with arguments (parameter) and with a return value
#include<stdio.h>

int sum(int a, int b);

int main()
{
  int result = 0;
  result = sum(10, 20);
  printf("The result is = %d\n", result);
return 0;
}


int sum(int a, int b)
{
  return (a+b);

}

Types of function calls:

Before we go into the topic, let us understand the type of arguments.

There are two types of arguments.

  1. Actual arguments: The arguments that we are sending while calling a function is called as Actual arguments.

Example:

sum (a, b);

In the above example, we are calling sum function. The parameters a, b are called as actual arguments.

  1. Formal arguments: The arguments that are used in function definition are called as Formal arguments.

Example:

int sum( int c, int d)
{

  return (c+ d);

}

In the above example, the arguments c, d are called as formal arguments.

There are 2 types of function calls in C:

  1. call by value
  2. call by reference

 

  1. Call By value:

In this type, when we make a function call using arguments, the actual arguments are copied into formal arguments.

Hence any changes in the formal arguments will not reflect in the actual arguments after the function has completed it’s execution.

Example:

#include<stdio.h>

void swap(int c, int d);

int main()
{
  int a = 10, b = 20;
  printf("The value of a = %d b = %d before function call \n", a, b);
  swap(a, b);
  printf("The value of a = %d b = %d after function call \n", a, b);
return 0;
}


void swap(int c, int d)
{
  int temp = 0;
  temp = c;
  c = d;
  d = temp;
  
  printf("The value of a = %d b = %d in swap function  \n", c, d);
  

}

Output:

The value of a = 10 b = 20 before function call

The value of a = 20 b = 10 in swap function 

The value of a = 10 b = 20 after function call

Hence from the above, we can see that the actual arguments are not changed due to formal arguments.

 

Call by reference:

In this type, we send the address of the actual arguments, instead of values. Hence if there is a change in formal arguments, that will be reflected in actual arguments also.

Example:

#include<stdio.h>

void swap(int *c, int *d);

int main()
{
  int a = 10, b = 20;
  printf("The value of a = %d b = %d before function call \n", a, b);
  swap(&a, &b);
  printf("The value of a = %d b = %d after function call \n", a, b);
return 0;
}


void swap(int *c, int *d)
{
  int temp = 0;
  temp = *c;
  *c = *d;
  *d = temp;
  
  printf("The value of a = %d b = %d in swap function  \n", *c, *d);

}

Output:

 

The value of a = 10 b = 20 before function call

The value of a = 20 b = 10 in swap function 

The value of a = 20 b = 10 after function call

Form the output we can infer that there is change in the actual arguments when we change formal arguments.

More function examples

Function as an argument

#include<stdio.h>


int double_num(int num)
{
	return (num * 2);
}

int square(int num)
{
	return num * num;
}

int main()
{
	int num = 20;

	int result = double_num(square(num)); // sending a function as an argument.
	// Here the square of the number is calculated.
	// Then double of that number is calculated
	// Square of 20 = 400
	//Double of 400 is 800 hence the result.

	printf("The result is %d\n", result );

}

Output:

The result is 800

Function with decision statement

#include<stdio.h>


int get_num()
{
	int num;
	printf("Enter a number \n");
	scanf("%d", &num);
	return num;
}



int main()
{
	// below we are calling a function inside if statement.
	if (get_num() % 2 == 0)
	{
		printf("The number is even\n");
	}
	else
	{
		printf("The number is odd\n");		
	}
	return 0;
}

Output:

Enter a number
4
The number is even

Function with looping statement

#include<stdio.h>


int get_num()
{
	int num;
	printf("Enter a number \n");
	scanf("%d", &num);
	return num;
}



int main()
{
	// below we are calling a function inside while statement.
	// till you enter -ve value, it will keep on looping
	while (get_num() > 0 )
	{
		printf("The number is greater than 0\n");
	}
	
	printf("The number is lesser than or equal to 0. Hence exit\n");		
	
	return 0;
}

Output:

Enter a number

3

The number is greater than 0

Enter a number

4

The number is greater than 0

Enter a number

-1

The number is lesser than or equal to 0. Hence exit.

Recursion Function

A function that calls itself is called as a recursion function.

Only user-defined functions are involved in recursion.

In recursion, there has to be a base condition, that will make to break the loop. Else it will become infinite recursive calls.

Example:

Get the factorial of a number using recursion

#include<stdio.h>

int factorial_recursion(int num)
{
	if(num == 1) return num; // base condition

	else
		return num * (factorial_recursion(num -1));

}

int main()
{
	int  num = 5;

	printf("The factorial of 5 is %d \n", factorial_recursion(5));
}

Output:

The factorial of 5 is 120

 

List Of Tutorials available in this website:

C Programming 20+ ChaptersC++ Programming 80+ Chapters
100+ Solved Coding QuestionsData Structures and Algorithms 85+ Chapters
System design 20+ ChaptersShell Scripting 12 Chapters
4g LTE 60+ ChaptersMost Frequently asked Coding questions
5G NR 50+ ChaptersLinux System Programming 20+ chapters
Share
Email
Tweet
Linkedin
Reddit
Stumble
Pinterest
Prev Article
Next Article

About The Author

prodevelopertutorial

Follow this blog to learn more about C, C++, Linux, Competitive Programming concepts, Data Structures.

Leave a Reply Cancel Reply

You must be logged in to post a comment.

ProDeveloperTutorial.com

Tutorials and Programming Solutions
Copyright © 2023 ProDeveloperTutorial.com
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie SettingsAccept
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT