Logo

Navigation
  • Home
  • Services
    • ERP Solutions
    • Implementation Solutions
    • Support and Maintenance Solutions
    • Custom Solutions
    • Upgrade Solutions
    • Training and Mentoring
    • Web Solutions
    • Production Support
    • Architecture Designing
    • Independent Validation and Testing Services
    • Infrastructure Management
  • Expertise
    • Microsoft Development Expertise
    • Mobile Development
    • SQL Server Database and BI
    • SAP BI, SAP Hana, SAP BO
    • Oracle and BI
    • Oracle RAC
  • Technical Training
    • Learn Data Management
      • Business Intelligence
      • Data Mining
      • Data Modeling
      • Data Warehousing
      • Disaster Recovery
    • Learn Concepts
      • Application Development
      • Client Server
      • Cloud Computing Tutorials
      • Cluster Computing
      • CRM Tutorial
      • EDI Tutorials
      • ERP Tutorials
      • NLP
      • OOPS
      • Concepts
      • SOA Tutorial
      • Supply Chain
      • Technology Trends
      • UML
      • Virtualization
      • Web 2.0
    • Learn Java
      • JavaScript Tutorial
      • JSP Tutorials
      • J2EE
    • Learn Microsoft
      • MSAS
      • ASP.NET
      • ASP.NET 2.0
      • C Sharp
      • MS Project Training
      • Silverlight
      • SQL Server 2005
      • VB.NET 2005
    • Learn Networking
      • Networking
      • Wireless
    • Learn Oracle
      • Oracle 10g
      • PL/SQL
      • Oracle 11g Tutorials
      • Oracle 9i
      • Oracle Apps
    • Learn Programming
      • Ajax Tutorial
      • C Language
      • C++ Tutorials
      • CSS Tutorial
      • CSS3 Tutorial
      • JavaScript Tutorial
      • jQuery Tutorial
      • MainFrame
      • PHP Tutorial
      • VBScript Tutorial
      • XML Tutorial
    • Learn Software Testing
      • Software Testing Types
      • SQA
      • Testing
  • Career Training
    • Career Improvement
      • Career Articles
      • Certification Articles
      • Conflict Management
      • Core Skills
      • Decision Making
      • Entrepreneurship
      • Goal Setting
      • Life Skills
      • Performance Development
      • Personal Excellence
      • Personality Development
      • Problem Solving
      • Relationship Management
      • Self Confidence
      • Self Supervision
      • Social Networking
      • Strategic Planning
      • Time Management
    • Education Help
      • Career Tracks
      • Essay Writing
      • Internship Tips
      • Online Education
      • Scholarships
      • Student Loans
    • Managerial Skills
      • Business Communication
      • Business Networking
      • Facilitator Skills
      • Managing Change
      • Marketing Management
      • Meeting Management
      • Process Management
      • Project Management
      • Project Management Life Cycle
      • Project Management Process
      • Project Risk Management
      • Relationship Management
      • Task Management
      • Team Building
      • Virtual Team Management
    • Essential Life Skills
      • Anger Management
      • Anxiety Management
      • Attitude Development
      • Coaching and Mentoring
      • Emotional Intelligence
      • Stress Management
      • Positive Thinking
    • Communication Skills
      • Conversation Skills
      • Cross Culture Competence
      • English Vocabulary
      • Listening Skills
      • Public Speaking Skills
      • Questioning Skills
    • Soft Skills
      • Assertive Skills
      • Influence Skills
      • Leadership Skills
      • Memory Skills
      • People Skills
      • Presentation Skills
    • Finding a Job
      • Etiquette Tips
      • Group Discussions
      • HR Interviews
      • Interview Notes
      • Job Search Tips
      • Resume Tips
      • Sample Resumes
 

C Programming – Functions (Part-II)

By Brian Moriya | on May 22, 2006 |
C Language

Types of Functions

You may have noticed the discussion about the <data type> portion of the function declaration missing from the last section. The <data type> refers to the type of data that would be returned by the function when it finishes executing. Data types are the same as any variable data types plus another one called void. Void type functions do not return any values.

Function with no arguments and no return value:

  1.  void function_name();

Think of this as a self-contained block of reusable code that does not need any data from the outside or have to return any data back to its caller. Functions that print values are good candidates for this type of function.

Sometimes you have a list of data you want to print to the console and you keep copy and pasting it or retyping it over and over again in different places. This practice is error prone and unprofessional. Consider the following example.

  1.  void main() {
  2. int x = 10;
  3. int y = 5;
  4.  
  5. printf("value of x: %d", x); printf("value of y: %d", y); printf("n"); 
  6. x += y; 
  7. printf("value of x: %d", x); printf("value of y: %d", y); 
  8. y++; 
  9. printf("value of x: %d", x); printf("value of y: %d", y);
  10. }

The quality of this program would greatly be improved by moving the print statements into a function. printxy1.c:

  1.   #include <stdio.h></stdio.h> 
  2. //global variables. int x, y; 
  3. //Function for printing variable's values.
  4. void printxy(); 
  5. void main() { 
  6. x = 10;
  7. y = 5; 
  8. printxy(); 
  9. x += y; 
  10. printxy(); 
  11. y++; 
  12. printxy();
  13. } 
  14. void printxy() { printf("value of x: %d, ", x); printf("value of y: %d", y); printf("n");
  15. }

Now instead of retyping the same three lines over and over, you simply make a call to the function to perform the exact same task in a more readable and maintainable manner.

Here is the output.

Function with arguments and no return value:

  1.  void function_name(int var);

Did you notice in the last example that we had to move the x and y variables to the global scope in order to use this function type. This should be avoided whenever possible. Global variables are considered bad practice, but in reality there are times (especially in embedded systems development) where you just have to give in.

In our case we can fix it by using a function that takes arguments and does not have to return a value to its caller.

Let us change our function declaration from the last example to this:

  1.  void printxy(int val1, int val2);

This function declaration has a parameter list which is just a comma-separated list of variables that will be used within the function that the caller can assign values when the function is called.

Here is a good place to introduce the idea of function signatures. A signature is just a function declaration or prototype, but it is called as such because no two functions may have the same signature or the compiler will not be able to resolve the call to a function. The signature is made up of the data type, function name, and the parameter list.

Let us fix our program so that it does not need to rely on global variables. Changes are in bold.

printxy2.c:

  1.   #include <stdio.h></stdio.h> 
  2. //Function for printing variable's values. 2 arguments, no return value.
  3. void printxy(int x, int y); 
  4. void main() { 
  5. int x, y;
  6. x = 10;
  7. y = 5; 
  8. printxy(x, y); 
  9. x += y; 
  10. printxy(x, y); 
  11. y++; 
  12. printxy(x, y);
  13. } 
  14. void printxy(int x, int y) { printf("value of x: %d, ", x); printf("value of y: %d", y); printf("n");
  15. }

All we did was change the signature of our printxy function so it now takes two arguments and prints those values. Main is the caller, and passes the new function the values it currently has for x and y when it makes the call. Now we do not need to use global variables to access x and y, we can just pass local variables as arguments.

Here is the output.

Functions with no arguments and a return value:

  1.  <data type></data> function_name();

A function of this form takes no arguments, but does return a value of some variable local to the function back to its caller. The data type refers to what kind of data is returned by the function on exiting.

Sometimes global flags are used in a program to indicate the current state of something, this is very common in embedded systems applications or system level programming. That is one thing you might use this type of function for.

Instead of inventing some useless function, let us use the standard C function from the stdio.h library as an example. This is the prototype for the getchar function:

  1.  int getchar();

This function returns a character input from standard input as an integer. Although you would most likely set a char variable with this function.

  1.  char character = getchar();

That would set character to the value returned by getchar once it finished executing.

Function with arguments and a return value:

  1.  <data type></data> function_name(<data type></data> arg1, 
  2. <data type></data> arg2, ...);

This type allows you to use a variable from an external source within the scope of the function, and you can return data back to the caller.

Main is actually a function of this type, we just have not been passing it arguments or using it is return values for anything. It is not necessary to use or catch a function’s return value but you can if you want to.

Usually main takes the following form:

  1.  int main(int argc, char *argv[]);

For now, ignore the odd looking second parameter, its explanation is reserved for the pointers topic. This form of main is very useful because it allows your program to receive command-line arguments and return data. Unix, DOS, and Linux programmers know about this if they ever use terminal commands or write bash scripts that rely on the output of a command.

Let us modify our printxy example to show the name of the program before it runs and a message telling when it is finished. Ignore the fact that the argv argument is a pointer to an array of chars for now and focus on the fact that we are able to use the values passed through these function arguments.

Below is the latest source code for our printxy example. Changes are in bold.

printxy3.c:

  1.   #include <stdio.h></stdio.h>
  2.  #include <stdlib.h></stdlib.h> 
  3. //Function for printing variable's values. 2 arguments, no return value. 
  4. void printxy(int x, int y); 
  5. int main(int argc, char *argv[]) { 
  6. int x, y;
  7. x = 10;
  8. y = 5; 
  9. printf("%s is runningn", argv[0]); printxy(x, y); 
  10. x += y; printxy(x, y); 
  11. y++; printxy(x, y); 
  12. printf("%s is finishedn", argv[0]); 
  13. return 0;
  14. } 
  15. void printxy(int x, int y) { printf("value of x: %d, ", x); printf("value of y: %d", y); printf("n");
  16. }

The function main takes two arguments, argc and argv, we use the value in argv to print the name of the program and a message about the state of the program. Main returns 0 to indicate that it completed successfully.

You may be wondering where the program name was passed to main from. It is passed automatically by the command line as the first element in the argv array. In case you were wondering, argc is the number of arguments passed by the command-line when the program is launched from the terminal.

Here is what the output of printxy3 should look like.

« « PHP Tutorial – Syntax
C Programming – Functions (Part-I) » »

Author Description

Avatar

Free Training

RSSSubscribe 392 Followers
  • Popular
  • Recent
  • C Programming – Decision Making – Branching

    April 4, 2006 - 0 Comment
  • Call by Value and Call by Reference

    July 6, 2006 - 0 Comment
  • C Programming – Decision Making – Looping

    April 4, 2006 - 0 Comment
  • Concept of Pixel in C Graphics

    July 11, 2006 - 0 Comment
  • C Programming – Arrays

    April 13, 2006 - 0 Comment
  • TSR in C – An Introduction

    July 11, 2006 - 0 Comment
  • C Programming – Handling of Character String

    April 17, 2006 - 0 Comment
  • C Programming – Data Types : Part 2

    August 21, 2011 - 0 Comment
  • C Programming – An Overview

    March 2, 2006 - 0 Comment
  • C Programming – Functions (Part-I)

    May 23, 2006 - 0 Comment
  • C Programming – Data Types : Part 2

    August 21, 2011 - 0 Comment
  • C Circular Linked Lists

    June 26, 2011 - 0 Comment
  • C Doubly Linked Lists

    June 26, 2011 - 0 Comment
  • TSR in C – An Introduction

    July 11, 2006 - 0 Comment
  • Concept of Pixel in C Graphics

    July 11, 2006 - 0 Comment
  • Call by Value and Call by Reference

    July 6, 2006 - 0 Comment
  • C Language – The Preprocessor

    May 31, 2006 - 0 Comment
  • C Programming – File management in C

    May 31, 2006 - 0 Comment
  • C Programming – Linked Lists

    May 29, 2006 - 0 Comment
  • C Programming – Dynamic Memory allocation

    May 29, 2006 - 0 Comment

Exforsys e-Newsletter

ebook
 

Related Articles

  • C Programming – Data Types : Part 2
  • C Circular Linked Lists
  • C Doubly Linked Lists
  • TSR in C – An Introduction
  • Concept of Pixel in C Graphics

Latest Articles

  • Project Management Techniques
  • Product Development Best Practices
  • Importance of Quality Data Management
  • How to Maximize Quality Assurance
  • Utilizing Effective Quality Assurance Strategies
  • Sitemap
  • Privacy Policy
  • DMCA
  • Trademark Information
  • Contact Us
© 2023. All Rights Reserved.IT Training and Consulting
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.AcceptReject Read More
Privacy & Cookies Policy

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. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT